-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdemo.go
107 lines (92 loc) · 2.69 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2021-2023 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is
// located in the "LICENSE" file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
pvrecorder "github.com/Picovoice/pvrecorder/binding/go"
"github.com/go-audio/wav"
)
func main() {
showAudioDevices := flag.Bool("show_audio_devices", false, "Display all the available input devices")
audioDeviceIndex := flag.Int("audio_device_index", -1, "Index of audio input device to use.")
outputWavPath := flag.String("output_wav_path", "", "Output path to save recorded a wav file.")
flag.Parse()
log.Printf("pvrecorder.go version: %s\n", pvrecorder.Version)
if *showAudioDevices {
log.Println("Printing devices...")
if devices, err := pvrecorder.GetAvailableDevices(); err != nil {
log.Fatalf("Error: %s.\n", err.Error())
} else {
for i, device := range devices {
log.Printf("index: %d, device name: %s\n", i, device)
}
}
return
}
recorder := pvrecorder.PvRecorder{
DeviceIndex: *audioDeviceIndex,
FrameLength: 512,
BufferedFramesCount: 10,
}
log.Println("Initializing...")
if err := recorder.Init(); err != nil {
log.Fatalf("Error: %s.\n", err.Error())
}
defer recorder.Delete()
log.Printf("Using device: %s", recorder.GetSelectedDevice())
log.Println("Starting...")
if err := recorder.Start(); err != nil {
log.Fatalf("Error: %s.\n", err.Error())
}
signalCh := make(chan os.Signal, 1)
waitCh := make(chan struct{})
signal.Notify(signalCh, os.Interrupt)
go func() {
<-signalCh
close(waitCh)
}()
var outputWav *wav.Encoder
if *outputWavPath != "" {
outputFilePath, _ := filepath.Abs(*outputWavPath)
outputFile, err := os.Create(outputFilePath)
if err != nil {
log.Fatalf("Failed to create output audio at path %s", outputFilePath)
}
defer outputFile.Close()
outputWav = wav.NewEncoder(outputFile, pvrecorder.SampleRate, 16, 1, 1)
defer outputWav.Close()
}
waitLoop:
for {
select {
case <-waitCh:
log.Println("Stopping...")
break waitLoop
default:
pcm, err := recorder.Read()
if err != nil {
log.Fatalf("Error: %s.\n", err.Error())
}
if outputWav != nil {
for outputBufIndex := range pcm {
err := outputWav.WriteFrame(pcm[outputBufIndex])
if err != nil {
log.Fatal(err)
}
}
}
}
}
}