December 03, 2020, 08:59:08 PM

Author Topic: [bmx] WAV Saver by Ghost Dancer [ 1+ years ago ]  (Read 689 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] WAV Saver by Ghost Dancer [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : WAV Saver
Author : Ghost Dancer
Posted : 1+ years ago

Description : Thought I'd put this code under the file utilities - it's a direct copy of the code I put in the audio section but thought it best to post here as well :-)

Code :
Code: BlitzMax
  1. Strict
  2.  
  3. Local oggFile$ = RequestFile$("Load Ogg...", "Ogg Files:ogg")
  4. Local wavFile$ = RequestFile$("Save as...", "Wav files:wav", True)
  5.  
  6. If oggToWav(oggFile$, wavFile$)
  7.         Print "Wav file created."
  8. Else
  9.         Print "Error, could not create wav file."
  10. End If
  11.  
  12. End
  13.  
  14. Function oggToWav(oggFile$, wavFile$)
  15.         Local sndSample:TAudioSample, sampleSize, sndBank:TBank, fileStream:TStream
  16.         Local channels, bitRate, blockAlign, fileSaved
  17.        
  18.         'load the sound
  19.         sndSample = LoadAudioSample(oggFile$)
  20.        
  21.         'determine mono/stero
  22.         If sndSample.format = SF_MONO8 Or sndSample.format = SF_MONO16LE Or sndSample.format = SF_MONO16BE Then
  23.                 channels = 1
  24.         Else
  25.                 channels = 2
  26.         End If
  27.                
  28.         'determine bitrate & calculate size
  29.         If sndSample.format = SF_MONO8 Or sndSample.format = SF_STEREO8 Then
  30.                 bitRate = 8
  31.                 sampleSize = sndSample.length * channels
  32.         Else
  33.                 bitRate = 16
  34.                 sampleSize = sndSample.length * channels * 2
  35.         End If
  36.        
  37.         blockAlign = channels * bitRate / 8
  38.        
  39.        
  40.         'create a bank from the loaded sound
  41.         sndBank = CreateStaticBank(sndSample.samples, sampleSize)
  42.        
  43.         'create a stream to save data
  44.         fileStream = WriteStream(wavFile$)
  45.        
  46.         If fileStream Then
  47.                 'write wav header info
  48.                 fileStream.writeString("RIFF")                                          '"RIFF" file description header (4 bytes)
  49.                 fileStream.writeInt(sampleSize + 40)                            'file size - 8 (4 bytes)
  50.                 fileStream.writeString("WAVE")                                          '"WAVE" description header (4 bytes)
  51.                 fileStream.writeString("fmt ")                                          '"fmt " description header (4 bytes)
  52.                 fileStream.writeInt(16)                                                         'size of WAVE section chunk (4 bytes)
  53.                 fileStream.writeShort(1)                                                        'WAVE type format (2 bytes)
  54.                 fileStream.writeShort(channels)                                         'mono/stereo (2 bytes)
  55.                 fileStream.writeInt(sndSample.hertz)                            'sample rate (4 bytes)
  56.                 fileStream.writeInt(sndSample.hertz * blockAlign)       'avg bytes/sec (4 bytes)
  57.                 fileStream.writeShort(blockAlign)                                       'Block alignment (2 bytes)
  58.                 fileStream.writeShort(bitRate)                                          'Bits/sample (2 bytes)
  59.                 fileStream.writeString("data")                                          '"data" description header (4 bytes)
  60.                 fileStream.writeInt(sampleSize)                                         'size of data chunk (4 bytes)
  61.                
  62.                 'write wav sound data
  63.                 sndBank.Write(fileStream, 0, sampleSize)
  64.                
  65.                 'close the stream
  66.                 CloseStream fileStream
  67.                
  68.                 fileSaved = True
  69.         Else
  70.                 fileSaved = False
  71.         End If
  72.        
  73.         'free up mem
  74.         fileStream = Null
  75.         sndBank = Null
  76.         sndSample = Null
  77.        
  78.         Return fileSaved
  79. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal