-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecording.go
237 lines (207 loc) · 5.81 KB
/
recording.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"errors"
"log"
"time"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
"github.com/gordonklaus/portaudio"
"github.com/zimmski/osutil"
)
const sampleRate = 44100
var isRecording bool = false
var audioStream chan []int32 = make(chan []int32, 10)
var currentSession Session
var isPlaying bool = false
var audioDiskStream *wav.Encoder
// Playback position in samples
var playbackPosition int = 0
var portaudioInitialized bool = false
func initPortAudio() {
var err error
o, _ := osutil.CaptureWithCGo(func() {
err = portaudio.Initialize()
})
log.Printf(string(o))
if err != nil {
log.Fatalf("Failed to initialize recording: %s", err)
}
portaudioInitialized = true
}
func record() {
const bufSize = 1024
// This is based on the record example shown in the portaudio repo.
// It's unclear whether or not framesPerBuffer should match the buffer size
// or be zero (where portaudio will provide variable length buffers).
in := make([]int32, bufSize)
stream, err := portaudio.OpenDefaultStream(1, 0, sampleRate, len(in), in)
if err != nil {
log.Fatalf("Failed to open stream audio: %s", err)
}
defer stream.Close()
err = stream.Start()
if err != nil {
log.Fatalf("Failed to start stream audio: %s", err)
}
isRecording = true
log.Print("Recording started")
for {
// wait for enough audio to fill the buffer
for avail := 0; avail < len(in); avail, _ = stream.AvailableToRead() {
time.Sleep(time.Second / sampleRate * time.Duration(len(in)-avail) / 2)
}
err := stream.Read()
if err != nil {
log.Fatalf("Failed to read stream audio: %s", err)
}
audioStream <- in
if !isRecording {
break
}
}
log.Print("Recording stopped")
}
func StartSession() {
currentSession.deriveId()
var err error
audioDiskStream, err = currentSession.StartStreamingToDisk()
if err != nil {
log.Fatalf("Failed to streaming to disk: %s", err)
}
if !isRecording {
go record()
}
}
func EndSession() error {
if isRecording {
isRecording = false
}
currentSession.StopStreamingToDisk(audioDiskStream)
err := currentSession.FullSave()
if err != nil {
log.Printf("Failed to save session: %s", err)
return err
}
return nil
}
func audioProcessor() {
log.Print("Audio processing started")
const displayBufferSize = sampleRate
for {
buffer := <-audioStream
if cap(audioStream)-len(audioStream) < 3 {
log.Printf("WARNING: audioStream channel is being overloaded, buffered messages: %d/%d", len(audioStream), cap(audioStream))
}
currentSession.Audio = append(currentSession.Audio, buffer...)
buf := audio.PCMBuffer{
Format: audio.FormatMono44100,
DataType: audio.DataTypeI32,
SourceBitDepth: 32,
I32: buffer,
}
audioDiskStream.Write(buf.AsIntBuffer())
if isRecordingTake {
if isRecordingSyncTake {
currentSession.Doc.syncTakes[selectedTake].End = samplesToDuration(sampleRate, len(currentSession.Audio))
} else {
chunk := currentSession.Doc.GetChunk(int(selectedChunk))
chunk.Takes[selectedTake].End = samplesToDuration(sampleRate, len(currentSession.Audio))
}
}
}
}
func playbackTimespan(timespan TimeSpan) {
isPlaying = true
log.Printf("playing back %d samples...", len(currentSession.Audio))
// This is based on the play example shown in the portaudio repo.
const bufSize = 1024
if !portaudioInitialized {
initPortAudio()
}
out := make([]int32, bufSize)
stream, err := portaudio.OpenDefaultStream(0, 1, sampleRate, len(out), &out)
if err != nil {
log.Fatalf("Failed to open stream audio: %s", err)
}
defer stream.Close()
log.Printf("stream open: %v", stream.Info())
err = stream.Start()
if err != nil {
log.Fatalf("Failed to start stream audio: %s", err)
}
defer stream.Stop()
log.Printf("stream started")
start := durationToSamples(sampleRate, timespan.Start)
end := durationToSamples(sampleRate, timespan.End)
samples := currentSession.Audio[start:end]
for b := 0; b < len(samples); b += len(out) {
playbackPosition = start + b
out = samples[b:clamp(b+bufSize, 0, len(samples))]
err := stream.Write()
if err != nil {
log.Printf("Failed to write stream audio: %v", err)
}
}
log.Printf("playback complete")
isPlaying = false
}
func playbackTake(take Take) {
playbackTimespan(TimeSpan{
Start: take.Start,
End: take.End,
})
}
func samplesToDuration(sampleRate int, nSamples int) time.Duration {
return time.Duration(nSamples) * time.Second / time.Duration(sampleRate)
}
func durationToSamples(sampleRate int, d time.Duration) int {
return int(d.Seconds() * float64(sampleRate))
}
type TimeSpan struct {
Start time.Duration
End time.Duration
}
func (t *TimeSpan) Duration() time.Duration {
return t.End - t.Start
}
type Take struct {
TimeSpan
Mark TakeMark
}
func startTake(sync bool) error {
if isRecordingTake {
return errors.New("Already recording take")
}
take := Take{}
take.Start = samplesToDuration(sampleRate, len(currentSession.Audio))
if sync {
take.Mark = Sync
currentSession.Doc.syncTakes = append(currentSession.Doc.syncTakes, take)
selectedTake = len(currentSession.Doc.syncTakes) - 1
isRecordingSyncTake = true
} else {
chunk := currentSession.Doc.GetChunk(int(selectedChunk))
chunk.Takes = append(chunk.Takes, take)
selectedTake = len(chunk.Takes) - 1
}
isRecordingTake = true
return nil
}
func endTake() error {
if !isRecordingTake {
return errors.New("Not recording take")
}
if isRecordingSyncTake {
currentSession.Doc.syncTakes[selectedTake].End = samplesToDuration(sampleRate, len(currentSession.Audio))
isRecordingSyncTake = false
if currentSession.Doc.SyncOffset == time.Duration(0) {
currentSession.updateSyncOffset()
}
} else {
chunk := currentSession.Doc.GetChunk(int(selectedChunk))
chunk.Takes[selectedTake].End = samplesToDuration(sampleRate, len(currentSession.Audio))
}
isRecordingTake = false
currentSession.FullSave()
return nil
}