Created
May 15, 2022 10:28
-
-
Save husain-zaidi/c8d4fac481a3b860243566835afaeca0 to your computer and use it in GitHub Desktop.
Records desktop/system audio using malgo, loopback WASAPI backend. Saves raw audio. golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/gen2brain/malgo" | |
) | |
func main() { | |
var backends []malgo.Backend = []malgo.Backend{malgo.BackendWasapi} | |
f, err := os.Create("audio") | |
ctx, err := malgo.InitContext(backends, malgo.ContextConfig{}, func(message string) { | |
fmt.Printf("LOG <%v>\n", message) | |
}) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer func() { | |
_ = ctx.Uninit() | |
ctx.Free() | |
}() | |
deviceConfig := malgo.DefaultDeviceConfig(malgo.Loopback) | |
deviceConfig.Capture.Format = malgo.FormatF32 | |
deviceConfig.Capture.Channels = 2 | |
deviceConfig.SampleRate = 44100 | |
onRecvFrames := func(pSample2, pSample []byte, framecount uint32) { | |
_, err := f.Write(pSample) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} | |
fmt.Println("Recording...") | |
captureCallbacks := malgo.DeviceCallbacks{ | |
Data: onRecvFrames, | |
} | |
device, err := malgo.InitDevice(ctx.Context, deviceConfig, captureCallbacks) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
err = device.Start() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
fmt.Println("Press Enter to stop recording...") | |
fmt.Scanln() | |
device.Uninit() | |
f.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment