diff --git a/assets/audio/mixkit-crunchy-road-fast-walking-loop-1274.wav b/assets/audio/mixkit-crunchy-road-fast-walking-loop-1274.wav deleted file mode 100644 index 0ced0d0..0000000 Binary files a/assets/audio/mixkit-crunchy-road-fast-walking-loop-1274.wav and /dev/null differ diff --git a/assets/audio/mixkit-game-ball-tap-2073.wav b/assets/audio/mixkit-game-ball-tap-2073.wav deleted file mode 100644 index f99c64e..0000000 Binary files a/assets/audio/mixkit-game-ball-tap-2073.wav and /dev/null differ diff --git a/assets/audio/ravenwhoosh1a-101659.mp3 b/assets/audio/ravenwhoosh1a-101659.mp3 deleted file mode 100644 index 78fdf50..0000000 Binary files a/assets/audio/ravenwhoosh1a-101659.mp3 and /dev/null differ diff --git a/audio/Makefile b/audio/Makefile index a6537ad..7cb46f7 100644 --- a/audio/Makefile +++ b/audio/Makefile @@ -6,7 +6,7 @@ # By: whaffman +#+ # # +#+ # # 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) diff --git a/audio/audio b/audio/audio new file mode 100755 index 0000000..b395b82 Binary files /dev/null and b/audio/audio differ diff --git a/audio/audio.c b/audio/audio.c index 2f71d8c..66b1f38 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -1,6 +1,9 @@ -#include - #include "audio.h" +#include "libft.h" + +#include +#include +#include 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); } diff --git a/audio/audio.h b/audio/audio.h index 34648c5..dac0939 100644 --- a/audio/audio.h +++ b/audio/audio.h @@ -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 diff --git a/audio/main.c b/audio/main.c index b88dac6..ee431c3 100644 --- a/audio/main.c +++ b/audio/main.c @@ -1,84 +1,30 @@ #include "audio.h" #include +#include +#include +#include // 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); + t_audio *audio; + audio = audio_init(); + if (!audio) + { + fprintf(stderr, "Failed to initialize audio.\n"); + return 1; + } + if (audio_load_sounds(audio) != SUCCESS) + { + fprintf(stderr, "Failed to load sounds.\n"); + return 1; + } + printf("Audio initialized and sounds loaded successfully.\n"); - (void)pInput; -} - -int main(int argc, char** argv) -{ - 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) - { - 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; + 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); } diff --git a/assets/audio/mixkit-electricity-intense-hum-2138.wav b/audio/sounds/damage.wav similarity index 100% rename from assets/audio/mixkit-electricity-intense-hum-2138.wav rename to audio/sounds/damage.wav diff --git a/assets/audio/mixkit-mechanical-crate-pick-up-3154.wav b/audio/sounds/death.wav similarity index 100% rename from assets/audio/mixkit-mechanical-crate-pick-up-3154.wav rename to audio/sounds/death.wav diff --git a/assets/audio/birdwing-close-92442.mp3 b/audio/sounds/enemy.mp3 similarity index 100% rename from assets/audio/birdwing-close-92442.mp3 rename to audio/sounds/enemy.mp3 diff --git a/audio/sounds/flash.wav b/audio/sounds/flash.wav new file mode 100644 index 0000000..2d8db8b Binary files /dev/null and b/audio/sounds/flash.wav differ diff --git a/assets/audio/mixkit-ship-engine-loop-3077.wav b/audio/sounds/hum.wav similarity index 100% rename from assets/audio/mixkit-ship-engine-loop-3077.wav rename to audio/sounds/hum.wav diff --git a/assets/audio/mixkit-industrial-hum-loop-2139.wav b/audio/sounds/noise.wav similarity index 100% rename from assets/audio/mixkit-industrial-hum-loop-2139.wav rename to audio/sounds/noise.wav diff --git a/assets/audio/mixkit-screechy-electified-item-3213.wav b/audio/sounds/pickup.wav similarity index 100% rename from assets/audio/mixkit-screechy-electified-item-3213.wav rename to audio/sounds/pickup.wav diff --git a/assets/audio/mixkit-footsteps-in-a-tunnel-loop-543.wav b/audio/sounds/walk.wav similarity index 100% rename from assets/audio/mixkit-footsteps-in-a-tunnel-loop-543.wav rename to audio/sounds/walk.wav