Skip to content

Commit

Permalink
Merge pull request #4 from pablodz/dev
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
pablodz authored Jan 11, 2023
2 parents 249012f + 1b47a51 commit c30e1e9
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 25 deletions.
71 changes: 71 additions & 0 deletions examples/bytesulaw2wav/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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, 1, 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)
}
}
2 changes: 0 additions & 2 deletions examples/linear_resampler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func main() {

// Open the input file
in, err := os.Open("./internal/samples/v1_16b_16000.wav")
if err != nil {
Expand Down Expand Up @@ -71,5 +70,4 @@ func main() {
if err != nil {
panic(err)
}

}
2 changes: 0 additions & 2 deletions examples/ulaw2wav_logpcm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func main() {

// Open the input file
in, err := os.Open("./internal/samples/recording.ulaw")
if err != nil {
Expand Down Expand Up @@ -71,5 +70,4 @@ func main() {
if err != nil {
panic(err)
}

}
2 changes: 0 additions & 2 deletions examples/ulaw2wav_lpcm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func main() {

// Open the input file
in, err := os.Open("./internal/samples/recording.ulaw")
if err != nil {
Expand Down Expand Up @@ -71,5 +70,4 @@ func main() {
if err != nil {
panic(err)
}

}
1 change: 0 additions & 1 deletion pkg/resampler/linear.go
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
15 changes: 12 additions & 3 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 All @@ -16,7 +17,6 @@ import (
)

func init() {

err := error(nil)
WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0)
if err != nil {
Expand All @@ -28,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)
Expand Down Expand Up @@ -143,11 +142,16 @@ func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (
}

return nil

}

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 +183,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
3 changes: 0 additions & 3 deletions pkg/transcoder/mulaw2wavlogpcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

func init() {

err := error(nil)
WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0)
if err != nil {
Expand All @@ -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)
Expand Down Expand Up @@ -141,5 +139,4 @@ func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder)
}

return nil

}
1 change: 0 additions & 1 deletion pkg/transcoder/resampling_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions pkg/transcoder/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ 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

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 All @@ -40,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

Expand Down
1 change: 0 additions & 1 deletion pkg/transcoder/transcoding_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions pkg/transcoder/wavlpcm2wavlpcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func wavLpcm2wavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) {

// read all the file
if transcoder.Verbose {
graphIn(in)
Expand Down Expand Up @@ -129,5 +128,4 @@ func wavLpcm2wavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder)
}

return nil

}
1 change: 0 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c30e1e9

Please sign in to comment.