Hi,
I’m trying to capture sound from the mic and save it to a wav file and also play it back via the speaker. Not sure how to do that, and was hoping to get some help/advise.
I’ve attached a mic and speaker to the ports and did manage to play back another wav file via the speaker, so at least I know the playback works in general. Just not sure how to capture sound from the mic and play that back.
See the code below for what I started with and although it read 1024 bytes, I don’t hear anything when I try to play it back. I did also try putting the read and write in a loop but no luck.
What am I missing? Any advice?
func echoTest() {
err := audio.Start()
check(err)
// Tried 1 channel also
err = audio.Init(2, 16000, audio.FormatS16LE)
check(err)
// Maybe put this in a loop. Why loop based on what?
// Read audio from mic
bytes_read, err := audio.Read()
check(err)
count_bytes_read := len(bytes_read)
log.Info.Println("Read bytes: ", count_bytes_read)
// Save read audio to file
saveAudioToFile("test.wav", bytes_read)
// Playback audio from mic to speaker
_, err = audio.Write(bytes_read)
check(err)
}
func check(e error) {
if e != nil {
log.Fatal.Println("Fatal error:", e)
panic(e)
}
}
Ideally I’d like to know how to capture n seconds of audio and save it to a wav file.
Thanks!