Skip to content

Commit

Permalink
support 4 bytes streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodz committed Jan 11, 2023
1 parent 1423257 commit cd2f13d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
73 changes: 73 additions & 0 deletions examples/bytesulaw2wav/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
12 changes: 12 additions & 0 deletions pkg/transcoder/mulaw2wav.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transcoder

import (
"bufio"
"bytes"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/transcoder/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit cd2f13d

Please sign in to comment.