record with OpenAL

BlitzMax Forums/BlitzMax Programming/record with OpenAL

My test:
SuperStrict

Framework Vertex.OpenAL

Const SECONDS : Int = 5

Global Specifiers       : String[], ..
       Specifier        : String, ..
       DefaultSpecifier : String, ..
       Index            : Int, ..
       Input            : String, ..
       Recorder         : Byte Ptr, ..
       Samples          : Byte Ptr, ..
       NumSamples       : Int

Global AudioDevice      : Byte Ptr, ..
       Context          : Byte Ptr, ..
       Source           : Int, ..
       Buffer           : Int, ..
       State            : Int

Try

TOpenAL.Init()

' Recorder
Specifiers = EnumDevices(ALC_CAPTURE_DEVICE_SPECIFIER)
If Not Specifiers Then Throw("No recorders where found")
DefaultSpecifier = String.FromCString(..
                    alcGetString(Null, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER))

WriteStdout("Recorders~n")
For Index = 0 Until Specifiers.Length
	Specifier = Specifiers[Index]
	WriteStdout(" " + (Index + 1) + ") " + Specifier)
	If Specifier = DefaultSpecifier Then
		WriteStdout("(Default)~n")
	Else
		WriteStdout("~n")
	EndIf
Next

WriteStdout("Index: ") ; Input = ReadStdin()
If Input Then
	Index = Input.ToInt()
	If Index < 1 Or Index > Specifiers.Length Then ..
		Throw("Index out of range")
	Specifier = Specifiers[Index - 1]
Else
	Specifier = DefaultSpecifier
EndIf

Recorder = alcCaptureOpenDevice(Specifier, 44100, AL_FORMAT_MONO16, ..
                                SECONDS*44100*2)
If Not Recorder Then Throw("Unable to open recorder")

Samples = MemAlloc(SECONDS*44100*2)

' Audio Output
AudioDevice = alcOpenDevice(Null)
If Not AudioDevice Then Throw("Unable to open audio output device")

Context = alcCreateContext(AudioDevice, Null)
If Not Context Then Throw("Unable to create audio context")

If Not alcMakeContextCurrent(Context) Then ..
	Throw("unable to make context current")

alGenSources(1, Varptr(Source))
alGenBuffers(1, Varptr(Buffer))

' Start recording
alcCaptureStart(Recorder)
Repeat
	alcGetIntegerv(Recorder, ALC_CAPTURE_SAMPLES, 4, Varptr(NumSamples))
	WriteStdout(NumSamples + "/" + SECONDS*44100 + " Samples~n")
	Delay(100)
Until NumSamples >= SECONDS*44100
alcCaptureSamples(Recorder, Samples, NumSamples)

' Stop recording
alcCaptureStop(Recorder)

' Output
alBufferData(Buffer, AL_FORMAT_MONO16, Samples, NumSamples*2, 44100)
alSourcei(Source, AL_BUFFER, Buffer)

alSourcePlay(Source)
Repeat
	alGetSourcei(Source, AL_SOURCE_STATE, Varptr(State))
Until State <> AL_PLAYING

Catch Exception : Object
	WriteStdout("~nError:~n " + Exception.ToString())
End Try

alSourceStop(Source)
alDeleteSources(1, Varptr(Source))
alDeleteBuffers(1, Varptr(Buffer))
alcMakeContextCurrent(Null)
alcDestroyContext(Context)
alcCloseDevice(AudioDevice)
alcCaptureCloseDevice(Recorder)

WriteStdout("~nready~n")
End

Function EnumDevices:String[](Device:Int)
	Local Devices : Byte Ptr, ..
	      List    : String[]

	Devices = alcGetString(Null, Device)
	If Not Devices Then Return Null

	While Devices[0]
		List = List[..List.Length + 1]
		List[List.Length - 1] = String.FromCString(Devices)
		Devices :+ List[List.Length - 1].Length + 1
	Wend

	Return List
End Function


http://vertex.dreamfall.at/openal/openal103.zip
(Does only work on Windows and maybe Linux)