summaryrefslogtreecommitdiff
path: root/wav/wav.go
blob: acd78418c2f0648e523a67126d55125c1e81d420 (plain) (blame)
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
package wav

// #define DR_WAV_IMPLEMENTATION
// #include "dr_wav.h"
import "C"
import (
	"fmt"

	"sync"

	"github.com/op/go-logging"
	"layeh.com/gopus"
)

// number of individual samples per channel per batch
const samplesPerChannelPerBatch = 1920

var log = logging.MustGetLogger("wav")

type Wav struct {
	filename *C.char
	enc      *gopus.Encoder
	wav      *C.drwav

	once sync.Once
	Done <-chan struct{}
	done chan<- struct{}
}

func New(filename string) (*Wav, error) {
	cfname := C.CString(filename)
	wav := C.drwav_open_file(cfname)
	if wav == nil {
		C.drwav_close(wav)
		return nil, fmt.Errorf("Unable to initialize drwav.")
	}

	if int(wav.channels) != 2 {
		C.drwav_close(wav)
		return nil, fmt.Errorf("Wrong number of channels!")
	}

	if int(wav.sampleRate) != 48000 {
		C.drwav_close(wav)
		return nil, fmt.Errorf("Wrong sample rate.")
	}

	enc, err := gopus.NewEncoder(int(wav.sampleRate), int(wav.channels), gopus.Audio)
	if err != nil {
		C.drwav_close(wav)
		return nil, err
	}

	done := make(chan struct{}, 1)
	return &Wav{
		filename: C.CString(filename),
		enc:      enc,
		wav:      wav,

		done: done,
		Done: done,
	}, nil
}

func (w *Wav) Stop() {
	w.once.Do(func() {
		close(w.done)
	})
}

func (w *Wav) Start(ch chan<- []byte) {
	go func() {
		defer w.Stop()

		samplesPerBatch := samplesPerChannelPerBatch * int(w.wav.channels)
		batchSize := samplesPerBatch * int(w.wav.bytesPerSample)

		buf := C.malloc(C.size_t(batchSize))
		defer C.free(buf)
		defer C.drwav_close(w.wav)

		elems := make([]int16, samplesPerBatch)
		idx := 0

		for i := 0; i*samplesPerBatch <= int(w.wav.totalSampleCount); i += 1 {
			readSamples := C.drwav_read_s16(w.wav, C.dr_uint64(samplesPerBatch), (*C.dr_int16)(buf))
			slc := (*[1 << 30]int16)(buf)[:readSamples:readSamples]

			readIdx := 0

			for {
				batchSamplesToFill := samplesPerBatch - idx
				readSamplesRemaining := int(readSamples) - readIdx

				// break if we don't have enough samples to fill the rest of the buffer
				if readSamplesRemaining < batchSamplesToFill {
					break
				}

				copy(elems[idx:], slc[readIdx:readIdx+batchSamplesToFill])
				idx = 0
				readIdx += batchSamplesToFill

				b, err := processPCM(w.wav, w.enc, elems[:])
				if err != nil {
					log.Errorf("error encoding pcm: %q", err)
					continue
				}

				select {
				case <-w.Done:
					return
				default:
				}
				select {
				case <-w.Done:
					return
				case ch <- b:
				}
			}

			batchSamplesToFill := samplesPerBatch - idx
			readSamplesRemaining := int(readSamples) - readIdx
			if readSamplesRemaining >= batchSamplesToFill {
				log.Fatalf("Had enough samples to fill batch after for loop.")
			}

			copy(elems[idx:], slc)
			idx += len(slc)

			if int(readSamples) < samplesPerBatch {
				break
			}
		}
	}()
}

func processPCM(wav *C.drwav, enc *gopus.Encoder, data []int16) ([]byte, error) {
	return enc.Encode(data, len(data)/int(wav.channels), len(data)*2)
}