# HG changeset patch # User Sam # Date 1687615444 -25200 # Node ID 5df644a3b6b8a8415ec0002ec52e3136c2c4cd75 # Parent eb5ae1f1bc8782d1a9484b2086dcc0cd87de105b add: support for loading mono-ogg, change default sample rate to 44100 (audacity default) diff -r eb5ae1f1bc87 -r 5df644a3b6b8 src/semicongine/core/audiotypes.nim --- a/src/semicongine/core/audiotypes.nim Tue Jun 20 00:30:58 2023 +0700 +++ b/src/semicongine/core/audiotypes.nim Sat Jun 24 21:04:04 2023 +0700 @@ -3,9 +3,9 @@ # in order to generate sound files that are directly usable with the engine, # convert an audio file to a raw PCM signed 16 bit little endian file with 2 channels and 48kHz: # -# ffmpeg -i -f s16le -ac 2 -ar 48000 -acodec pcm_s16le +# ffmpeg -i -f s16le -ac 2 -ar 44100 -acodec pcm_s16le -const AUDIO_SAMPLE_RATE* = 48000 +const AUDIO_SAMPLE_RATE* = 44100 type Level* = 0'f .. 1'f diff -r eb5ae1f1bc87 -r 5df644a3b6b8 src/semicongine/resources/audio.nim --- a/src/semicongine/resources/audio.nim Tue Jun 20 00:30:58 2023 +0700 +++ b/src/semicongine/resources/audio.nim Sat Jun 24 21:04:04 2023 +0700 @@ -79,10 +79,17 @@ raise newException(Exception, &"Unable to read ogg/vorbis sound file, error code: {nSamples}") if sampleRate != AUDIO_SAMPLE_RATE: raise newException(Exception, &"Only support sample rate of {AUDIO_SAMPLE_RATE} Hz but got {sampleRate} Hz, please resample (e.g. ffmpeg -i -acodec libvorbis -ar {AUDIO_SAMPLE_RATE} )") - if channels != 2: - raise newException(Exception, &"Currently only support 2 channels, but ogg/ vorbis file had {channels}") result = new Sound result[].setLen(int(nSamples)) - copyMem(addr result[][0], output, nSamples * sizeof(Sample)) - free(output) + if channels == 2: + copyMem(addr result[][0], output, nSamples * sizeof(Sample)) + free(output) + elif channels == 1: + for i in 0 ..< nSamples: + let value = cast[ptr UncheckedArray[int16]](output)[i] + result[].add [value, value] + free(output) + else: + free(output) + raise newException(Exception, "Only support mono and stereo audio at the moment (1 or 2 channels), but found " & $channels)