Skip to content

Commit d64649a

Browse files
authored
Merge pull request #440 from thekhalifa/feat-examples-portaudio
examples: Add portaudio sine wave example
2 parents 6dedb96 + 178c3de commit d64649a

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

examples/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ NETWORK := $(subst .bas,,$(wildcard network/*.bas network/*/*.bas \
4646
network/*/*/*.bas))
4747
#OPTIMIZE := $(subst .bas,,$(wildcard OptimizePureAbstractTypes/*.bas))
4848
#REGEX := $(subst .bas,,$(wildcard regex/*/*.bas))
49-
SOUND := $(subst .bas,,$(wildcard sound/*/*.bas))
49+
SOUND := $(subst .bas,,$(wildcard sound/*.bas)) \
50+
$(subst .bas,,$(wildcard sound/*/*.bas))
5051
THREADS := $(subst .bas,,$(wildcard threads/*.bas)) \
5152
threads/timer-lib/libtimer$(DLLEXT) threads/timer-lib/test
5253
UNICODE := $(subst .bas,,$(wildcard unicode/*.bas))

examples/sound/portaudio-sine.bas

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
''
2+
'' PortAudio Sine wave example
3+
''
4+
'' Requires libportaudio2
5+
6+
#include "portaudio.bi"
7+
8+
Const Pi as Double = 3.14159265358979323846
9+
Const SampleRate as Integer = 44100
10+
Const SineSamples as Integer = SampleRate / 10
11+
12+
Type PlayPayload
13+
frequency As Single
14+
End Type
15+
16+
Function PlayCallback cdecl (InputBuff as Const Any Ptr, OutputBuff as Any Ptr, _
17+
FrameCount as CUlong, TimeInfo as Const PaStreamCallbackTimeInfo Ptr, _
18+
StatusFlags as PaStreamCallbackFlags, UserData as Any ptr) as Long
19+
20+
Dim as Long Ptr outBuff = OutputBuff
21+
Dim as PlayPayload Ptr payload = UserData
22+
Dim as Single frequency = payload->frequency
23+
For i as Integer = 0 To FrameCount - 1
24+
outBuff[i] = CLng(&h7FFFFFFF * 0.5 * Sin(2 * Pi * frequency * i / SampleRate))
25+
Next
26+
Function = paContinue
27+
End Function
28+
29+
Sub Play(Frequency as Single, Duration as Single)
30+
Dim as PaError pe
31+
Dim as PaStream Ptr stream
32+
33+
Dim as PlayPayload payload
34+
payload.frequency = Frequency
35+
36+
pe = Pa_OpenDefaultStream(@stream, 0, 1, paInt32, SampleRate, SineSamples, @PlayCallback, @payload)
37+
If pe <> paNoError Then
38+
Print "Error: "; *Pa_GetErrorText(pe)
39+
End 1
40+
End If
41+
42+
Print "Playing "; Frequency; "Hz for "; Duration; "ms"
43+
pe = Pa_StartStream(stream)
44+
If pe <> paNoError Then
45+
Print "Error: "; *Pa_GetErrorText(pe)
46+
End 1
47+
End If
48+
49+
'Sleep 2
50+
Pa_Sleep(Duration)
51+
52+
pe = Pa_StopStream(stream)
53+
If pe <> paNoError Then
54+
Print "Error: "; *Pa_GetErrorText(pe)
55+
End 1
56+
End If
57+
58+
pe = Pa_CloseStream(stream)
59+
If pe <> paNoError Then
60+
Print "Error: "; *Pa_GetErrorText(pe)
61+
End 1
62+
End If
63+
End Sub
64+
65+
Dim as PaError pe
66+
Print "Initialize PortAudio ("; *Pa_GetVersionText(); ")"
67+
68+
pe = Pa_Initialize()
69+
If pe <> paNoError Then
70+
Print "Error: "; *Pa_GetErrorText(pe)
71+
End 1
72+
End If
73+
74+
Dim defdevice as Integer = Pa_GetDefaultOutputDevice()
75+
If defdevice = paNoDevice Then
76+
Print "Error: no default output device"
77+
End 1
78+
End If
79+
80+
Dim devinfo as const PaDeviceInfo Ptr = Pa_GetDeviceInfo(defdevice)
81+
Dim apiinfo as const PaHostApiInfo Ptr = Pa_GetHostApiInfo(Pa_GetDefaultHostApi())
82+
Print "Using "; *apiinfo->name; " api on default output device: "; *devinfo->name
83+
84+
'' Play from A4 (440Hz) for 440ms until A3 (220Hz) for 220ms
85+
For i as Integer = 440 to 220 Step -110
86+
Play(i, i)
87+
Next
88+
89+
Print "Shutdown PortAudio"
90+
Pa_Terminate()
91+
92+
End 0

0 commit comments

Comments
 (0)