-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
153 lines (123 loc) · 2.94 KB
/
record.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
//"github.com/williamsharkey/rec/portaudio"
//"fmt"
//"github.com/williamsharkey/rec/portaudio"
"github.com/williamsharkey/rec/go-wave"
"github.com/williamsharkey/rec/portaudio"
"os"
//"os"
//"os/signal"
)
func Record(cutoff int, name string, f func(s string), turnOff func(), errf func(s string)) (killRec chan int) {
killRec = make(chan int, 1)
recImpl(cutoff, name, killRec, f, turnOff, errf)
return
}
func recImpl(cutoff int, name string, killRec chan int, f func(s string), turnOff func(), errf func(s string)) (err error) {
//sig := make(chan os.Signal, 1)
// signal.Notify(sig, os.Interrupt, os.Kill)
audioFileName := name + ".wav" //os.Args[1]
//fmt.Println("Recording to "+audioFileName, cutoff, "seconds")
waveFile, err := os.Create(audioFileName)
if err != nil {
errf(err.Error())
return
}
inputChannels := 1
outputChannels := 0
sampleRate := 44100
buf := 4410
int16Slice := make([]int16, buf)
portaudio.Initialize()
//defer portaudio.Terminate()
//apis, err := portaudio.HostApis()
//for i, api := range apis {
//
// fmt.Println(i, api.Name, api.Type)
// for j, d := range api.Devices {
//
// fmt.Println(" ", j, d.Name, d.MaxInputChannels, d.MaxOutputChannels)
// }
//}
stream, err := portaudio.OpenDefaultStream(inputChannels, outputChannels, float64(sampleRate), len(int16Slice), int16Slice)
if err != nil {
errf(err.Error())
return
}
//defer stream.Close()
// setup Wave file writer
param := wave.WriterParam{
Out: waveFile,
Channel: inputChannels,
SampleRate: sampleRate,
BitsPerSample: 16, // if 16, change to WriteSample16()
}
waveWriter, err := wave.NewWriter(param)
if err != nil {
errf(err.Error())
return
}
defer waveWriter.Close()
// start reading from microphone
err = stream.Start()
if err != nil {
errf(err.Error())
return
}
//fmt.Println("Recording is live now. Say something to your microphone!")
i := 0
//uInt16Slice := make([]uint16, buf, buf)
for {
err = stream.Read()
if err != nil {
errf(err.Error())
return
}
i++
//uInt16Slice = *(*[]uint16)(unsafe.Pointer(&int16Slice))
_, err := waveWriter.WriteSample16(int16Slice)
if err != nil {
errf(err.Error())
return err
}
elapsed := (i * buf) / sampleRate
f(fmt.Sprintf("%d", elapsed))
//fmt.Printf("\r %d", elapsed)
if cutoff != 0 && elapsed >= cutoff {
closeX(waveWriter, stream)
turnOff()
return err
}
select {
//case <-sig:
// close(sig)
// close(killRec)
// closeX(waveWriter, stream)
// turnOff()
// return err
case <-killRec:
//close(sig)
close(killRec)
fmt.Println("kill")
closeX(waveWriter, stream)
turnOff()
return err
default:
}
}
err = stream.Stop()
if err != nil {
errf(err.Error())
return err
}
return
}
func closeX(waveWriter *wave.Writer, stream *portaudio.Stream) {
//fmt.Println("\nclosed")
waveWriter.Close()
stream.Close()
portaudio.Terminate()
//os.Exit(0)
}