Remove unused audio files and update audio handling in the project
This commit is contained in:
parent
c784b5e5f7
commit
71b6e2c877
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2025/06/02 13:18:45 by whaffman #+# #+# #
|
||||
# Updated: 2025/06/02 14:29:42 by whaffman ######## odam.nl #
|
||||
# Updated: 2025/06/11 17:41:19 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -14,8 +14,9 @@ NAME := audio
|
||||
CC := cc
|
||||
SRCS := $(wildcard *.c)
|
||||
OBJS := $(SRCS:.c=.o)
|
||||
CFLAGS := -Wall -Wextra -Werror -I../include
|
||||
LDLIBS := -lm -ldl -lpthread
|
||||
CFLAGS := -Wall -Wextra -Werror
|
||||
LDLIBS := -lm -ldl -lpthread -L../lib/libft -lft
|
||||
INCLUDES := -I../inc -I../lib/libft/inc
|
||||
RM := rm -f
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
@ -26,7 +27,7 @@ $(NAME): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(LDLIBS) -o $@ $^
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJS)
|
||||
|
||||
BIN
audio/audio
Executable file
BIN
audio/audio
Executable file
Binary file not shown.
@ -1,6 +1,9 @@
|
||||
#include <miniaudio.h>
|
||||
|
||||
#include "audio.h"
|
||||
#include "libft.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
t_audio *audio_init(void)
|
||||
{
|
||||
@ -10,11 +13,11 @@ t_audio *audio_init(void)
|
||||
audio = (t_audio *)malloc(sizeof(t_audio));
|
||||
if (!audio)
|
||||
return (NULL);
|
||||
ft_memset(audio, 0, sizeof(t_audio));
|
||||
memset(audio, 0, sizeof(t_audio));
|
||||
config = ma_engine_config_init();
|
||||
config.pUserData = audio;
|
||||
config.pLogCallback = NULL;
|
||||
config.logLevel = MA_LOG_LEVEL_INFO;
|
||||
// config.pUserData = audio;
|
||||
// config.pLogCallback = NULL;
|
||||
// config.logLevel = MA_LOG_LEVEL_INFO;
|
||||
if (ma_engine_init(&config, &audio->engine) != MA_SUCCESS)
|
||||
{
|
||||
free(audio);
|
||||
@ -32,10 +35,10 @@ void audio_uninit(t_audio *audio)
|
||||
i = 0;
|
||||
while (i < SND_SIZE)
|
||||
{
|
||||
if (audio->sounds[i])
|
||||
if (&audio->sounds[i])
|
||||
{
|
||||
ma_sound_uninit(&audio->sounds[i]);
|
||||
audio->sounds[i] = NULL;
|
||||
|
||||
}
|
||||
i++;
|
||||
}
|
||||
@ -47,20 +50,21 @@ int audio_load_sounds(t_audio *audio)
|
||||
{
|
||||
int i;
|
||||
const char *sound_files[SND_SIZE] = {
|
||||
"assets/sounds/walk.wav",
|
||||
"assets/sounds/flash.wav",
|
||||
"assets/sounds/damage.wav",
|
||||
"assets/sounds/death.wav",
|
||||
"assets/sounds/enemy.wav",
|
||||
"assets/sounds/pickup.wav",
|
||||
"assets/sounds/hum.wav",
|
||||
"assets/sounds/noise.wav"};
|
||||
"sounds/walk.wav",
|
||||
"sounds/flash.wav",
|
||||
"sounds/damage.wav",
|
||||
"sounds/death.wav",
|
||||
"sounds/enemy.mp3",
|
||||
"sounds/pickup.wav",
|
||||
"sounds/hum.wav",
|
||||
"sounds/noise.wav"};
|
||||
|
||||
if (!audio)
|
||||
return ();
|
||||
return (FAILURE);
|
||||
i = 0;
|
||||
while (i < SND_SIZE)
|
||||
{
|
||||
printf("Loading sound: %s\n", sound_files[i]);
|
||||
if (ma_sound_init_from_file(&audio->engine, sound_files[i],
|
||||
0, NULL, NULL, &audio->sounds[i]) != MA_SUCCESS)
|
||||
{
|
||||
@ -74,9 +78,9 @@ int audio_load_sounds(t_audio *audio)
|
||||
|
||||
int audio_play(t_audio *audio, t_sound sound)
|
||||
{
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !audio->sounds[sound])
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
|
||||
return (FAILURE);
|
||||
ma_sound_seek_to_pcm_frame(audio->sound[sound], 0);
|
||||
ma_sound_seek_to_pcm_frame(&audio->sounds[sound], 0);
|
||||
if (ma_sound_start(&audio->sounds[sound]) != MA_SUCCESS)
|
||||
return (FAILURE);
|
||||
return (SUCCESS);
|
||||
@ -84,9 +88,9 @@ int audio_play(t_audio *audio, t_sound sound)
|
||||
|
||||
int audio_stop(t_audio *audio, t_sound sound)
|
||||
{
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !audio->sounds[sound])
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
|
||||
return (FAILURE);
|
||||
ma_sound_seek_to_pcm_frame(audio->sound[sound], 0);
|
||||
ma_sound_seek_to_pcm_frame(&audio->sounds[sound], 0);
|
||||
if (ma_sound_stop(&audio->sounds[sound]) != MA_SUCCESS)
|
||||
return (FAILURE);
|
||||
return (SUCCESS);
|
||||
@ -94,19 +98,18 @@ int audio_stop(t_audio *audio, t_sound sound)
|
||||
|
||||
int audio_is_playing(t_audio *audio, t_sound sound)
|
||||
{
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !audio->sounds[sound])
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
|
||||
return (FAILURE);
|
||||
if (ma_sound_is_playing(&audio->sounds[sound]))
|
||||
return (SUCCESS);
|
||||
return (FAILURE);
|
||||
}
|
||||
|
||||
int audio_loop(t_audio *audio, t_sound sound)
|
||||
int audio_loop(t_audio *audio, t_sound sound, int loop)
|
||||
{
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !audio->sounds[sound])
|
||||
return (FAILURE);
|
||||
if (ma_sound_set_looping(&audio->sounds[sound], loop) != MA_SUCCESS)
|
||||
if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
|
||||
return (FAILURE);
|
||||
ma_sound_set_looping(&audio->sounds[sound], loop);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ typedef enum s_sound {
|
||||
|
||||
typedef struct s_audio {
|
||||
ma_engine engine; // The audio engine instance
|
||||
ma_sound *sounds[SND_SIZE]; // Array of sounds
|
||||
ma_sound sounds[SND_SIZE]; // Array of sounds
|
||||
char play_queue[SND_SIZE]; // Queue for playing sounds
|
||||
char stop_queue[SND_SIZE]; // Queue for stopping sounds
|
||||
char is_playing[SND_SIZE]; // Array to check if sounds are playing
|
||||
@ -26,9 +26,11 @@ typedef struct s_audio {
|
||||
|
||||
t_audio *audio_init(void);
|
||||
int audio_load_sounds(t_audio *audio);
|
||||
void audio_destroy(void);
|
||||
void audio_play(t_sound sound);
|
||||
|
||||
int audio_stop(t_audio *audio, t_sound sound);
|
||||
int audio_is_playing(t_audio *audio, t_sound sound);
|
||||
int audio_loop(t_audio *audio, t_sound sound, int loop);
|
||||
int audio_handle_queues(t_audio *audio);
|
||||
void audio_uninit(t_audio *audio);
|
||||
|
||||
// typedef struct s_audio {
|
||||
// ma_engine_config config; // Configuration for the audio engine
|
||||
|
||||
96
audio/main.c
96
audio/main.c
@ -1,84 +1,30 @@
|
||||
|
||||
#include "audio.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h> // For sleep function
|
||||
|
||||
|
||||
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
|
||||
int main(void)
|
||||
{
|
||||
ma_data_source_read_pcm_frames((ma_data_source*)pDevice->pUserData, pOutput, frameCount, NULL);
|
||||
|
||||
(void)pInput;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
t_audio *audio;
|
||||
audio = audio_init();
|
||||
if (!audio)
|
||||
{
|
||||
ma_result result;
|
||||
ma_device_config deviceConfig;
|
||||
ma_device device;
|
||||
ma_resource_manager_config resourceManagerConfig;
|
||||
ma_resource_manager resourceManager;
|
||||
ma_resource_manager_data_source dataSource;
|
||||
|
||||
if (argc < 2)
|
||||
fprintf(stderr, "Failed to initialize audio.\n");
|
||||
return 1;
|
||||
}
|
||||
if (audio_load_sounds(audio) != SUCCESS)
|
||||
{
|
||||
printf("No input file.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Device */
|
||||
deviceConfig = ma_device_config_init(ma_device_type_playback);
|
||||
deviceConfig.dataCallback = data_callback;
|
||||
deviceConfig.pUserData = &dataSource;
|
||||
result = ma_device_init(NULL, &deviceConfig, &device);
|
||||
if (result != MA_SUCCESS)
|
||||
{
|
||||
printf("Failed to initialize device.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* ResourceManager */
|
||||
resourceManagerConfig = ma_resource_manager_config_init();
|
||||
resourceManagerConfig.decodedFormat = device.playback.format;
|
||||
resourceManagerConfig.decodedChannels = device.playback.channels;
|
||||
resourceManagerConfig.decodedSampleRate = device.sampleRate;
|
||||
result = ma_resource_manager_init(&resourceManagerConfig, &resourceManager);
|
||||
if (result != MA_SUCCESS) {
|
||||
ma_device_uninit(&device);
|
||||
printf("Failed to initialize the resource manager.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Load the sound. */
|
||||
result = ma_resource_manager_data_source_init(
|
||||
&resourceManager,
|
||||
argv[1],
|
||||
MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_DECODE | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_ASYNC | MA_RESOURCE_MANAGER_DATA_SOURCE_FLAG_STREAM,
|
||||
NULL, /* Async notification. */
|
||||
&dataSource);
|
||||
if (result != MA_SUCCESS) {
|
||||
printf("Failed to load sound \"%s\".", argv[1]);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Looping. */
|
||||
ma_data_source_set_looping(&dataSource, MA_TRUE);
|
||||
|
||||
/* Start the device. */
|
||||
result = ma_device_start(&device);
|
||||
if (result != MA_SUCCESS) {
|
||||
ma_device_uninit(&device);
|
||||
printf("Failed to start device.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
printf("Press Enter to quit...\n");
|
||||
getchar();
|
||||
|
||||
/* Teardown. */
|
||||
ma_device_uninit(&device);
|
||||
ma_resource_manager_data_source_uninit(&dataSource);
|
||||
ma_resource_manager_uninit(&resourceManager);
|
||||
|
||||
return 0;
|
||||
fprintf(stderr, "Failed to load sounds.\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Audio initialized and sounds loaded successfully.\n");
|
||||
|
||||
audio->play_queue[SND_ENEMY] = 1; // Example to play enemy sound
|
||||
audio_handle_queues(audio);
|
||||
|
||||
sleep(10); // Keep the program running to hear the sound
|
||||
audio_uninit(audio);
|
||||
}
|
||||
|
||||
|
||||
BIN
audio/sounds/flash.wav
Normal file
BIN
audio/sounds/flash.wav
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user