mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-19 09:54:22 +00:00
Add ReformatWAV function for channel conversion
This commit is contained in:
parent
8a918c74cd
commit
77e544ce41
2 changed files with 2 additions and 4 deletions
|
|
@ -38,13 +38,11 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
||||||
switch bitsPerSample {
|
switch bitsPerSample {
|
||||||
case 8:
|
case 8:
|
||||||
for _, sample := range data {
|
for _, sample := range data {
|
||||||
// Convert float to 8-bit unsigned integer
|
|
||||||
val := uint8((sample + 1.0) * 127.5)
|
val := uint8((sample + 1.0) * 127.5)
|
||||||
byteData = append(byteData, byte(val))
|
byteData = append(byteData, byte(val))
|
||||||
}
|
}
|
||||||
case 16:
|
case 16:
|
||||||
for _, sample := range data {
|
for _, sample := range data {
|
||||||
// Convert float to 16-bit signed integer
|
|
||||||
val := int16(sample * 32767.0)
|
val := int16(sample * 32767.0)
|
||||||
buf := make([]byte, 2)
|
buf := make([]byte, 2)
|
||||||
binary.LittleEndian.PutUint16(buf, uint16(val))
|
binary.LittleEndian.PutUint16(buf, uint16(val))
|
||||||
|
|
@ -52,7 +50,6 @@ func FloatsToBytes(data []float64, bitsPerSample int) ([]byte, error) {
|
||||||
}
|
}
|
||||||
case 24:
|
case 24:
|
||||||
for _, sample := range data {
|
for _, sample := range data {
|
||||||
// Convert float to 24-bit signed integer
|
|
||||||
val := int32(sample * 8388607.0)
|
val := int32(sample * 8388607.0)
|
||||||
buf := make([]byte, 4)
|
buf := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(buf, uint32(val)<<8) // Shift by 8 bits to fit 24-bit
|
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:
|
case 32:
|
||||||
for _, sample := range data {
|
for _, sample := range data {
|
||||||
// Convert float to 32-bit signed integer
|
|
||||||
val := int32(sample * 2147483647.0)
|
val := int32(sample * 2147483647.0)
|
||||||
buf := make([]byte, 4)
|
buf := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(buf, uint32(val))
|
binary.LittleEndian.PutUint32(buf, uint32(val))
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ func ConvertToWAV(inputFilePath string, channels int) (wavFilePath string, err e
|
||||||
return outputFile, nil
|
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) {
|
func ReformatWAV(inputFilePath string, channels int) (reformatedFilePath string, errr error) {
|
||||||
if channels < 1 || channels > 2 {
|
if channels < 1 || channels > 2 {
|
||||||
channels = 1
|
channels = 1
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue