-
Notifications
You must be signed in to change notification settings - Fork 20
/
wav.go
100 lines (83 loc) · 2.63 KB
/
wav.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
// Package wavwriter allows writing of audio data to disk as a WAV file.
package wavwriter
import (
"fmt"
"os"
"github.com/jetsetilly/gopher2600/hardware/television/signal"
tia "github.com/jetsetilly/gopher2600/hardware/tia/audio"
"github.com/jetsetilly/gopher2600/hardware/tia/audio/mix"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
)
// WavWriter implements the television.AudioMixer interface
type WavWriter struct {
filename string
buffer []int16
}
// New is the preferred method of initialisation for the WavWriter type
func NewWavWriter(filename string) (*WavWriter, error) {
aw := &WavWriter{
filename: fmt.Sprintf("%s.wav", filename),
buffer: make([]int16, 0, 0),
}
return aw, nil
}
// SetAudio implements the television.AudioMixer interface.
func (aw *WavWriter) SetAudio(sig []signal.SignalAttributes) error {
for _, s := range sig {
if s&signal.AudioUpdate != signal.AudioUpdate {
continue
}
v0 := uint8((s & signal.AudioChannel0) >> signal.AudioChannel0Shift)
v1 := uint8((s & signal.AudioChannel1) >> signal.AudioChannel1Shift)
m := mix.Mono(v0, v1)
aw.buffer = append(aw.buffer, m)
}
return nil
}
const numChannels = 1
const bitDepth = 16
// EndMixing implements the television.AudioMixer interface
func (aw *WavWriter) EndMixing() error {
f, err := os.Create(aw.filename)
if err != nil {
return fmt.Errorf("wavwriter: %w", err)
}
defer f.Close()
enc := wav.NewEncoder(f, tia.SampleFreq, bitDepth, numChannels, 1)
if enc == nil {
return fmt.Errorf("wavwriter: bad parameters for wav encoding")
}
defer enc.Close()
buf := audio.PCMBuffer{
Format: &audio.Format{
NumChannels: numChannels,
SampleRate: tia.SampleFreq,
},
I16: aw.buffer,
DataType: audio.DataTypeI16,
SourceBitDepth: bitDepth,
}
err = enc.Write(buf.AsIntBuffer())
if err != nil {
return fmt.Errorf("wavwriter: %w", err)
}
return nil
}
// Reset implements the television.AudioMixer interface
func (aw *WavWriter) Reset() {
}