@@ -11,35 +11,41 @@ import (
11
11
)
12
12
13
13
func main () {
14
- device := mal .NewDevice ()
15
-
16
- numChannels := 2
14
+ numChannels := 1
17
15
sampleRate := 48000
18
16
19
- var capturedSampleCount uint32
20
- pCapturedSamples := make ([]byte , 0 )
21
-
22
- onRecvFrames := func (framecount uint32 , pSamples []byte ) {
23
- sizeInBytes := device .SampleSizeInBytes (device .Format ())
24
- sampleCount := framecount * device .Channels () * sizeInBytes
25
-
26
- newCapturedSampleCount := capturedSampleCount + sampleCount
27
- pCapturedSamples = append (pCapturedSamples , pSamples ... )
28
- capturedSampleCount = newCapturedSampleCount
29
- }
30
-
31
- err := device .ContextInit (nil , mal.ContextConfig {})
17
+ ctx , err := malgo .InitContext (nil , malgo.ContextConfig {}, func (message string ) {
18
+ fmt .Printf ("LOG <%v>\n " , message )
19
+ })
32
20
if err != nil {
33
21
fmt .Println (err )
34
22
os .Exit (1 )
35
23
}
24
+ defer func () {
25
+ _ = ctx .Uninit ()
26
+ ctx .Free ()
27
+ }()
36
28
37
- defer device .ContextUninit ()
29
+ deviceConfig := malgo .DefaultDeviceConfig (malgo .Capture )
30
+ deviceConfig .Capture .Format = malgo .FormatS16
31
+ deviceConfig .Capture .Channels = uint32 (numChannels )
38
32
39
- config := device .ConfigInit (mal .FormatS16 , uint32 (numChannels ), uint32 (sampleRate ), onRecvFrames , nil )
33
+ var capturedSampleCount uint32
34
+ pCapturedSamples := make ([]byte , 0 )
35
+
36
+ sizeInBytes := uint32 (malgo .SampleSizeInBytes (deviceConfig .Capture .Format ))
37
+ onRecvFrames := func (pSample2 , pSample []byte , framecount uint32 ) {
38
+ sampleCount := framecount * deviceConfig .Capture .Channels * sizeInBytes
39
+ newCapturedSampleCount := capturedSampleCount + sampleCount
40
+ pCapturedSamples = append (pCapturedSamples , pSample ... )
41
+ capturedSampleCount = newCapturedSampleCount
42
+ }
40
43
41
44
fmt .Println ("Recording..." )
42
- err = device .Init (mal .Capture , nil , & config )
45
+ captureCallbacks := malgo.DeviceCallbacks {
46
+ Data : onRecvFrames ,
47
+ }
48
+ device , err := malgo .InitDevice (ctx .Context , deviceConfig , captureCallbacks )
43
49
if err != nil {
44
50
fmt .Println (err )
45
51
os .Exit (1 )
@@ -89,8 +95,5 @@ func main() {
89
95
os .Exit (1 )
90
96
}
91
97
92
- fmt .Println ("Press Enter to quit..." )
93
- fmt .Scanln ()
94
-
95
98
os .Exit (0 )
96
99
}
0 commit comments