I worked on a purebasic "port" a long time ago, here it is it should be out of date :
If you want to know how to save to a wav file, here is the "recording" example in purebasic :
Code: Select all
; ===============================================================================================
; Record example
; Copyright (c), Firelight Technologies Pty, Ltd 2004-2005.
;
; This example shows how to record a sound, then write it To a wav file.
; It then shows how to play a sound While it is being recorded to. Because it is recording, the
; sound playback has to be delayed a little bit so that the playback doesn't play part of the
; buffer that is still being written To.
; ===============================================================================================
XIncludeFile "../../api/purebasic/fmod_errors.pbi"
Global System.l, Sound.l, Channel.l
Global result.l
Structure RIFF_HEADER
RIFF.l
riffBlockSize.l
riffBlockType.l
EndStructure
Structure WAVE_HEADER
dataBlockType.l
dataBlockSize.l
EndStructure
Structure WAVE_FORMAT
wfBlockType.l
wfBlockSize.l
wFormatTag.w
nChannels.w
nSamplesPerSec.l
nAvgBytesPerSec.l
nBlockAlign.w
wBitsPerSample.w
EndStructure
#OUTPUT_FILE = 0
Procedure ERRCHECK(the_result.l)
If the_result <> #FMOD_OK
PrintN("FMOD error ! " + Str(the_result) + FMOD_ErrorString(the_result) )
Delay(2000)
End
EndIf
EndProcedure
Procedure SaveToWav(ptrSound.l)
Protected ptr1.l, ptr2.l, len1.l, len2.l
Protected lenbytes.l, channels.l, bits.l, rate.f
Protected m_structRIFF.RIFF_HEADER
Protected m_structWAVEHdr.WAVE_HEADER
Protected m_structWAVEFmt.WAVE_FORMAT
If ptrSound = 0
ProcedureReturn
EndIf
FMOD_Sound_GetFormat_ (ptrSound, 0, 0, @channels, @bits)
FMOD_Sound_GetDefaults_(ptrSound, @rate, 0, 0, 0)
FMOD_Sound_GetLength_(ptrSound, @lenbytes, #FMOD_TIMEUNIT_PCMBYTES)
; Prepare the Wave Header
m_structWAVEFmt\wFormatTag = 1
m_structWAVEFmt\nChannels = channels
m_structWAVEFmt\nSamplesPerSec = rate
m_structWAVEFmt\wBitsPerSample = bits
m_structWAVEFmt\nBlockAlign = m_structWAVEFmt\nChannels * m_structWAVEFmt\wBitsPerSample / 8
m_structWAVEFmt\nAvgBytesPerSec = m_structWAVEFmt\nSamplesPerSec * m_structWAVEFmt\nBlockAlign
m_structWAVEFmt\wfBlockType = $20746D66 ; 'f' 'm' 't'
m_structWAVEFmt\wfBlockSize = 16 ; SizeOf(FmtChunk) - SizeOf(RiffChunk)
m_structRIFF\RIFF = $46464952 ; 'R' 'I' 'F' 'F'
m_structRIFF\riffBlockSize = lenbytes + 44 - 12
m_structRIFF\riffBlockType = $45564157 ; 'W' 'A' 'V' 'E'
m_structWAVEHdr\dataBlockType = $61746164
m_structWAVEHdr\dataBlockSize = lenbytes
m_hNewFile = CreateFile(#OUTPUT_FILE, "record.wav")
If m_hNewFile = 0
MessageRequester("Error", "An error occured while creating record.wav file", 0)
ProcedureReturn
EndIf
; Write out the WAV Header
WriteData(m_structRIFF, SizeOf(RIFF_HEADER))
WriteData(m_structWAVEFmt, SizeOf(WAVE_FORMAT))
WriteData(m_structWAVEHdr, SizeOf(WAVE_HEADER))
; Lock the sound to get access to the raw data.
FMOD_Sound_Lock_(ptrSound, 0, lenbytes, @ptr1, @ptr2, @len1, @len2)
;Write it to disk.
WriteData(ptr1, len1)
; Unlock the sound to allow FMOD to use it again.
FMOD_Sound_Unlock_(ptrSound, ptr1, ptr2, len1, len2)
; And close the output file
CloseFile(#OUTPUT_FILE)
EndProcedure
OpenConsole()
; Create a System object And initialize.
result = FMOD_System_Create_(@System)
ERRCHECK(result)
result = FMOD_System_GetVersion_(System, @version)
ERRCHECK(result)
If version < #FMOD_VERSION
MessageRequester("Error", "Error ! You are using an old version of FMOD " + Hex(version >> 16) + "." + Hex(version & $FF) + ". This program requires " + Hex(#FMOD_VERSION >> 16) + "." + Hex(#FMOD_VERSION & $FF) )
EndIf
; System initialization
PrintN("---------------------------------------------------------");
PrintN("Select OUTPUT type")
PrintN("---------------------------------------------------------")
PrintN("1 : DirectSound")
PrintN("2 : Windows Multimedia WaveOut")
PrintN("3 : ASIO")
PrintN("---------------------------------------------------------")
PrintN("Press a corresponding number or ESC to quit")
Repeat
ch.s = Inkey()
If Asc(ch) = 27
CloseConsole()
End
EndIf
value = Val(Left(ch, 1))
Delay(10)
Until (value >= 1 And value <= 3)
Select value
Case 1 : result = FMOD_System_SetOutput_(System, #FMOD_OUTPUTTYPE_DSOUND)
Case 2 : result = FMOD_System_SetOutput_(System, #FMOD_OUTPUTTYPE_WINMM)
Case 3 : result = FMOD_System_SetOutput_(System, #FMOD_OUTPUTTYPE_ASIO)
EndSelect
ERRCHECK(result)
; Enumerate playback devices
result = FMOD_System_GetNumDrivers_(System, @numdrivers)
ERRCHECK(result)
PrintN("---------------------------------------------------------")
PrintN("Choose a PLAYBACK driver")
PrintN("---------------------------------------------------------")
For count=0 To numdrivers - 1
name.s = Space(256)
result = FMOD_System_GetDriverName_(System, count, @name, 256)
ERRCHECK(result)
PrintN(Str(count+1) + " : " + name)
Next count
PrintN("---------------------------------------------------------")
PrintN("Press a corresponding number or ESC to quit\n")
Repeat
ch.s = Inkey()
value = Val(Left(ch, 1))
Delay(10)
Until value > 0 And value <= numdrivers
result = FMOD_System_SetDriver_(system,value-1)
ERRCHECK(result)
; Enumerate record devices
result = FMOD_System_GetRecordNumDrivers_(System, @numdrivers)
ERRCHECK(result)
PrintN("---------------------------------------------------------")
PrintN("Choose a RECORD driver")
PrintN("---------------------------------------------------------")
For count=0 To numdrivers - 1
name.s = Space(256)
result = FMOD_System_GetRecordDriverName_(System, count, @name, 256)
ERRCHECK(result)
PrintN(Str(count+1) + " : " + name)
Next count
PrintN("---------------------------------------------------------")
PrintN("Press a corresponding number or ESC to quit")
Repeat
ch.s = Inkey()
value = Val(Left(ch, 1))
If value = 27
CloseConsole()
End
EndIf
Delay(10)
Until value > 0 And value <= numdrivers
result = FMOD_System_SetRecordDriver_(System, value)
ERRCHECK(result)
result = FMOD_System_Init_(System, 32, #FMOD_INIT_NORMAL, 0)
ERRCHECK(result)
exinfo.FMOD_CREATESOUNDEXINFO
RtlZeroMemory_(exinfo, SizeOf(FMOD_CREATESOUNDEXINFO))
exinfo\cbsize = SizeOf(FMOD_CREATESOUNDEXINFO)
exinfo\numchannels = 1
exinfo\format = #FMOD_SOUND_FORMAT_PCM16
exinfo\defaultfrequency = 44100
exinfo\length = exinfo\defaultfrequency * 2 * exinfo\numchannels * 5
result = FMOD_System_CreateSound_(System, 0, #FMOD_2D | #FMOD_SOFTWARE | #FMOD_OPENUSER, @exinfo, @Sound)
ERRCHECK(result)
PrintN("===================================================================")
PrintN("Recording example. Copyright (c) Firelight Technologies 2004-2005.")
PrintN("===================================================================")
PrintN("")
PrintN("Press 'r' to record a 5 second segment of audio and write it to a wav file.")
PrintN("Press 'p' to play the 5 second segment of audio.")
PrintN("Press 'l' to turn looping on/off.")
PrintN("Press 's' to stop recording and playback.")
PrintN("Press 'w' to save the 5 second segment to a wav file.")
PrintN("Press 'Esc' to quit")
PrintN("")
Repeat
key.s = LCase(Left(Inkey(),1))
looping = 0
recording = 0
playing = 0
recordpos = 0
playpos = 0
Select key
Case "r"
result = FMOD_System_RecordStart_(System, Sound, looping)
ERRCHECK(result)
Case "p"
If (looping)
FMOD_Sound_SetMode_(Sound, #FMOD_LOOP_NORMAL)
Else
FMOD_Sound_SetMode_(Sound, #FMOD_LOOP_OFF)
EndIf
ERRCHECK(result)
result = FMOD_System_PlaySound_(System, #FMOD_CHANNEL_REUSE, Sound, 0, @Channel)
ERRCHECK(result)
Case "l"
looping ! 1
Case "s"
result = FMOD_System_RecordStop_(System)
If Channel
FMOD_Channel_Stop_(Channel)
channel = 0
EndIf
Case "w"
PrintN("Writing to record.wav ... ")
SaveToWav(Sound)
Delay(500)
EndSelect
FMOD_Sound_GetLength_(Sound, @length, #FMOD_TIMEUNIT_PCM)
ERRCHECK(result)
FMOD_System_IsRecording_(System, @recording)
ERRCHECK(result)
FMOD_System_GetRecordPosition_(System, @recordpos)
ERRCHECK(result)
If (Channel)
FMOD_Channel_IsPlaying_(Channel, @playing)
ERRCHECK(result)
FMOD_Channel_GetPosition_(Channel, @playpos, #FMOD_TIMEUNIT_PCM)
ERRCHECK(result)
EndIf
playback_status.s = Space(20)
playback_status = ""
If recording
If playing
playback_status = "Recording / Playing"
Else
playback_status = "Recording"
EndIf
ElseIf playing
playback_status = "Playing"
Else
playback_status = "Idle"
EndIf
loop.s = Space(3)
loop = " "
If looping
loop = "On"
Else
loop = "Off"
EndIf
ConsoleLocate(0, 23)
PrintN("State : " + playback_status + " Record pos = " + Str(recordpos) + " : Play pos = " + Str(playpos) + " : Loop = " + loop)
FMOD_System_Update_(System)
Delay(10)
Until Asc(key) = 27
PrintN("")
; Shut down
result = FMOD_Sound_Release_(sound)
ERRCHECK(result)
result = FMOD_System_Release_(system)
ERRCHECK(result)
CloseConsole()
End