Remove unused audio files and update audio handling in the project

This commit is contained in:
whaffman 2025-06-11 18:16:41 +02:00
parent c784b5e5f7
commit 71b6e2c877
16 changed files with 62 additions and 110 deletions

View File

@ -6,7 +6,7 @@
# By: whaffman <whaffman@student.codam.nl> +#+ # # By: whaffman <whaffman@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2025/06/02 13:18:45 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 CC := cc
SRCS := $(wildcard *.c) SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o) OBJS := $(SRCS:.c=.o)
CFLAGS := -Wall -Wextra -Werror -I../include CFLAGS := -Wall -Wextra -Werror
LDLIBS := -lm -ldl -lpthread LDLIBS := -lm -ldl -lpthread -L../lib/libft -lft
INCLUDES := -I../inc -I../lib/libft/inc
RM := rm -f RM := rm -f
.PHONY: all clean fclean re .PHONY: all clean fclean re
@ -26,7 +27,7 @@ $(NAME): $(OBJS)
$(CC) $(CFLAGS) $(LDLIBS) -o $@ $^ $(CC) $(CFLAGS) $(LDLIBS) -o $@ $^
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
clean: clean:
$(RM) $(OBJS) $(RM) $(OBJS)

BIN
audio/audio Executable file

Binary file not shown.

View File

@ -1,6 +1,9 @@
#include <miniaudio.h>
#include "audio.h" #include "audio.h"
#include "libft.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
t_audio *audio_init(void) t_audio *audio_init(void)
{ {
@ -10,11 +13,11 @@ t_audio *audio_init(void)
audio = (t_audio *)malloc(sizeof(t_audio)); audio = (t_audio *)malloc(sizeof(t_audio));
if (!audio) if (!audio)
return (NULL); return (NULL);
ft_memset(audio, 0, sizeof(t_audio)); memset(audio, 0, sizeof(t_audio));
config = ma_engine_config_init(); config = ma_engine_config_init();
config.pUserData = audio; // config.pUserData = audio;
config.pLogCallback = NULL; // config.pLogCallback = NULL;
config.logLevel = MA_LOG_LEVEL_INFO; // config.logLevel = MA_LOG_LEVEL_INFO;
if (ma_engine_init(&config, &audio->engine) != MA_SUCCESS) if (ma_engine_init(&config, &audio->engine) != MA_SUCCESS)
{ {
free(audio); free(audio);
@ -32,10 +35,10 @@ void audio_uninit(t_audio *audio)
i = 0; i = 0;
while (i < SND_SIZE) while (i < SND_SIZE)
{ {
if (audio->sounds[i]) if (&audio->sounds[i])
{ {
ma_sound_uninit(&audio->sounds[i]); ma_sound_uninit(&audio->sounds[i]);
audio->sounds[i] = NULL;
} }
i++; i++;
} }
@ -47,20 +50,21 @@ int audio_load_sounds(t_audio *audio)
{ {
int i; int i;
const char *sound_files[SND_SIZE] = { const char *sound_files[SND_SIZE] = {
"assets/sounds/walk.wav", "sounds/walk.wav",
"assets/sounds/flash.wav", "sounds/flash.wav",
"assets/sounds/damage.wav", "sounds/damage.wav",
"assets/sounds/death.wav", "sounds/death.wav",
"assets/sounds/enemy.wav", "sounds/enemy.mp3",
"assets/sounds/pickup.wav", "sounds/pickup.wav",
"assets/sounds/hum.wav", "sounds/hum.wav",
"assets/sounds/noise.wav"}; "sounds/noise.wav"};
if (!audio) if (!audio)
return (); return (FAILURE);
i = 0; i = 0;
while (i < SND_SIZE) while (i < SND_SIZE)
{ {
printf("Loading sound: %s\n", sound_files[i]);
if (ma_sound_init_from_file(&audio->engine, sound_files[i], if (ma_sound_init_from_file(&audio->engine, sound_files[i],
0, NULL, NULL, &audio->sounds[i]) != MA_SUCCESS) 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) 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); 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) if (ma_sound_start(&audio->sounds[sound]) != MA_SUCCESS)
return (FAILURE); return (FAILURE);
return (SUCCESS); return (SUCCESS);
@ -84,9 +88,9 @@ int audio_play(t_audio *audio, t_sound sound)
int audio_stop(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); 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) if (ma_sound_stop(&audio->sounds[sound]) != MA_SUCCESS)
return (FAILURE); return (FAILURE);
return (SUCCESS); 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) 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); return (FAILURE);
if (ma_sound_is_playing(&audio->sounds[sound])) if (ma_sound_is_playing(&audio->sounds[sound]))
return (SUCCESS); return (SUCCESS);
return (FAILURE); 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]) if (!audio || sound < 0 || sound >= SND_SIZE || !&audio->sounds[sound])
return (FAILURE);
if (ma_sound_set_looping(&audio->sounds[sound], loop) != MA_SUCCESS)
return (FAILURE); return (FAILURE);
ma_sound_set_looping(&audio->sounds[sound], loop);
return (SUCCESS); return (SUCCESS);
} }

View File

@ -17,7 +17,7 @@ typedef enum s_sound {
typedef struct s_audio { typedef struct s_audio {
ma_engine engine; // The audio engine instance 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 play_queue[SND_SIZE]; // Queue for playing sounds
char stop_queue[SND_SIZE]; // Queue for stopping sounds char stop_queue[SND_SIZE]; // Queue for stopping sounds
char is_playing[SND_SIZE]; // Array to check if sounds are playing 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); t_audio *audio_init(void);
int audio_load_sounds(t_audio *audio); int audio_load_sounds(t_audio *audio);
void audio_destroy(void); int audio_stop(t_audio *audio, t_sound sound);
void audio_play(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 { // typedef struct s_audio {
// ma_engine_config config; // Configuration for the audio engine // ma_engine_config config; // Configuration for the audio engine

View File

@ -1,84 +1,30 @@
#include "audio.h" #include "audio.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // For sleep function
int main(void)
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{ {
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; audio->play_queue[SND_ENEMY] = 1; // Example to play enemy sound
} audio_handle_queues(audio);
int main(int argc, char** argv) sleep(10); // Keep the program running to hear the sound
{ audio_uninit(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)
{
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;
} }

BIN
audio/sounds/flash.wav Normal file

Binary file not shown.