[bmx] Ogg Converter/Wav Saver by Ghost Dancer [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Ogg Converter/Wav Saver
Author : Ghost Dancer
Posted : 1+ years ago

Description : Since loading Oggs can take a long time (10 secs or more for long tracks), I needed a better solution for handling big music files. Wavs are fine but very large file size making downloads very big.

The solution - distribute with Oggs (minimal file size) and convert them to Wavs the first time the game/app is run, then load in the Wavs for faster load times. This code has been designed for this purpose although it can be easily modified for other situations where you might want to save sound data.

The function loads the specified Ogg file and saves it to the specified Wav file.


Code :
Code (blitzmax) Select
Strict

Local oggFile$ = RequestFile$("Load Ogg...", "Ogg Files:ogg")
Local wavFile$ = RequestFile$("Save as...", "Wav files:wav", True)

If oggToWav(oggFile$, wavFile$)
Print "Wav file created."
Else
Print "Error, could not create wav file."
End If

End

Function oggToWav(oggFile$, wavFile$)
Local sndSample:TAudioSample, sampleSize, sndBank:TBank, fileStream:TStream
Local channels, bitRate, blockAlign, fileSaved

'load the sound
sndSample = LoadAudioSample(oggFile$)

'determine mono/stero
If sndSample.format = SF_MONO8 Or sndSample.format = SF_MONO16LE Or sndSample.format = SF_MONO16BE Then
channels = 1
Else
channels = 2
End If

'determine bitrate & calculate size
If sndSample.format = SF_MONO8 Or sndSample.format = SF_STEREO8 Then
bitRate = 8
sampleSize = sndSample.length * channels
Else
bitRate = 16
sampleSize = sndSample.length * channels * 2
End If

blockAlign = channels * bitRate / 8


'create a bank from the loaded sound
sndBank = CreateStaticBank(sndSample.samples, sampleSize)

'create a stream to save data
fileStream = WriteStream(wavFile$)

If fileStream Then
'write wav header info
fileStream.writeString("RIFF") '"RIFF" file description header (4 bytes)
fileStream.writeInt(sampleSize + 40) 'file size - 8 (4 bytes)
fileStream.writeString("WAVE") '"WAVE" description header (4 bytes)
fileStream.writeString("fmt ") '"fmt " description header (4 bytes)
fileStream.writeInt(16) 'size of WAVE section chunk (4 bytes)
fileStream.writeShort(1) 'WAVE type format (2 bytes)
fileStream.writeShort(channels) 'mono/stereo (2 bytes)
fileStream.writeInt(sndSample.hertz) 'sample rate (4 bytes)
fileStream.writeInt(sndSample.hertz * blockAlign) 'avg bytes/sec (4 bytes)
fileStream.writeShort(blockAlign) 'Block alignment (2 bytes)
fileStream.writeShort(bitRate) 'Bits/sample (2 bytes)
fileStream.writeString("data") '"data" description header (4 bytes)
fileStream.writeInt(sampleSize) 'size of data chunk (4 bytes)

'write wav sound data
sndBank.Write(fileStream, 0, sampleSize)

'close the stream
CloseStream fileStream

fileSaved = True
Else
fileSaved = False
End If

'free up mem
fileStream = Null
sndBank = Null
sndSample = Null

Return fileSaved
End Function


Comments : none...

Hardcoal

Greate Work Thank you.
People don't compliment as they should 
Some of the things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]
My Google Drive https://drive.google.com/drive/folders/1vOgkOxA_YwdMaomM2-K4NQh0mpwgpkii?usp=drive_link