Skip to content

Commit bd40855

Browse files
committed
examples: Add portaudio sine wave example
PortAudio already has a FB header, this adds an example usage to play a sine wave from a buffer.
1 parent 6dedb96 commit bd40855

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 (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+
'' Play from A4 (440Hz) for 44ms until A2 (220Hz) for 22ms
75+
For i as Integer = 440 to 220 Step -10
76+
Play(i, i / 10)
77+
Next
78+
79+
Print "Shutdown PortAudio"
80+
Pa_Terminate()
81+
82+
End 0

0 commit comments

Comments
 (0)