exp/i2s: loop recording 30 mins to file

This commit is contained in:
Trek Hopton 2024-02-14 16:08:49 +10:30
parent 89e530644d
commit 52b2bde9b4
1 changed files with 34 additions and 46 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"log"
"os"
"time"
@ -10,29 +9,14 @@ import (
yalsa "github.com/yobert/alsa"
)
const i2sDevName =
const i2sDevName = "simple-card_codec_link snd-soc-dummy-dai-0"
func main() {
// Set capture type.
var i string
var i2s bool
fmt.Printf("Force I2S recording? (y/n): ")
fmt.Scan(&i)
if i == "y" {
i2s = true
} else {
i2s = false
}
var i2s bool = true
// Set recording length.
var duration time.Duration
var err error
fmt.Printf("Recording time: ")
fmt.Scan(&i)
duration, err = time.ParseDuration(i)
if err != nil {
duration = 5 * time.Second
}
var duration time.Duration = 30 * time.Minute
// Find devices.
cards, err := yalsa.OpenCards()
@ -119,6 +103,8 @@ func main() {
return
}
// Record audio.
for {
buf := dev.NewBufferDuration(duration)
log.Println("recording for:", duration)
err = dev.Read(buf.Data)
@ -128,7 +114,8 @@ func main() {
}
log.Println("Recording stopped.")
file, err := os.Create("audio.wav")
fileName := "audio-" + time.Now().Format("2006-01-02_15-04-05") + ".wav"
file, err := os.Create(fileName)
if err != nil {
log.Println("failed to create file:", err)
return
@ -150,5 +137,6 @@ func main() {
}
log.Println("Audio saved to audio.wav")
}
}