From 52b2bde9b4f6de9c0a554318846ec028ccc3c616 Mon Sep 17 00:00:00 2001 From: Trek Hopton Date: Wed, 14 Feb 2024 16:08:49 +1030 Subject: [PATCH] exp/i2s: loop recording 30 mins to file --- exp/i2s/main.go | 80 +++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/exp/i2s/main.go b/exp/i2s/main.go index 80bcc7c6..368ee1f4 100644 --- a/exp/i2s/main.go +++ b/exp/i2s/main.go @@ -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,36 +103,40 @@ func main() { return } - buf := dev.NewBufferDuration(duration) - log.Println("recording for:", duration) - err = dev.Read(buf.Data) - if err != nil { - log.Println("Failed to read:", err) - return - } - log.Println("Recording stopped.") + // Record audio. + for { + buf := dev.NewBufferDuration(duration) + log.Println("recording for:", duration) + err = dev.Read(buf.Data) + if err != nil { + log.Println("Failed to read:", err) + return + } + log.Println("Recording stopped.") - file, err := os.Create("audio.wav") - if err != nil { - log.Println("failed to create file:", err) - return - } - defer file.Close() + 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 + } + defer file.Close() - var audio wav.WAV - audio.Metadata = wav.Metadata{Channels: 2, AudioFormat: wav.PCMFormat, SampleRate: 44100, BitDepth: 16} - _, err = audio.Write(buf.Data) - if err != nil { - log.Println("Failed to encode wav:", err) - return - } + var audio wav.WAV + audio.Metadata = wav.Metadata{Channels: 2, AudioFormat: wav.PCMFormat, SampleRate: 44100, BitDepth: 16} + _, err = audio.Write(buf.Data) + if err != nil { + log.Println("Failed to encode wav:", err) + return + } - _, err = file.Write(audio.Audio) - if err != nil { - log.Println("failed to write audio:", err) - return - } + _, err = file.Write(audio.Audio) + if err != nil { + log.Println("failed to write audio:", err) + return + } - log.Println("Audio saved to audio.wav") + log.Println("Audio saved to audio.wav") + } }