From 77e544ce4121810cb347d4d6ce2e5507cde3550c Mon Sep 17 00:00:00 2001 From: Chigozirim Igweamaka Date: Tue, 1 Apr 2025 17:57:25 +0100 Subject: [PATCH] Add ReformatWAV function for channel conversion --- utils/helpers.go | 4 ---- wav/convert.go | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/helpers.go b/utils/helpers.go index cd7ceb0..98c1e56 100644 --- a/utils/helpers.go +++ b/utils/helpers.go @@ -38,13 +38,11 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) { switch bitsPerSample { case 8: for _, sample := range data { - // Convert float to 8-bit unsigned integer val := uint8((sample + 1.0) * 127.5) byteData = append(byteData, byte(val)) } case 16: for _, sample := range data { - // Convert float to 16-bit signed integer val := int16(sample * 32767.0) buf := make([]byte, 2) binary.LittleEndian.PutUint16(buf, uint16(val)) @@ -52,7 +50,6 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) { } case 24: for _, sample := range data { - // Convert float to 24-bit signed integer val := int32(sample * 8388607.0) buf := make([]byte, 4) binary.LittleEndian.PutUint32(buf, uint32(val)<<8) // Shift by 8 bits to fit 24-bit @@ -60,7 +57,6 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) { } case 32: for _, sample := range data { - // Convert float to 32-bit signed integer val := int32(sample * 2147483647.0) buf := make([]byte, 4) binary.LittleEndian.PutUint32(buf, uint32(val)) diff --git a/wav/convert.go b/wav/convert.go index aa5366b..1c815ba 100644 --- a/wav/convert.go +++ b/wav/convert.go @@ -51,6 +51,8 @@ func ConvertToWAV(inputFilePath string, channels int) (wavFilePath string, err e return outputFile, nil } +// ReformatWAV converts a given WAV file to the specified number of channels, +// either mono (1 channel) or stereo (2 channels). func ReformatWAV(inputFilePath string, channels int) (reformatedFilePath string, errr error) { if channels < 1 || channels > 2 { channels = 1