2023-08-18 15:37:56 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <libdragon.h>
|
|
|
|
|
|
|
|
#include "mp3_player.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_FREQUENCY (44100)
|
2024-01-17 15:43:02 +01:00
|
|
|
#define NUM_BUFFERS (4)
|
2023-08-18 15:37:56 +02:00
|
|
|
#define NUM_CHANNELS (2)
|
|
|
|
|
|
|
|
|
|
|
|
static bool sound_initialized = false;
|
|
|
|
|
|
|
|
|
2023-08-18 20:19:20 +02:00
|
|
|
static void sound_reconfigure (int frequency) {
|
|
|
|
if ((frequency > 0) && (audio_get_frequency() != frequency)) {
|
2023-08-18 15:37:56 +02:00
|
|
|
if (sound_initialized) {
|
|
|
|
mixer_close();
|
|
|
|
audio_close();
|
|
|
|
}
|
2023-08-18 20:19:20 +02:00
|
|
|
audio_init(frequency, NUM_BUFFERS);
|
2023-08-18 15:37:56 +02:00
|
|
|
mixer_init(NUM_CHANNELS);
|
|
|
|
mp3player_mixer_init();
|
|
|
|
sound_initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 20:19:20 +02:00
|
|
|
|
|
|
|
void sound_init_default (void) {
|
|
|
|
sound_reconfigure(DEFAULT_FREQUENCY);
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:37:56 +02:00
|
|
|
void sound_init_mp3_playback (void) {
|
2023-08-18 20:19:20 +02:00
|
|
|
sound_reconfigure(mp3player_get_samplerate());
|
2023-08-18 15:37:56 +02:00
|
|
|
}
|
|
|
|
|
2023-10-10 21:12:53 +02:00
|
|
|
void sound_deinit (void) {
|
|
|
|
if (sound_initialized) {
|
|
|
|
mixer_close();
|
|
|
|
audio_close();
|
|
|
|
sound_initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:37:56 +02:00
|
|
|
void sound_poll (void) {
|
|
|
|
if (sound_initialized && audio_can_write()) {
|
|
|
|
short *audio_buffer = audio_write_begin();
|
|
|
|
mixer_poll(audio_buffer, audio_get_buffer_length());
|
|
|
|
audio_write_end();
|
|
|
|
}
|
|
|
|
}
|