-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_processing_test.go
More file actions
177 lines (156 loc) · 5.51 KB
/
audio_processing_test.go
File metadata and controls
177 lines (156 loc) · 5.51 KB
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
package main
import (
"fmt"
"math"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/Foxenfurter/foxAudioLib/foxAudioDecoder"
"github.com/Foxenfurter/foxAudioLib/foxAudioEncoder"
"github.com/Foxenfurter/foxAudioLib/foxNormalizer"
"github.com/Foxenfurter/foxAudioLib/foxResampler"
)
// ProcessAudio reads a WAV file, resamples it, and writes it to a target folder.
// ... [previous imports and code]
func ProcessAudio(inputFile string, outputFile string, targetSampleRate int, targetBitDepth int) error {
const functionName = "Main"
fmt.Printf("Testing something foxAudioLib \n")
startTime := time.Now()
// Decode the audio file
myDecoder := foxAudioDecoder.AudioDecoder{
Type: "WAV",
}
myDecoder.Filename = inputFile
err := myDecoder.Initialise()
if err != nil {
return fmt.Errorf("%s: decoder init failed: %v", functionName, err)
}
// Calculate new size using sample rate and bit depth ratios
sizeRatio := (float64(targetSampleRate) / float64(myDecoder.SampleRate)) *
(float64(targetBitDepth) / float64(myDecoder.BitDepth))
newSize := int64(float64(myDecoder.Size) * sizeRatio)
// Initialize Audio Encoder
myEncoder := foxAudioEncoder.AudioEncoder{
Type: "Wav",
SampleRate: targetSampleRate,
BitDepth: targetBitDepth, // Use targetBitDepth, not myDecoder.BitDepth
NumChannels: myDecoder.NumChannels,
Size: newSize,
Filename: outputFile,
}
fmt.Println("Test: Output file: ", myEncoder.Filename)
fmt.Println("Test: Creating new Encoder SampleRate: ", myEncoder.SampleRate, " Channels: ", myEncoder.NumChannels, " BitDepth: ", myEncoder.BitDepth)
err = myEncoder.Initialise()
if err != nil {
panic(err)
}
myResampler := foxResampler.NewResampler()
myResampler.FromSampleRate = myDecoder.SampleRate
myResampler.ToSampleRate = myEncoder.SampleRate
myResampler.Quality = 10
var WG sync.WaitGroup
DecodedSamplesChannel := make(chan [][]float64, 10000)
ResampledChannel := make(chan [][]float64, 10000)
fmt.Println("Test: Decoding Data...")
WG.Add(1)
go func() {
defer func() {
WG.Done()
// We are done so close the channel
//os.Stdin.Sync()
close(DecodedSamplesChannel)
}()
myDecoder.DecodeSamples(DecodedSamplesChannel, nil)
//close(DecodedSamplesChannel) // Close the channel after decoding
}()
WG.Add(1)
go func() {
defer WG.Done()
minGains := make([]float64, 0)
minGainLocations := make([]int, 0)
for samples := range DecodedSamplesChannel {
// Initialize to -1 to indicate no gain found yet
if len(minGains) == 0 {
minGains = make([]float64, len(samples))
minGainLocations = make([]int, len(samples))
for i := range minGainLocations {
minGainLocations[i] = -1
minGains[i] = 1.0
}
}
for channelIndex, channelSamples := range samples {
for sampleIndex, sample := range channelSamples {
sampleAbs := math.Abs(sample)
if sampleAbs < minGains[channelIndex] && sampleAbs != 0.0 {
minGains[channelIndex] = sampleAbs
minGainLocations[channelIndex] = sampleIndex
}
}
}
myResampler.InputSamples = samples
err := myResampler.Resample()
if err != nil {
fmt.Printf("Resampling failed: %v\n", err)
continue
}
ResampledChannel <- myResampler.InputSamples
}
fmt.Println("Max Gains:", minGains)
fmt.Println("Max Gain Locations:", minGainLocations)
close(ResampledChannel) // Close the channel after resampling
}()
fmt.Println("Test: Encoding Data...")
//WG.Add(1)
//go func() {
// defer WG.Done()
outputSamples := make([][]float64, myDecoder.NumChannels)
for i := range outputSamples {
outputSamples[i] = make([]float64, 0)
}
fmt.Println("Test: Structure built now build output Samples...")
for samples := range ResampledChannel {
for channelIdx, channelData := range samples {
outputSamples[channelIdx] = append(outputSamples[channelIdx], channelData...)
}
}
fmt.Println("Test: Ready to Normalize...")
targetLevel := 0.89
targetLevel = foxNormalizer.TargetGainCSharp(myDecoder.SampleRate, myEncoder.SampleRate, targetLevel)
fmt.Println("Test: Target Gain: ", targetLevel)
foxNormalizer.Normalize(outputSamples, targetLevel)
fmt.Println("Test: Outputting...")
myEncoder.EncodeData(outputSamples)
//}()
fmt.Println("Test: Waiting...")
WG.Wait()
//myDecoder.Close()
// Fix elapsedTime declaration order
elapsedTime := int(time.Since(startTime).Milliseconds())
println("\n============================================================================================")
println("AudioLib: Incremental (ms): ", elapsedTime)
println("============================================================================================\n")
return nil
} // <-- ProcessAudio ends here
func TestProcessAudio(t *testing.T) {
inputFiles := []string{
"C:\\temp\\InputFilters\\Cavern4Iloud_44k.wav",
"C:\\temp\\InputFilters\\Test_filter-44k.wav",
// "C:\\temp\\InputFilters\\Test_filter-96k.wav",
"C:\\temp\\InputFilters\\Opera_with_Sub_REW_20230303.wav",
"C:\\temp\\InputFilters\\iloudSubMini_48k.wav",
"C:\\temp\\InputFilters\\96000_Impulses_Cavern4Iloud.wav",
}
targetSampleRates := []int{96000, 44100, 48000, 192000, 88000}
for _, inputFile := range inputFiles {
for _, targetSampleRate := range targetSampleRates {
outputFile := "C:\\temp\\OutputFilters\\" + strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile)) + "_" + strconv.Itoa(targetSampleRate) + "k.wav"
err := ProcessAudio(inputFile, outputFile, targetSampleRate, 16)
if err != nil {
t.Fatalf("Test failed for %s at %d: %v", inputFile, targetSampleRate, err)
}
}
}
}