Skip to content

Commit 7045053

Browse files
committed
fix audio visualizer sign issue
1 parent 824ddd7 commit 7045053

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

Audio.Visualizer.Wpf/AudioVisualizer.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ private void Process(AudioBuffer buffer, Envelope envelope) {
105105
while (loc < buffer.Data.Length) {
106106
for (var i = 0; i < drawingSampleBuffer_Channels; i++) {
107107
var sample = ReadOneSample(buffer, ref loc);
108-
accumulateBuffer[i] += sample;
108+
accumulateBuffer[i] += Math.Abs(sample);
109109
}
110110
accumulatedSampleCount++;
111111
if (accumulatedSampleCount >= numSamplesForEachPixel) {
112112
var index = drawingSampleBuffer_Count % drawingSampleBuffer_Length;
113113
for (var i = 0; i < drawingSampleBuffer_Channels; i++) {
114114
var mean = accumulateBuffer[i] / accumulatedSampleCount;
115-
var val = Math.Max(0, mean);
115+
var val = Math.Max(0, Math.Min(1, mean));
116116
drawingSampleBuffer[index, i] = val;
117117
}
118118
drawingSampleBuffer_Count++;
@@ -131,7 +131,7 @@ private static float ReadOneSample(AudioBuffer buffer, ref int location) {
131131
case WaveFormatTag.WAVE_FORMAT_IEEE_FLOAT:
132132
Debug.Assert(buffer.Format.BitsPerSample == 8 * sizeof(float));
133133
result = BitConverter.ToSingle(buffer.Data, location);
134-
Debug.Assert(0 <= result && result <= 1);
134+
Debug.Assert(-1 <= result && result <= 1);
135135
location += sizeof(float);
136136
return result;
137137
case WaveFormatTag.WAVE_FORMAT_PCM:
@@ -140,15 +140,11 @@ private static float ReadOneSample(AudioBuffer buffer, ref int location) {
140140
result = (float)buffer.Data[location] / byte.MaxValue;
141141
location += sizeof(byte);
142142
return result;
143-
case 16:
144-
var s = BitConverter.ToUInt16(buffer.Data, location);
145-
result = (float)s / ushort.MaxValue;
143+
case 16://signed
144+
var s = BitConverter.ToInt16(buffer.Data, location);
145+
result = (float)s / short.MaxValue;
146146
location += sizeof(ushort);
147147
return result;
148-
case 32:
149-
result = (float)BitConverter.ToUInt32(buffer.Data, location) / uint.MaxValue;
150-
location += sizeof(uint);
151-
return result;
152148
default:
153149
throw new NotSupportedException($"Audio visualizer does not support calculating audio with {buffer.Format.BitsPerSample} per sample");
154150
}

0 commit comments

Comments
 (0)