4 AudioMixer := Object clone do(
6 self sources
:= List clone
7 self mixed
:= Sequence
clone
10 newSlot("streamDestination", AudioDevice)
11 newSlot("processedSamples", 0)
12 newSlot("isRunning", false
)
14 addSource
:= method(s
, sources
append(s
))
15 removeSource
:= method(s
, sources
remove(s
))
17 process
:= method(sampleCount
,
18 byteCount
:= sampleCount
* 8
19 mixed
setSize(byteCount
)
21 sources
foreach(source
,
22 while(source outputBuffer size
< byteCount
,
23 //writeln("source outputBuffer size = ", source outputBuffer size
, " < ", byteCount
)
26 writeln("b ", source outputBuffer size
)
27 mixed
float32ArrayAdd(source outputBuffer
)
28 source outputBuffer
removeSlice(0, byteCount
)
29 writeln("a ", source outputBuffer size
)
31 //mixed
float32ArrayDivideBy(sources size
)
32 writeln("AudioMixer writing ", mixed size
, " bytes to ", streamDestination
type)
33 streamDestination
write(mixed
)
34 processedSamples
= processedSamples
+ sampleCount
38 writeln("AudioMixer start")
40 while(isRunning
, process(22050)) // 1/16th of a second
50 MP2Decoder
:= AVCodec
clone setCodecName("mp2")
52 AVCodec withDestination
:= method(dest
,
54 decoder
setStreamDestination(dest
)
55 decoder
@@startAudioDecoding
60 SampleRateConverter withDestination
:= method(dest
,
61 SampleRateConverter
clone setStreamDestination(dest
)
65 path1
:= "/Users/steve/Downloads/Hp\ lovecraft/HP\ Lovecraft\ -\ The\ Fungi\ From\ Yuggoth/HP\ Lovecraft\ -\ Fungi\ From\ Yuggoth.mp3"
66 path1
:= "/Users/steve/Music/Unnatural\ History\ III/Baby\ Food\ \(1994\).mp3"
67 //path2
:= Path
with(launchPath
, "sounds/max.mp3")
69 File streamTo
:= method(dest
,
70 setStreamDestination(dest
)
75 //file1
:= File with(path1
) streamTo(MP2Decoder
withDestination(SampleRateConverter
clone setStreamDestination(AudioDevice)))
76 file1
:= File with(path1
)
77 file1
@@streamTo(MP2Decoder
withDestination(SampleRateConverter
clone))
78 //file2
:= File with(path2
) @streamTo(MP2Decoder
withDestination(SampleRateConverter
clone))
81 writeln("AudioMixer clone")
82 mixer
:= AudioMixer clone
83 writeln("AudioMixer addSource ", file1 streamDestination
type)
84 mixer
addSource(file1 streamDestination streamDestination
)
85 //mixer
addSource(file2 streamDestination streamDestination
)
86 writeln("AudioMixer start")