From cd2f13da3080276cd50582b6866077db80aac98d Mon Sep 17 00:00:00 2001 From: Pablo Diaz Date: Tue, 10 Jan 2023 20:24:48 -0400 Subject: [PATCH 1/2] support 4 bytes streaming --- examples/bytesulaw2wav/main.go | 73 ++++++++++++++++++++++++++++++++++ pkg/transcoder/mulaw2wav.go | 12 ++++++ pkg/transcoder/router.go | 7 ++-- 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 examples/bytesulaw2wav/main.go diff --git a/examples/bytesulaw2wav/main.go b/examples/bytesulaw2wav/main.go new file mode 100644 index 0000000..9401de2 --- /dev/null +++ b/examples/bytesulaw2wav/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "bytes" + "os" + + "github.com/pablodz/sopro/pkg/audioconfig" + "github.com/pablodz/sopro/pkg/cpuarch" + "github.com/pablodz/sopro/pkg/encoding" + "github.com/pablodz/sopro/pkg/fileformat" + "github.com/pablodz/sopro/pkg/method" + "github.com/pablodz/sopro/pkg/transcoder" +) + +func main() { + + data := []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 4, 2, 3, 3, 5, 01, 7, 8, 1, 4, 0} + // Open the input file + in := bytes.NewBuffer(data) + + // Create the output file + out, err := os.Create("./internal/samples/output.wav") + if err != nil { + panic(err) + } + defer out.Close() + + // create a transcoder + t := &transcoder.Transcoder{ + MethodT: method.BIT_LOOKUP_TABLE, + SourceConfigs: transcoder.TranscoderAudioConfig{ + Endianness: cpuarch.LITTLE_ENDIAN, + }, + TargetConfigs: transcoder.TranscoderAudioConfig{ + Endianness: cpuarch.LITTLE_ENDIAN, + }, + SizeBuffer: 1024, + Verbose: true, + } + + // Transcode the file + err = t.Mulaw2Wav( + &transcoder.AudioFileIn{ + Data: in, + AudioFileGeneral: transcoder.AudioFileGeneral{ + Format: fileformat.AUDIO_MULAW, + Config: audioconfig.MulawConfig{ + BitDepth: 8, + Channels: 1, + Encoding: encoding.SPACE_LOGARITHMIC, // ulaw is logarithmic + SampleRate: 8000, + }, + }, + }, + &transcoder.AudioFileOut{ + Data: out, + AudioFileGeneral: transcoder.AudioFileGeneral{ + Format: fileformat.AUDIO_WAV, + Config: audioconfig.WavConfig{ + BitDepth: 8, + Channels: 1, + Encoding: encoding.SPACE_LOGARITHMIC, + SampleRate: 8000, + }, + }, + }, + ) + + if err != nil { + panic(err) + } + +} diff --git a/pkg/transcoder/mulaw2wav.go b/pkg/transcoder/mulaw2wav.go index eb44a36..8ae87ef 100644 --- a/pkg/transcoder/mulaw2wav.go +++ b/pkg/transcoder/mulaw2wav.go @@ -2,6 +2,7 @@ package transcoder import ( "bufio" + "bytes" "fmt" "io" "log" @@ -148,6 +149,12 @@ func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) ( func graphIn(in *AudioFileIn) { log.Println("[WARNING] Reading the whole file into memory. This may take a while...") + // check if in is *bytes.Buffer + if _, ok := in.Data.(*bytes.Buffer); ok { + log.Println("Input file is a bytes.Buffer") + return + } + // make an independent copy of the file file := in.Data.(*os.File) f, err := os.Open(file.Name()) @@ -179,6 +186,11 @@ func graphIn(in *AudioFileIn) { func graphOut(in *AudioFileIn, out *AudioFileOut) { log.Println("[WARNING] Reading the whole file into memory. This may take a while...") + // check if in is *bytes.Buffer + if _, ok := in.Data.(*bytes.Buffer); ok { + log.Println("Input file is a bytes.Buffer") + return + } file := in.Data.(*os.File) f, err := os.Open(file.Name()) if err != nil { diff --git a/pkg/transcoder/router.go b/pkg/transcoder/router.go index 316e930..6853f13 100644 --- a/pkg/transcoder/router.go +++ b/pkg/transcoder/router.go @@ -20,17 +20,18 @@ func (t *Transcoder) Mulaw2Wav(in *AudioFileIn, out *AudioFileOut) error { outSpace := out.Config.(audioconfig.WavConfig).Encoding switch { - case t.MethodT != method.BIT_LOOKUP_TABLE && + case t.MethodT == method.BIT_LOOKUP_TABLE && inSpace == encoding.SPACE_LOGARITHMIC && outSpace == encoding.SPACE_LINEAR: return mulaw2WavLpcm(in, out, t) - case t.MethodT != method.BIT_LOOKUP_TABLE && + case t.MethodT == method.BIT_LOOKUP_TABLE && inSpace == encoding.SPACE_LOGARITHMIC && outSpace == encoding.SPACE_LOGARITHMIC: return mulaw2WavLogpcm(in, out, t) default: return fmt.Errorf( - "%s: %s -> %s", + "[%s] %s: %s -> %s", + method.METHODS[t.MethodT], ErrUnsupportedConversion, encoding.ENCODINGS[inSpace], encoding.ENCODINGS[outSpace], From 1b47a514c0fd6e613cac1c49df82c64d3f2a79dd Mon Sep 17 00:00:00 2001 From: Pablo Diaz Date: Tue, 10 Jan 2023 20:34:13 -0400 Subject: [PATCH 2/2] gofumpt -l -w . --- examples/bytesulaw2wav/main.go | 4 +--- examples/linear_resampler/main.go | 2 -- examples/ulaw2wav_logpcm/main.go | 2 -- examples/ulaw2wav_lpcm/main.go | 2 -- pkg/resampler/linear.go | 1 - pkg/transcoder/mulaw2wav.go | 3 --- pkg/transcoder/mulaw2wavlogpcm.go | 3 --- pkg/transcoder/resampling_general.go | 1 - pkg/transcoder/router.go | 8 ++++---- pkg/transcoder/transcoding_general.go | 1 - pkg/transcoder/wavlpcm2wavlpcm.go | 2 -- utils/utils.go | 1 - 12 files changed, 5 insertions(+), 25 deletions(-) diff --git a/examples/bytesulaw2wav/main.go b/examples/bytesulaw2wav/main.go index 9401de2..dfecc78 100644 --- a/examples/bytesulaw2wav/main.go +++ b/examples/bytesulaw2wav/main.go @@ -13,8 +13,7 @@ import ( ) func main() { - - data := []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 4, 2, 3, 3, 5, 01, 7, 8, 1, 4, 0} + data := []byte{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 4, 2, 3, 3, 5, 1, 7, 8, 1, 4, 0} // Open the input file in := bytes.NewBuffer(data) @@ -69,5 +68,4 @@ func main() { if err != nil { panic(err) } - } diff --git a/examples/linear_resampler/main.go b/examples/linear_resampler/main.go index c4fcff4..cff86bd 100644 --- a/examples/linear_resampler/main.go +++ b/examples/linear_resampler/main.go @@ -12,7 +12,6 @@ import ( ) func main() { - // Open the input file in, err := os.Open("./internal/samples/v1_16b_16000.wav") if err != nil { @@ -71,5 +70,4 @@ func main() { if err != nil { panic(err) } - } diff --git a/examples/ulaw2wav_logpcm/main.go b/examples/ulaw2wav_logpcm/main.go index 74505f1..9270700 100644 --- a/examples/ulaw2wav_logpcm/main.go +++ b/examples/ulaw2wav_logpcm/main.go @@ -12,7 +12,6 @@ import ( ) func main() { - // Open the input file in, err := os.Open("./internal/samples/recording.ulaw") if err != nil { @@ -71,5 +70,4 @@ func main() { if err != nil { panic(err) } - } diff --git a/examples/ulaw2wav_lpcm/main.go b/examples/ulaw2wav_lpcm/main.go index 7106a39..fa60b92 100644 --- a/examples/ulaw2wav_lpcm/main.go +++ b/examples/ulaw2wav_lpcm/main.go @@ -12,7 +12,6 @@ import ( ) func main() { - // Open the input file in, err := os.Open("./internal/samples/recording.ulaw") if err != nil { @@ -71,5 +70,4 @@ func main() { if err != nil { panic(err) } - } diff --git a/pkg/resampler/linear.go b/pkg/resampler/linear.go index 620642a..fbb6b5d 100644 --- a/pkg/resampler/linear.go +++ b/pkg/resampler/linear.go @@ -1,7 +1,6 @@ package resampler func LinearInterpolation[T int16 | int32 | int64 | int | byte](data []T, ratio float64) ([]T, error) { - // Calculate the length of the resampled data slice. resampledLength := int(float64(len(data)) / ratio) diff --git a/pkg/transcoder/mulaw2wav.go b/pkg/transcoder/mulaw2wav.go index 8ae87ef..df959b3 100644 --- a/pkg/transcoder/mulaw2wav.go +++ b/pkg/transcoder/mulaw2wav.go @@ -17,7 +17,6 @@ import ( ) func init() { - err := error(nil) WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0) if err != nil { @@ -29,7 +28,6 @@ func init() { // https://raw.githubusercontent.com/corkami/pics/master/binary/WAV.png // http://www.topherlee.com/software/pcm-tut-wavformat.html func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) { - // read all the file if transcoder.Verbose { graphIn(in) @@ -144,7 +142,6 @@ func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) ( } return nil - } func graphIn(in *AudioFileIn) { diff --git a/pkg/transcoder/mulaw2wavlogpcm.go b/pkg/transcoder/mulaw2wavlogpcm.go index fcadad0..528e1d3 100644 --- a/pkg/transcoder/mulaw2wavlogpcm.go +++ b/pkg/transcoder/mulaw2wavlogpcm.go @@ -14,7 +14,6 @@ import ( ) func init() { - err := error(nil) WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0) if err != nil { @@ -26,7 +25,6 @@ func init() { // https://raw.githubusercontent.com/corkami/pics/master/binary/WAV.png // http://www.topherlee.com/software/pcm-tut-wavformat.html func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) { - // read all the file if transcoder.Verbose { graphIn(in) @@ -141,5 +139,4 @@ func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) } return nil - } diff --git a/pkg/transcoder/resampling_general.go b/pkg/transcoder/resampling_general.go index c036c36..d8b489c 100644 --- a/pkg/transcoder/resampling_general.go +++ b/pkg/transcoder/resampling_general.go @@ -12,7 +12,6 @@ import ( var doOnceResampling sync.Once func ResampleBytes(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) error { - bitsProcessed, err := differentSampleRate(in, out, transcoder) if err != nil { return err diff --git a/pkg/transcoder/router.go b/pkg/transcoder/router.go index 6853f13..3bdd7fe 100644 --- a/pkg/transcoder/router.go +++ b/pkg/transcoder/router.go @@ -9,13 +9,14 @@ import ( "github.com/pablodz/sopro/pkg/resampler" ) -var WIDTH_TERMINAL = 80 -var HEIGHT_TERMINAL = 30 +var ( + WIDTH_TERMINAL = 80 + HEIGHT_TERMINAL = 30 +) const ErrUnsupportedConversion = "unsupported conversion" func (t *Transcoder) Mulaw2Wav(in *AudioFileIn, out *AudioFileOut) error { - inSpace := in.Config.(audioconfig.MulawConfig).Encoding outSpace := out.Config.(audioconfig.WavConfig).Encoding @@ -41,7 +42,6 @@ func (t *Transcoder) Mulaw2Wav(in *AudioFileIn, out *AudioFileOut) error { } func (t *Transcoder) Wav2Wav(in *AudioFileIn, out *AudioFileOut) error { - inSpace := in.Config.(audioconfig.WavConfig).Encoding outSpace := out.Config.(audioconfig.WavConfig).Encoding diff --git a/pkg/transcoder/transcoding_general.go b/pkg/transcoder/transcoding_general.go index c81a722..a853dd9 100644 --- a/pkg/transcoder/transcoding_general.go +++ b/pkg/transcoder/transcoding_general.go @@ -12,7 +12,6 @@ import ( var doOnceTranscoding sync.Once func TranscodeBytes(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) error { - equalEncod := (transcoder.SourceConfigs.Encoding == transcoder.TargetConfigs.Encoding) bitsProcessed := 0 err := error(nil) diff --git a/pkg/transcoder/wavlpcm2wavlpcm.go b/pkg/transcoder/wavlpcm2wavlpcm.go index 5af130f..797b847 100644 --- a/pkg/transcoder/wavlpcm2wavlpcm.go +++ b/pkg/transcoder/wavlpcm2wavlpcm.go @@ -12,7 +12,6 @@ import ( ) func wavLpcm2wavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) { - // read all the file if transcoder.Verbose { graphIn(in) @@ -129,5 +128,4 @@ func wavLpcm2wavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) } return nil - } diff --git a/utils/utils.go b/utils/utils.go index 32bb338..e44d57a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -30,7 +30,6 @@ func PrintGraphInt16(items []int16) { fmt.Println("MaxX: ", len(stringSlice)) fmt.Println("DeltaX: ", (len(stringSlice) - 0)) fmt.Println() - } func PrintTableInt16(items []int16, numPerRow int) {