/
foxDSP.go
703 lines (570 loc) · 19.9 KB
/
foxDSP.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package foxDSP
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"scientificgo.org/fft"
)
const FilterLength = 4000
func hammingWindow(length int) []float64 {
window := make([]float64, length)
alpha := 0.54
beta := 0.46
for i := 0; i < length; i++ {
window[i] = alpha - beta*math.Cos(2*math.Pi*float64(i)/float64(length-1))
}
return window
}
func ConvolveImpulsesFFTwithTail(impulse, signal []float64, overlapSize int, tail []float64) ([]float64, []float64) {
impulseLength := len(impulse)
signalLength := len(signal)
outputLength := impulseLength + signalLength - 1
// Pad input arrays to the nearest power of 2
paddedLength := int(math.Pow(2, math.Ceil(math.Log2(float64(impulseLength+signalLength-1)))))
paddedImpulse := make([]complex128, paddedLength)
paddedSignal := make([]complex128, paddedLength)
// Apply Hamming window to impulse and signal
hammingWindowImpulse := hammingWindow(impulseLength)
hammingWindowSignal := hammingWindow(signalLength)
// Map impulse and signal to padded arrays with windowing
for i := 0; i < impulseLength; i++ {
paddedImpulse[i] = complex(impulse[i]*hammingWindowImpulse[i], 0)
}
for i := 0; i < signalLength; i++ {
paddedSignal[i] = complex(signal[i]*hammingWindowSignal[i], 0)
}
// Combine tail and signal into a combined block
combinedBlock := make([]float64, len(tail)+signalLength)
copy(combinedBlock, tail)
copy(combinedBlock[len(tail):], signal)
// Pad combined block to the padded length
paddedCombinedBlock := make([]complex128, paddedLength)
for i := 0; i < len(combinedBlock); i++ {
paddedCombinedBlock[i] = complex(combinedBlock[i], 0)
}
// Apply FFT to impulse and combined block
fft.Fft(paddedImpulse, false)
fft.Fft(paddedCombinedBlock, false)
// Multiply the transformed arrays element-wise
multipliedResult := make([]complex128, paddedLength)
for i := 0; i < paddedLength; i++ {
multipliedResult[i] = paddedImpulse[i] * paddedCombinedBlock[i]
}
// Apply inverse FFT to the multiplied result
fft.Fft(multipliedResult, true)
// Extract convolved output excluding overlap region
convolvedOutput := make([]float64, outputLength)
for i := overlapSize; i < outputLength+overlapSize; i++ {
convolvedOutput[i-overlapSize] = real(multipliedResult[i])
}
// Update tail for next iteration
tail = make([]float64, overlapSize)
for i := 0; i < overlapSize; i++ {
tail[i] = real(multipliedResult[i+outputLength])
}
return convolvedOutput, tail
}
func ConvolveImpulsesFFT(impulse, signal []float64) []float64 {
impulseLength := len(impulse)
signalLength := len(signal)
outputLength := impulseLength + signalLength - 1
result := make([]float64, outputLength)
// Pad input arrays to the nearest power of 2
paddedLength := int(math.Pow(2, math.Ceil(math.Log2(float64(impulseLength+signalLength-1)))))
paddedImpulse := make([]complex128, paddedLength)
paddedSignal := make([]complex128, paddedLength)
// Apply Hamming window to impulse and signal
hammingWindowImpulse := hammingWindow(impulseLength)
hammingWindowSignal := hammingWindow(signalLength)
// Map impulse and signal over the length of the padded array with applied window
for i := 0; i < impulseLength; i++ {
paddedImpulse[i] = complex(impulse[i]*hammingWindowImpulse[i], 0)
}
for i := 0; i < signalLength; i++ {
paddedSignal[i] = complex(signal[i]*hammingWindowSignal[i], 0)
}
// Apply FFT to both padded input arrays
fft.Fft(paddedImpulse, false)
fft.Fft(paddedSignal, false)
// Multiply the transformed arrays element-wise
multipliedResult := make([]complex128, paddedLength)
for i := 0; i < paddedLength; i++ {
multipliedResult[i] = paddedImpulse[i] * paddedSignal[i]
}
// Apply inverse FFT to the multiplied result
fft.Fft(multipliedResult, true)
// Extract the real part and assign it to the result array
for i := 0; i < outputLength; i++ {
result[i] = real(multipliedResult[i])
}
return result
}
func IIRFilter(input []float64, b [3]float64, a [3]float64, output []float64) {
N := len(input)
M := len(b)
//L := len(a)
y := make([]float64, N)
w := make([]float64, M)
for n := 0; n < N; n++ {
y[n] = b[0]*input[n] + w[0]
for i := 1; i < M; i++ {
w[i-1] = b[i]*input[n] + w[i] - a[i]*y[n]
}
output[n] = y[n]
}
}
func CascadeFilters(coefficients [][]float64, myFilterLength int) []float64 {
//results are more consistent with REW if filters are cascaded before generating an impulse response rather than merging a set of impulses
maxLength := len(coefficients)
if maxLength == 0 {
fmt.Printf("No coeeficients: ")
return nil
}
if myFilterLength == 0 {
myFilterLength = FilterLength
}
var a, b [3]float64
impulse := make([]float64, myFilterLength)
signal := make([]float64, myFilterLength)
impulse[0] = 1
for i := 0; i < maxLength; i++ {
a[0] = coefficients[i][0]
a[1] = coefficients[i][1]
a[2] = coefficients[i][2]
b[0] = coefficients[i][3]
b[1] = coefficients[i][4]
b[2] = coefficients[i][5]
IIRFilter(impulse, b, a, signal)
impulse = make([]float64, len(signal))
copy(impulse, signal)
}
return signal
}
func GenerateImpulseResponse(a0, a1, a2, b0, b1, b2 float64) []float64 {
a := [3]float64{a0, a1, a2}
b := [3]float64{b0, b1, b2}
// Create an array of zeros of length 'FilterLength'
impulse := make([]float64, FilterLength)
// Set the first element of the impulse array to 1
impulse[0] = 1
// Create a signal array of length 'FilterLength'
signal := make([]float64, FilterLength)
// Apply the filter coefficients to the impulse array using the IIRFilter function
IIRFilter(impulse, b, a, signal)
return signal
}
func CalcBiquadFilter(filterType string, centreFrequency, sampleRate int, peakGain, width float64, slopeType string) []float64 {
// Nyquist frequency approximation
Nyquist := 0.445
if centreFrequency < 10 || centreFrequency > 25000 {
panic(fmt.Sprintf("frequency should be between 10 and 25000, got: %d", centreFrequency))
}
if sampleRate < 10000 || sampleRate > 400000 {
panic(fmt.Sprintf("sampleRate should be a recognized sample rate, got: %d", sampleRate))
}
if peakGain < -30 || peakGain > 20 {
panic(fmt.Sprintf("peakGain should be between -30 and +20, got: %f", peakGain))
}
// Adjust frequency if it exceeds Nyquist frequency
if float64(centreFrequency)/float64(sampleRate) > Nyquist {
// Need to add in Error Logger here.
centreFrequency = int(float64(sampleRate) * Nyquist)
}
b0, b1, b2, a0, a1, a2, norm := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0
ampl := math.Pow(10, math.Abs(peakGain)/40)
if peakGain < 0 {
ampl = 1 / ampl
}
SQRTA := math.Sqrt(ampl)
var alpha, beta float64
if filterType == "lowpass" || filterType == "highcut" {
// Correct for the low pass filter rolling off too early
desiredLevel := -1.0
x := math.Pow(10, desiredLevel/20.0)
desiredStartingPoint := float64(centreFrequency) + float64(centreFrequency) - (float64(centreFrequency) * x)
centreFrequency = int(desiredStartingPoint)
filterType = "lowpass"
}
if filterType == "highpass" || filterType == "lowcut" {
filterType = "highpass"
desiredLevel := 2.0
desiredStartingPoint := float64(centreFrequency) * math.Sqrt(math.Pow(10, desiredLevel/10.0)-1)
centreFrequency = int(desiredStartingPoint)
}
omega := 2 * math.Pi * float64(centreFrequency) / float64(sampleRate)
coso := math.Cos(omega)
sino := math.Sin(omega)
switch slopeType {
case "slope":
alpha = sino / 2 * math.Sqrt((ampl+1/ampl)*(1/width-1)+2)
case "Q":
alpha = sino / (2 * width)
case "octave":
alpha = sino * math.Sinh(math.Log(2)/2*width*omega/sino)
default:
panic(fmt.Sprintf("slopeType %s was not recognized [slope, Q, octave]", slopeType))
}
switch filterType {
case "lowpass":
norm = 1 / (1.0 + alpha)
b0 = norm * ((1.0 - coso) / 2.0)
b1 = norm * (1.0 - coso)
b2 = norm * ((1.0 - coso) / 2.0)
a0 = 1.0
a1 = norm * (-2.0 * coso)
a2 = norm * (1.0 - alpha)
case "highpass":
norm = 1 / (1.0 + alpha)
b0 = norm * ((1.0 + coso) / 2.0)
b1 = norm * (-(1.0 + coso))
b2 = b0
a0 = 1.0
a1 = norm * (-2.0 * coso)
a2 = norm * (1.0 - alpha)
case "bandpass":
norm = 1 / (1.0 + alpha)
b0 = norm * (alpha)
b1 = 0.0
b2 = norm * (-alpha)
a0 = 1.0
a1 = norm * (-2.0 * coso)
a2 = norm * (1.0 - alpha)
case "notch":
norm = 1 / (1.0 + alpha)
b0 = norm
b1 = norm * -2.0 * coso
b2 = b0
a0 = 1.0
a1 = norm * -2.0 * coso
a2 = norm * (1.0 - alpha)
case "peak":
norm = 1 / (1 + alpha/ampl)
b0 = norm * (1 + alpha*ampl)
b1 = norm * (-2 * coso)
b2 = norm * (1 - alpha*ampl)
a0 = 1
a1 = norm * (-2 * coso)
a2 = norm * (1 - alpha/ampl)
case "lowshelf":
beta = 2.0 * SQRTA * alpha
norm = 1 / (ampl + 1 + (ampl-1)*coso + 2*SQRTA*alpha)
b0 = norm * (ampl * (ampl + 1 - (ampl-1)*coso + beta))
b1 = norm * (2 * ampl * (ampl - 1 - (ampl+1)*coso))
b2 = norm * (ampl * (ampl + 1 - (ampl-1)*coso - beta))
a0 = 1
a1 = norm * (-2 * (ampl - 1 + (ampl+1)*coso))
a2 = norm * (ampl + 1 + (ampl-1)*coso - beta)
case "highshelf":
norm = 1 / ((ampl + 1) - (ampl-1)*coso + 2*SQRTA*alpha)
b0 = norm * (ampl * ((ampl + 1) + (ampl-1)*coso + 2*SQRTA*alpha))
b1 = norm * (-2 * ampl * ((ampl - 1) + (ampl+1)*coso))
b2 = norm * (ampl * ((ampl + 1) + (ampl-1)*coso - 2*SQRTA*alpha))
a0 = 1.0
a1 = norm * (2 * ((ampl - 1) - (ampl+1)*coso))
a2 = norm * ((ampl + 1) - (ampl-1)*coso - 2*SQRTA*alpha)
default:
panic(fmt.Sprintf("filterType %s was not set to a recognized filter type", filterType))
}
myCoefficients := []float64{a0, a1, a2, b0, b1, b2}
return myCoefficients
}
//===================Stream Manager ==========================
// StreamManager is the equivalent of the C# StreamManager struct
type StreamManager struct {
InputImpulse [][]float64
}
// ExportWavFile exports the WAV file using the CreateMemoryStream function
func ExportWavFile(filename string, sampleRate, bitDepth int, sm *StreamManager) error {
// Create memory stream
wavData, err := sm.CreateMemoryStream(sampleRate, bitDepth)
if err != nil {
return err
}
// Write the WAV data to a file
//err = ioutil.WriteFile(filename, wavData, 0666)
err = ioutil.WriteFile(filename, wavData.Bytes(), 0666)
if err != nil {
return err
}
fmt.Printf("WAV file successfully exported to %s\n", filename)
return nil
}
// CreateMemoryStream is the equivalent of the C# CreateMemoryStream method
func (sm *StreamManager) CreateMemoryStream(sampleRate, bitDepth int) (*bytes.Buffer, error) {
if bitDepth != 16 && bitDepth != 24 && bitDepth != 32 {
return nil, errors.New("bit depth should be 16, 24, or 32 bits")
}
numSamples := len(sm.InputImpulse[0])
numChannels := len(sm.InputImpulse)
for channel := 0; channel < numChannels; channel++ {
if numSamples != len(sm.InputImpulse[channel]) {
return nil, errors.New("all channel arrays must have the same length")
}
}
// Calculate the total data size in bytes
dataSize := numSamples * numChannels * (bitDepth / 8)
// Create a new bytes.Buffer to hold the WAV stream
buffer := new(bytes.Buffer)
// Write the RIFF header
buffer.WriteString("RIFF")
binary.Write(buffer, binary.LittleEndian, int32(dataSize+36)) // Total file size - 36 bytes for the header
buffer.WriteString("WAVE")
// Write the format chunk
buffer.WriteString("fmt ")
binary.Write(buffer, binary.LittleEndian, int32(16)) // Size of the format chunk
binary.Write(buffer, binary.LittleEndian, int16(1)) // Audio format (PCM)
binary.Write(buffer, binary.LittleEndian, int16(numChannels)) // Number of channels
binary.Write(buffer, binary.LittleEndian, int32(sampleRate)) // Sample rate
binary.Write(buffer, binary.LittleEndian, int32(sampleRate*numChannels*(bitDepth/8))) // Byte rate
binary.Write(buffer, binary.LittleEndian, int16(numChannels*(bitDepth/8))) // Block align
binary.Write(buffer, binary.LittleEndian, int16(bitDepth)) // Bits per sample
// Write the data chunk header
buffer.WriteString("data")
binary.Write(buffer, binary.LittleEndian, int32(dataSize))
for i := 0; i < numSamples; i++ {
for channel := 0; channel < numChannels; channel++ {
sample := math.Max(-1.0, math.Min(1.0, sm.InputImpulse[channel][i]))
var sampleBytes []byte
switch bitDepth {
case 16:
sampleBytes = convertTo16BitSample(sample)
case 24:
sampleBytes = convertTo24BitSample(sample)
case 32:
sampleBytes = convertTo32BitSample(sample)
}
buffer.Write(sampleBytes)
}
}
return buffer, nil
}
func convertTo16BitSample(sample float64) []byte {
const bitDepth = 16
maxValue := (1 << (bitDepth - 1)) - 1
minValue := -maxValue
// Scale the sample to the range of 16-bit signed integers
scaledValue := sample * float64(maxValue)
// Round to the nearest integer
roundedValue := int(math.Round(scaledValue))
// Clip the value to the valid range
clippedValue := int(math.Max(float64(minValue), math.Min(float64(maxValue), float64(roundedValue))))
// Convert the clipped value to a byte array in little-endian format
return []byte{
byte(clippedValue & 0xFF),
byte((clippedValue >> 8) & 0xFF),
}
}
func convertTo24BitSample(sample float64) []byte {
const bitDepth = 24
maxValue := (1 << (bitDepth - 1)) - 1
minValue := -maxValue
// Scale the sample to the range of 24-bit signed integers
scaledValue := sample * float64(maxValue)
// Round to the nearest integer
roundedValue := int(math.Round(scaledValue))
// Clip the value to the valid range
clippedValue := int(math.Max(float64(minValue), math.Min(float64(maxValue), float64(roundedValue))))
// Convert the clipped value to a byte array in little-endian format
return []byte{
byte(clippedValue & 0xFF),
byte((clippedValue >> 8) & 0xFF),
byte((clippedValue >> 16) & 0xFF),
}
}
func convertTo32BitSample(sample float64) []byte {
const bitDepth = 32
maxValue := (1 << (bitDepth - 1)) - 1
minValue := -maxValue
// Scale the sample to the range of 32-bit signed integers
scaledValue := sample * float64(maxValue)
// Round to the nearest integer
roundedValue := int64(math.Round(scaledValue))
// Clip the value to the valid range
clippedValue := int64(math.Max(float64(minValue), math.Min(float64(maxValue), float64(roundedValue))))
// Convert the clipped value to a byte array in little-endian format
return []byte{
byte(clippedValue & 0xFF),
byte((clippedValue >> 8) & 0xFF),
byte((clippedValue >> 16) & 0xFF),
byte((clippedValue >> 24) & 0xFF),
}
}
// Calculates the Maximum Gain Value - used for Normalization functions
func CalculateMaxGain(audioData []float64) float64 {
maxGain := 0.0
if audioData == nil {
return maxGain
}
for _, sample := range audioData {
sampleAbs := math.Abs(sample)
if sampleAbs > maxGain {
maxGain = sampleAbs
}
}
return maxGain
}
func NormalizeAudioImpulse(audioImpulse []float64, targetLevel float64, max float64) []float64 {
// Check for divide by zero and no normalization needed
if max == 0.0 || max == targetLevel {
return audioImpulse
}
// Calculate the normalization factor
normalizationFactor := targetLevel / max
// Normalize the audio impulse
normalizedImpulse := make([]float64, len(audioImpulse))
for i := range audioImpulse {
normalizedImpulse[i] = audioImpulse[i] * normalizationFactor
}
return normalizedImpulse
}
// Normalizes Audio Data in supploed samples
func Normalize(inputSamples [][]float64, targetLevel float64) float64 {
// Find max gain of channels
impulseGain := 0.0
for _, channel := range inputSamples {
testGain := CalculateMaxGain(channel)
if testGain > impulseGain {
impulseGain = testGain
}
}
if impulseGain == 0 {
return impulseGain
}
// Normalize gain
for i := range inputSamples {
inputSamples[i] = NormalizeAudioImpulse(inputSamples[i], targetLevel, impulseGain) // Replace with actual implementation
}
return impulseGain
}
func resampleChannel(inputSamples []float64, fromSampleRate, toSampleRate, quality int) []float64 {
// If no resampling required
if fromSampleRate == toSampleRate {
return inputSamples
}
// Best so far, slight hump where low pass filter is applied.
var samples []float64
srcLength := len(inputSamples)
destLength := int(float64(srcLength) * float64(toSampleRate) / float64(fromSampleRate))
dx := float64(srcLength) / float64(destLength)
if fromSampleRate < 88000 {
// Apply filter before resampling to avoid a bump
//num := float64(toSampleRate) / float64(fromSampleRate)
// Fmax: Nyquist half of destination sampleRate
// Fmax / sampleRater = 0.5
// Apply a low pass before resampling - this has been hand-tested
myFilters := [][]float64{CalcBiquadFilter("lowpass", 18000, fromSampleRate, 0, 0.3, "Q")}
lowPassImpulse := CascadeFilters(myFilters, 0)
inputSamples = ConvolveImpulsesFFT(lowPassImpulse, inputSamples)
}
fmaxDivSR := 0.5
rG := 2 * fmaxDivSR
// Quality is half the window width
wndWidth2 := quality
wndWidth := quality * 2
x := 0.0
var rY, rW, rA, rSnc float64
var tau, j int
for i := 0; i < destLength; i++ {
rY = 0.0
for tau = -wndWidth2; tau < wndWidth2; tau++ {
// Input sample index
j = int(x + float64(tau))
// Hann Window. Scale and calculate sinc
rW = 0.5 - 0.5*math.Cos(2*math.Pi*(0.5+(float64(j)-x)/float64(wndWidth)))
rA = 2 * math.Pi * (float64(j) - x) * fmaxDivSR
rSnc = 1.0
if rA != 0 {
rSnc = math.Sin(rA) / rA
}
if j >= 0 && j < srcLength {
rY += rG * rW * rSnc * inputSamples[j]
}
}
samples = append(samples, rY)
x += dx
}
return samples
}
func resampler(inputSamples [][]float64, fromSampleRate, toSampleRate, quality int) [][]float64 {
if fromSampleRate == toSampleRate {
return inputSamples
}
myChannelsLength := len(inputSamples)
for c := 0; c < myChannelsLength; c++ {
inputSamples[c] = resampleChannel(inputSamples[c], fromSampleRate, toSampleRate, quality)
}
return inputSamples
}
func ReadWavFile(filename string) ([][]float64, uint32, uint16, error) {
file, err := os.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
return nil, 0, 0, err
}
defer file.Close()
//
if err != nil {
panic(err) // Handle the error appropriately
}
// Need to plod through this and convert c# function line by line. See SimpleDSP ReadWav
// Read the header
var chunkID [4]byte
var chunkSize uint32
var format [4]byte
var subchunk1ID [4]byte
var subchunk1Size uint32
var audioFormat uint16
var numChannels uint16
var sampleRate uint32
var byteRate uint32
var blockAlign uint16
var bitsPerSample uint16
var subchunk2ID [4]byte
var subchunk2Size uint32
binary.Read(file, binary.LittleEndian, &chunkID)
binary.Read(file, binary.LittleEndian, &chunkSize)
binary.Read(file, binary.LittleEndian, &format)
binary.Read(file, binary.LittleEndian, &subchunk1ID)
binary.Read(file, binary.LittleEndian, &subchunk1Size)
binary.Read(file, binary.LittleEndian, &audioFormat)
binary.Read(file, binary.LittleEndian, &numChannels)
binary.Read(file, binary.LittleEndian, &sampleRate)
binary.Read(file, binary.LittleEndian, &byteRate)
binary.Read(file, binary.LittleEndian, &blockAlign)
binary.Read(file, binary.LittleEndian, &bitsPerSample)
binary.Read(file, binary.LittleEndian, &subchunk2ID)
binary.Read(file, binary.LittleEndian, &subchunk2Size)
// Read the data
numSamples := subchunk2Size / uint32(numChannels/(bitsPerSample/8))
samples := make([][]float64, numChannels)
for i := range samples {
samples[i] = make([]float64, numSamples)
}
for i := uint32(0); i < numSamples; i++ {
for j := uint16(0); j < numChannels; j++ {
switch bitsPerSample {
case 8:
var sample uint8
binary.Read(file, binary.LittleEndian, &sample)
samples[j][i] = float64(sample)
case 16:
var sample int16
binary.Read(file, binary.LittleEndian, &sample)
samples[j][i] = float64(sample) / 32768.0
case 24:
var sample [3]byte
binary.Read(file, binary.LittleEndian, &sample)
sample32 := int32(sample[0]) | int32(sample[1])<<8 | int32(sample[2])<<16
samples[j][i] = float64(sample32) / 8388608.0
case 32:
var sample int32
binary.Read(file, binary.LittleEndian, &sample)
samples[j][i] = float64(sample) / 2147483648.0
}
}
}
return samples, sampleRate, bitsPerSample, nil
}