Skip to content

Commit

Permalink
Fixup wav audio playback
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Nov 6, 2023
1 parent 1c2d2af commit 90870a9
Show file tree
Hide file tree
Showing 39 changed files with 77 additions and 93 deletions.
Binary file modified nitrofiles/bgm/baby_blue.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/clear_waters_1.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/clear_waters_2.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/rodrigo_y_gabriela.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/sauls_theme.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/the_cousins_1.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/the_cousins_2.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/the_final_cook.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/title_hook.wav
Binary file not shown.
Binary file modified nitrofiles/bgm/title_loop.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/acceptable.raw
Binary file not shown.
Binary file added nitrofiles/sfx/acceptable.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/click.raw
Binary file not shown.
Binary file added nitrofiles/sfx/click.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/crank.raw
Binary file not shown.
Binary file added nitrofiles/sfx/crank.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/dialogue_bleep.raw
Binary file not shown.
Binary file added nitrofiles/sfx/dialogue_bleep.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/goodbye_walter.raw
Binary file not shown.
Binary file added nitrofiles/sfx/goodbye_walter.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/menu_drum.raw
Binary file not shown.
Binary file added nitrofiles/sfx/menu_drum.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/menu_select.raw
Binary file not shown.
Binary file added nitrofiles/sfx/menu_select.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/minerals.raw
Binary file not shown.
Binary file added nitrofiles/sfx/minerals.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/pestle.raw
Binary file not shown.
Binary file added nitrofiles/sfx/pestle.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/success_bell.raw
Binary file not shown.
Binary file added nitrofiles/sfx/success_bell.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/vaccum.raw
Binary file not shown.
Binary file added nitrofiles/sfx/vaccum.wav
Binary file not shown.
Binary file removed nitrofiles/sfx/valve_turn.raw
Binary file not shown.
Binary file added nitrofiles/sfx/valve_turn.wav
Binary file not shown.
6 changes: 2 additions & 4 deletions sound/bgm/convert.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# for i in *.ogg; do ffmpeg -i "$i" -y -f s8 -acodec pcm_s8 -ar 11025 -vn "../../nitrofiles/bgm/${i%.*}.raw"; done

# Convert all *ogg files to 16-bit 44.1kHz PCM .wav files in ../../nitrofiles/bgm/file.wav
for i in *.ogg; do ffmpeg -i "$i" -y -f s16le -acodec pcm_s16le -ar 44100 -vn "../../nitrofiles/bgm/${i%.*}.wav"; done
# Converts ogg soundtrack files into metadata-stripped WAVs
for i in *.ogg; do ffmpeg -y -i "$i" -map_metadata -1 -acodec pcm_s16le -ac 2 -ar 22050 -fflags +bitexact -flags:v +bitexact -flags:a +bitexact "../../nitrofiles/bgm/${i%.*}.wav"; done
3 changes: 2 additions & 1 deletion sound/sfx/convert.sh
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
for i in *.ogg; do ffmpeg -i "$i" -y -f s8 -acodec pcm_s8 -ar 11025 -vn "../../nitrofiles/sfx/${i%.*}.raw"; done
# Converts ogg sfx files into metadata-stripped WAVs
for i in *.ogg; do ffmpeg -y -i "$i" -map_metadata -1 -acodec pcm_s16le -ac 2 -ar 22050 -fflags +bitexact -flags:v +bitexact -flags:a +bitexact "../../nitrofiles/sfx/${i%.*}.wav"; done
4 changes: 2 additions & 2 deletions source/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ Game::Game()
sys.mem_bank = nullptr;
sys.fifo_channel = FIFO_MAXMOD;
mmInit(&sys);
Audio::initAudioStream();
Audio::LoadSFXs();
Audio::SetupAudio();

// Load global save
globalSave.loadData();
Expand Down Expand Up @@ -1586,6 +1585,7 @@ void Game::Update()

// Update sounds
// sound.Update(frame);
mmStreamUpdate();

// Refresh shadow OAM copy
NF_SpriteOamSet(1);
Expand Down
60 changes: 33 additions & 27 deletions source/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace Audio

int WAV::loadWAV(const char *name)
{
// consoleDemoInit();
// setBrightness(2, 0);
// setBrightness(1, 0);
// setBrightness(0, 0);
printf("Loading %s\n", name);

free_();
_loops = 0;
char buffer[100];
Expand Down Expand Up @@ -101,33 +95,36 @@ namespace Audio
_dataStart = ftell(f);

_loaded = true;
printf("Loaded %s\n", name);
return 0;
}

void WAV::free_()
{
if (!_loaded)
{
return;
}
delete[] _filename;
_filename = nullptr;
fclose(_stream);
_stream = nullptr;
_loaded = false;
}

void initAudioStream()
void SetupAudio()
{
// Setup stream
mm_stream stream;

stream.sampling_rate = 44100;
stream.buffer_length = 8000;
stream.callback = fillAudioStream;
stream.format = MM_STREAM_16BIT_STEREO;
stream.timer = MM_TIMER0;
stream.manual = 1;

mmStreamOpen(&stream);

// Load sound effects
LoadSFXs();
}

void WAV::play()
Expand All @@ -154,14 +151,23 @@ namespace Audio
void WAV::stop()
{
if (!_active)
{
return;
}
_active = false;
if (_prev != nullptr)
{
_prev->_next = _next;
}
else
{
playingWavHead = _next;
}

if (_next != nullptr)
{
_next->_prev = _prev;
}
if (deleteOnStop)
{
free_();
Expand All @@ -188,15 +194,23 @@ namespace Audio
bool fillAudioStreamWav(WAV *wav, mm_word length, u16 *dest, mm_stream_formats)
{
if (wav == nullptr)
{
return true;
}
if (!wav->_active)
{
return true;
}
if (!wav->_loaded)
{
return true;
}
FILE *stream = wav->_stream;
// TODO: convert bit depth
if (wav->_bitsPerSample != 16)
{
return true;
}
// TODO: Document how sample rate change works
u32 dstI = 0;
// TODO: s32 addition; to clip
Expand Down Expand Up @@ -249,32 +263,24 @@ namespace Audio

void LoadSFXs()
{
// Prepare sound engine
// soundEnable();
// NF_InitRawSoundBuffers();
for (int sfx = 0; sfx < SFX_COUNT; sfx++)
{
auto *sfxWav = new Audio::WAV;
sfxWav->loadWAV(SFXS[sfx].path);
sfxWav->setLoops(0);


// Load SFX
// for (int i = 0; i < SFX_COUNT; i++)
// {
// NF_LoadRawSound(SFXS[i], i, 22050, 0);
// }
soundEffects[sfx] = sfxWav;
}
}

void PlaySFX(SfxId sfx)
{
// NF_PlayRawSound(
// sfx,
// 127,
// 64,
// false,
// 0
// );
soundEffects[sfx]->play();
}

void StopSFX()
{
soundKill(0);

}

void PlayBGM(BgmId bgm, bool loop)
Expand Down
97 changes: 38 additions & 59 deletions source/sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#include <nf_lib.h>
#include <maxmod9.h>

struct WavFile
{
const char* name;
const char* path;
};

enum BgmId
{
BGM_TITLE_INTRO,
Expand All @@ -24,48 +30,6 @@ enum BgmId
BGM_FINAL_COOK
};

struct Bgm
{
const char* name;
const char* path;
};

const int BGM_COUNT = 8;
const Bgm BGMS[BGM_COUNT] =
{
{
"Breaking Bad (Title)",
"bgm/title_hook"
},
{
"Breaking Bad (Menu)",
"bgm/title_loop"
},
{
"Baby Blue (Game Over)",
"bgm/baby_blue"
},
{
"The Cousins (Superlab)",
"bgm/the_cousins_1"
},
{
"Clear Waters (Tutorial)",
"bgm/clear_waters_1"
},
{
"Rodrigo y Gabriela (Clear)",
"bgm/rodrigo_y_gabriela"
},
{
"Saul's Theme",
"bgm/sauls_theme"
},
{
"The Final Cook",
"bgm/the_final_cook"
}
};

enum SfxId
{
Expand All @@ -83,22 +47,35 @@ enum SfxId
SFX_MINERALS
};

// const int SFX_COUNT = 12;
// const char* SFXS[SFX_COUNT] =
// {
// {"sfx/menu_drum"},
// {"sfx/menu_select"},
// {"sfx/dialogue_bleep"},
// {"sfx/success_bell"},
// {"sfx/valve_turn"},
// {"sfx/goodbye_walter"},
// {"sfx/pestle"},
// {"sfx/crank"},
// {"sfx/click"},
// {"sfx/vaccum"},
// {"sfx/acceptable"},
// {"sfx/minerals"}
// };
const int BGM_COUNT = 8;
const WavFile BGMS[BGM_COUNT] =
{
{"Breaking Bad (Title)", "bgm/title_hook"},
{"Breaking Bad (Menu)", "bgm/title_loop"},
{"Baby Blue (Game Over)", "bgm/baby_blue"},
{"The Cousins (Superlab)", "bgm/the_cousins_1"},
{"Clear Waters (Tutorial)", "bgm/clear_waters_1"},
{"Rodrigo y Gabriela (Clear)", "bgm/rodrigo_y_gabriela"},
{"Saul's Theme", "bgm/sauls_theme"},
{"The Final Cook", "bgm/the_final_cook"}
};

const int SFX_COUNT = 12;
const WavFile SFXS[SFX_COUNT] =
{
{"Drum", "sfx/menu_drum"},
{"Select", "sfx/menu_select"},
{"Bleep", "sfx/dialogue_bleep"},
{"Bell", "sfx/success_bell"},
{"Turn", "sfx/valve_turn"},
{"Waltuh", "sfx/goodbye_walter"},
{"Pestle", "sfx/pestle"},
{"Crank", "sfx/crank"},
{"Click", "sfx/click"},
{"Vaccum", "sfx/vaccum"},
{"Acceptable", "sfx/acceptable"},
{"Minerals", "sfx/minerals"}
};

namespace Audio {
// We do not read a sample at a time, that would take too long. We load in chunks.
Expand Down Expand Up @@ -148,7 +125,9 @@ namespace Audio {
friend bool fillAudioStreamWav(WAV*, mm_word, u16*, mm_stream_formats);
};

void initAudioStream();
static WAV* soundEffects[SFX_COUNT];

void SetupAudio();
mm_word fillAudioStream(mm_word length, mm_addr dest, mm_stream_formats format);
bool fillAudioStreamWav(WAV* wav, mm_word length, u16* dest, mm_stream_formats format);

Expand Down

0 comments on commit 90870a9

Please sign in to comment.