-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
442 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mulaw | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/sopro-dev/sopro-core/audio" | ||
"github.com/sopro-dev/sopro-core/audio/utils" | ||
) | ||
|
||
type MuLawFormat struct{} | ||
|
||
func (f *MuLawFormat) Decode(data []byte, info audio.AudioInfo) []float64 { | ||
|
||
pcmData := make([]float64, len(data)) | ||
for i := 0; i < len(data); i++ { | ||
pcmData[i] = float64(utils.DecodeFromULaw(data[i])) | ||
} | ||
|
||
log.Println("[Bytes][Org]", data[0:100]) | ||
log.Println("[Bytes][Pcm]", pcmData[0:100]) | ||
|
||
return pcmData | ||
} | ||
|
||
func (f *MuLawFormat) Encode(audioData []float64, info audio.AudioInfo) []byte { | ||
log.Printf("Not implemented") | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package audio | ||
|
||
import "testing" | ||
|
||
func TestValidateAudioInfo(t *testing.T) { | ||
// Test case 1: Valid audio info | ||
info := AudioInfo{BitDepth: 16, Channels: 2, SampleRate: 44100} | ||
err := validateAudioInfo(info) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
// Test case 2: Invalid bit depth | ||
info = AudioInfo{BitDepth: 5, Channels: 2, SampleRate: 44100} | ||
err = validateAudioInfo(info) | ||
if err == nil { | ||
t.Error("Expected an error for invalid bit depth") | ||
} | ||
if err != errInvalidBitDepth { | ||
t.Errorf("Expected error %v, got %v", errInvalidBitDepth, err) | ||
} | ||
|
||
// Test case 3: Invalid number of channels | ||
info = AudioInfo{BitDepth: 16, Channels: 0, SampleRate: 44100} | ||
err = validateAudioInfo(info) | ||
if err == nil { | ||
t.Error("Expected an error for invalid number of channels") | ||
} | ||
if err != errInvalidNumChannels { | ||
t.Errorf("Expected error %v, got %v", errInvalidNumChannels, err) | ||
} | ||
|
||
// Test case 4: Invalid sample rate | ||
info = AudioInfo{BitDepth: 16, Channels: 2, SampleRate: 0} | ||
err = validateAudioInfo(info) | ||
if err == nil { | ||
t.Error("Expected an error for invalid sample rate") | ||
} | ||
if err != errInvalidSampleRate { | ||
t.Errorf("Expected error %v, got %v", errInvalidSampleRate, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/binary" | ||
) | ||
|
||
// MergeSliceOfBytes merges multiple slices of bytes into a single slice | ||
func MergeSliceOfBytes(slices ...[]byte) []byte { | ||
var result []byte | ||
for _, s := range slices { | ||
result = append(result, s...) | ||
} | ||
return result | ||
} | ||
|
||
// IntToBytes converts an unsigned integer to a little-endian byte slice | ||
func IntToBytes(i interface{}) []byte { | ||
switch v := i.(type) { | ||
case uint32: | ||
buf := make([]byte, 4) | ||
binary.LittleEndian.PutUint32(buf, v) | ||
return buf | ||
case uint16: | ||
buf := make([]byte, 2) | ||
binary.LittleEndian.PutUint16(buf, v) | ||
return buf | ||
default: | ||
|
||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestMergeSliceOfBytes(t *testing.T) { | ||
slices := [][]byte{ | ||
[]byte("hello"), | ||
[]byte(" "), | ||
[]byte("world"), | ||
[]byte(""), | ||
} | ||
expected := []byte("hello world") | ||
|
||
result := MergeSliceOfBytes(slices...) | ||
|
||
if !bytes.Equal(result, expected) { | ||
t.Errorf("Expected %s but got %s", expected, result) | ||
} | ||
} | ||
|
||
func TestIntToBytes(t *testing.T) { | ||
tests := []struct { | ||
input interface{} | ||
expected []byte | ||
}{ | ||
{uint32(123456), []byte{64, 226, 1, 0}}, // 123456 in little-endian bytes | ||
{uint16(0), []byte{0, 0}}, // 0 in little-endian bytes | ||
{uint16(65535), []byte{255, 255}}, // 65535 in little-endian bytes | ||
{uint16(1), []byte{1, 0}}, // 65536 in little-endian bytes | ||
{uint32(2084), []byte{24, 8, 0, 0}}, // 65536 in little-endian bytes | ||
// Add more test cases as needed | ||
} | ||
|
||
for _, test := range tests { | ||
result := IntToBytes(test.input) | ||
if !bytesEqual(result, test.expected) { | ||
t.Errorf("For input %v, expected %v, but got %v", test.input, test.expected, result) | ||
} | ||
} | ||
} | ||
|
||
// bytesEqual compares two byte slices for equality | ||
func bytesEqual(a, b []byte) bool { | ||
return len(a) == len(b) && (len(a) == 0 || (a[0] == b[0] && bytesEqual(a[1:], b[1:]))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package utils | ||
|
||
// ulawDecode is a lookup table for u-law to LPCM | ||
var ulawDecode = [256]int16{ | ||
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, | ||
-23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, | ||
-15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, | ||
-11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, | ||
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, | ||
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, | ||
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, | ||
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, | ||
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, | ||
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924, | ||
-876, -844, -812, -780, -748, -716, -684, -652, | ||
-620, -588, -556, -524, -492, -460, -428, -396, | ||
-372, -356, -340, -324, -308, -292, -276, -260, | ||
-244, -228, -212, -196, -180, -164, -148, -132, | ||
-120, -112, -104, -96, -88, -80, -72, -64, | ||
-56, -48, -40, -32, -24, -16, -8, 0, | ||
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, | ||
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, | ||
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, | ||
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, | ||
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, | ||
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, | ||
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, | ||
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, | ||
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, | ||
1372, 1308, 1244, 1180, 1116, 1052, 988, 924, | ||
876, 844, 812, 780, 748, 716, 684, 652, | ||
620, 588, 556, 524, 492, 460, 428, 396, | ||
372, 356, 340, 324, 308, 292, 276, 260, | ||
244, 228, 212, 196, 180, 164, 148, 132, | ||
120, 112, 104, 96, 88, 80, 72, 64, | ||
56, 48, 40, 32, 24, 16, 8, 0, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,6 @@ | ||
package utils | ||
|
||
func DecodeFromULaw(uLaw byte) int16 { | ||
uLaw = ^uLaw | ||
sign := int16(1) | ||
if uLaw&0x80 != 0 { | ||
sign = -1 | ||
} | ||
exponent := int((uLaw >> 4) & 0x07) | ||
mantissa := int(uLaw&0x0F) + 16 | ||
return sign * int16(mantissa) << uint(exponent+3) | ||
} | ||
|
||
func EncodeToULaw(sample int16) byte { | ||
sign := byte(0) | ||
if sample < 0 { | ||
sign = 0x80 | ||
sample = -sample | ||
} | ||
|
||
sample = sample + 33 | ||
if sample > 0x7FFF { | ||
sample = 0x7FFF | ||
} | ||
|
||
exponent := byte(7) | ||
for (sample & 0x4000) == 0 { | ||
exponent-- | ||
sample <<= 1 | ||
} | ||
|
||
mantissa := byte(sample >> 6) | ||
return ^byte(sign | (exponent << 4) | mantissa) | ||
pcm := ulawDecode[uLaw] | ||
return int16(pcm) | ||
} |
Oops, something went wrong.