Skip to content

Commit aff3857

Browse files
committed
Fix example
1 parent 1e5a6d1 commit aff3857

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

examples/micgrab/micgrab.go

+25-22
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@ import (
1111
)
1212

1313
func main() {
14-
device := mal.NewDevice()
15-
16-
numChannels := 2
14+
numChannels := 1
1715
sampleRate := 48000
1816

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+
})
3220
if err != nil {
3321
fmt.Println(err)
3422
os.Exit(1)
3523
}
24+
defer func() {
25+
_ = ctx.Uninit()
26+
ctx.Free()
27+
}()
3628

37-
defer device.ContextUninit()
29+
deviceConfig := malgo.DefaultDeviceConfig(malgo.Capture)
30+
deviceConfig.Capture.Format = malgo.FormatS16
31+
deviceConfig.Capture.Channels = uint32(numChannels)
3832

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+
}
4043

4144
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)
4349
if err != nil {
4450
fmt.Println(err)
4551
os.Exit(1)
@@ -89,8 +95,5 @@ func main() {
8995
os.Exit(1)
9096
}
9197

92-
fmt.Println("Press Enter to quit...")
93-
fmt.Scanln()
94-
9598
os.Exit(0)
9699
}

0 commit comments

Comments
 (0)