From 3dc304b8299cc005e526692f1f227809c0043db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= Date: Sun, 8 Nov 2020 10:06:41 +0100 Subject: [PATCH] wip audio #1 --- .gitignore | 1 + src/engine/include/modules/audio.hpp | 9 +- src/engine/modules/audio.cpp | 118 ++++++++++++++++++--------- src/samples/ari/ari.cpp | 11 ++- 4 files changed, 97 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index e201fb4..11ec5ab 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.[Ww][Aa][Vv] *.[Dd][Ff][Ff] *.[Ii][Rr][Xx] +*.[Aa][Dd][Pp][Cc][Mm] *.[Mm][Dd]2 # --- diff --git a/src/engine/include/modules/audio.hpp b/src/engine/include/modules/audio.hpp index 4d8644f..8766491 100644 --- a/src/engine/include/modules/audio.hpp +++ b/src/engine/include/modules/audio.hpp @@ -35,16 +35,18 @@ public: void play(); void stop(); void loadSong(char *t_filename); + void test(); void unloadSong(); void setVolume(u8 t_volume); void addListener(AudioListener *t_listener); void startThread(); + const u8 isPlaying() const { return shouldPlay; } private: void work(); static void audioThread(); - u8 isInitialized, shouldPlay, songLoaded, isVolumeSet; - + u8 isInitialized, shouldPlay, songLoaded, isVolumeSet, latestVolume, isADPCMLoaded, wasADPCMLoaded; + audsrv_adpcm_t adpcmSettings; ee_thread_t audioThreadAttr; u8 audioThreadStack[STACK_SIZE] ALIGNED(16); int audioThreadId; @@ -56,6 +58,9 @@ private: void loadModules(); void initAUDSRV(); + u8 *sample1; + u32 adpcmFileSize; + int ret; char chunk[2048]; FILE *wav; diff --git a/src/engine/modules/audio.cpp b/src/engine/modules/audio.cpp index 845af05..d665b7b 100644 --- a/src/engine/modules/audio.cpp +++ b/src/engine/modules/audio.cpp @@ -29,6 +29,7 @@ Audio::Audio() songLoaded = 0; isVolumeSet = 0; shouldPlay = 0; + isADPCMLoaded = false; audioRef = this; initSema(); loadModules(); @@ -54,6 +55,7 @@ void Audio::init(u32 t_listenersAmount) listenersAmount = t_listenersAmount; listeners = new AudioListener *[t_listenersAmount]; isInitialized = true; + latestVolume = 100; PRINT_LOG("Audio module initialized!"); } @@ -91,6 +93,7 @@ void Audio::initAUDSRV() { PRINT_LOG("Initialize AUDSRV started"); ret = audsrv_init(); + ret = audsrv_adpcm_init(); if (ret != 0) { PRINT_ERR("Failed to initialize AUDSRV!"); @@ -147,6 +150,7 @@ void Audio::loadSong(char *t_filename) void Audio::setVolume(u8 t_volume) { audsrv_set_volume(t_volume); + latestVolume = t_volume; isVolumeSet = true; } @@ -167,41 +171,23 @@ void Audio::startThread() PRINT_LOG("Audio thread started"); } -/** Do not call this function. - * This is a audio thread, runned by AudioThread::start() -*/ -void Audio::audioThread() -{ - while (true) - { - if (audioRef->shouldPlay) - audioRef->work(); - } -} - -void Audio::play() -{ - if (!isInitialized) - { - PRINT_ERR("Please initialize audio class first!"); - return; - } - shouldPlay = 1; -} - -void Audio::stop() -{ - if (!isInitialized) - { - PRINT_ERR("Please initialize audio class first!"); - return; - } - shouldPlay = 0; -} - /** Play song and run it again on finish */ void Audio::work() { + if (isADPCMLoaded) + { + if (audsrv_load_adpcm(&adpcmSettings, sample1, adpcmFileSize)) + PRINT_ERR("audsrv_load_adpcm() failed!"); + wasADPCMLoaded = true; + isADPCMLoaded = false; + } + if (wasADPCMLoaded) + { + if (audsrv_play_adpcm(&adpcmSettings)) + PRINT_ERR("audsrv_play_adpcm() failed!"); + } + if (!shouldPlay) + return; if (!isTrackDone) { ret = fread(chunk, 1, sizeof(chunk), wav); @@ -231,15 +217,71 @@ void Audio::work() /** Unload audio module by closing file stream and stopping AUDSRV */ void Audio::unloadSong() { - PRINT_LOG("Unloading audio module started"); + PRINT_LOG("Song unloaded"); fclose(wav); - PRINT_LOG("Song file stream closed"); - audsrv_quit(); - PRINT_LOG("AUDSRV stopped"); - PRINT_LOG("Audio module unloaded"); } -/** Do not call this function. +void Audio::test() +{ + // audsrv_adpcm_set_volume(100); + + FILE *file = fopen("host:chiller.adpcm", "rb"); + fseek(file, 0L, SEEK_END); + adpcmFileSize = ftell(file); + sample1 = new u8[adpcmFileSize]; + rewind(file); + fread(sample1, sizeof(u8), adpcmFileSize, file); + fclose(file); + adpcmSettings.size = 0; + adpcmSettings.buffer = 0; + adpcmSettings.loop = 0; + adpcmSettings.pitch = 0; + adpcmSettings.channels = 0; + isADPCMLoaded = true; +} + +void Audio::play() +{ + if (!isInitialized) + { + PRINT_ERR("Please initialize audio class first!"); + return; + } + if (!shouldPlay) + { + shouldPlay = 1; + audsrv_set_volume(latestVolume); + } +} + +void Audio::stop() +{ + if (!isInitialized) + { + PRINT_ERR("Please initialize audio class first!"); + return; + } + if (shouldPlay) + { + shouldPlay = 0; + audsrv_set_volume(0); + } +} + +// --- + +/** + * Do not call this function. + * This is a audio thread, runned by AudioThread::start() + */ +void Audio::audioThread() +{ + while (true) + audioRef->work(); +} + +/** + * Do not call this function. * This is a static callback function * for AUDSRV fillbuffer (semaphore) */ diff --git a/src/samples/ari/ari.cpp b/src/samples/ari/ari.cpp index 83c3fc0..5639422 100644 --- a/src/samples/ari/ari.cpp +++ b/src/samples/ari/ari.cpp @@ -32,10 +32,9 @@ Ari::~Ari() {} void Ari::onInit() { - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); engine->audio.init(0); - engine->audio.setVolume(40); + engine->audio.setVolume(30); engine->audio.loadSong("MOV-CIRC.WAV"); engine->audio.play(); @@ -88,7 +87,15 @@ void Ari::initBulb() void Ari::onUpdate() { if (engine->pad.isCrossClicked) + { + // engine->audio.stop(); + engine->audio.test(); + // engine->audio.unloadSong(); + // engine->audio.loadSong("nob-else.wav"); + // engine->audio.play(); + printf("FPS:%f\n", engine->fps); + } camera.update(engine->pad, player.mesh); engine->renderer->draw(skybox); engine->renderer->draw(island);