Add ReformatWAV function for channel conversion

This commit is contained in:
Chigozirim Igweamaka 2025-04-01 17:57:25 +01:00
parent 8a918c74cd
commit 77e544ce41
2 changed files with 2 additions and 4 deletions

View file

@ -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))

View file

@ -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