Skip to content

Commit c30e1e9

Browse files
authored
Merge pull request #4 from pablodz/dev
fix
2 parents 249012f + 1b47a51 commit c30e1e9

File tree

12 files changed

+91
-25
lines changed

12 files changed

+91
-25
lines changed

examples/bytesulaw2wav/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
7+
"github.com/pablodz/sopro/pkg/audioconfig"
8+
"github.com/pablodz/sopro/pkg/cpuarch"
9+
"github.com/pablodz/sopro/pkg/encoding"
10+
"github.com/pablodz/sopro/pkg/fileformat"
11+
"github.com/pablodz/sopro/pkg/method"
12+
"github.com/pablodz/sopro/pkg/transcoder"
13+
)
14+
15+
func main() {
16+
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}
17+
// Open the input file
18+
in := bytes.NewBuffer(data)
19+
20+
// Create the output file
21+
out, err := os.Create("./internal/samples/output.wav")
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer out.Close()
26+
27+
// create a transcoder
28+
t := &transcoder.Transcoder{
29+
MethodT: method.BIT_LOOKUP_TABLE,
30+
SourceConfigs: transcoder.TranscoderAudioConfig{
31+
Endianness: cpuarch.LITTLE_ENDIAN,
32+
},
33+
TargetConfigs: transcoder.TranscoderAudioConfig{
34+
Endianness: cpuarch.LITTLE_ENDIAN,
35+
},
36+
SizeBuffer: 1024,
37+
Verbose: true,
38+
}
39+
40+
// Transcode the file
41+
err = t.Mulaw2Wav(
42+
&transcoder.AudioFileIn{
43+
Data: in,
44+
AudioFileGeneral: transcoder.AudioFileGeneral{
45+
Format: fileformat.AUDIO_MULAW,
46+
Config: audioconfig.MulawConfig{
47+
BitDepth: 8,
48+
Channels: 1,
49+
Encoding: encoding.SPACE_LOGARITHMIC, // ulaw is logarithmic
50+
SampleRate: 8000,
51+
},
52+
},
53+
},
54+
&transcoder.AudioFileOut{
55+
Data: out,
56+
AudioFileGeneral: transcoder.AudioFileGeneral{
57+
Format: fileformat.AUDIO_WAV,
58+
Config: audioconfig.WavConfig{
59+
BitDepth: 8,
60+
Channels: 1,
61+
Encoding: encoding.SPACE_LOGARITHMIC,
62+
SampleRate: 8000,
63+
},
64+
},
65+
},
66+
)
67+
68+
if err != nil {
69+
panic(err)
70+
}
71+
}

examples/linear_resampler/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
func main() {
15-
1615
// Open the input file
1716
in, err := os.Open("./internal/samples/v1_16b_16000.wav")
1817
if err != nil {
@@ -71,5 +70,4 @@ func main() {
7170
if err != nil {
7271
panic(err)
7372
}
74-
7573
}

examples/ulaw2wav_logpcm/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
func main() {
15-
1615
// Open the input file
1716
in, err := os.Open("./internal/samples/recording.ulaw")
1817
if err != nil {
@@ -71,5 +70,4 @@ func main() {
7170
if err != nil {
7271
panic(err)
7372
}
74-
7573
}

examples/ulaw2wav_lpcm/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
func main() {
15-
1615
// Open the input file
1716
in, err := os.Open("./internal/samples/recording.ulaw")
1817
if err != nil {
@@ -71,5 +70,4 @@ func main() {
7170
if err != nil {
7271
panic(err)
7372
}
74-
7573
}

pkg/resampler/linear.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package resampler
22

33
func LinearInterpolation[T int16 | int32 | int64 | int | byte](data []T, ratio float64) ([]T, error) {
4-
54
// Calculate the length of the resampled data slice.
65
resampledLength := int(float64(len(data)) / ratio)
76

pkg/transcoder/mulaw2wav.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package transcoder
22

33
import (
44
"bufio"
5+
"bytes"
56
"fmt"
67
"io"
78
"log"
@@ -16,7 +17,6 @@ import (
1617
)
1718

1819
func init() {
19-
2020
err := error(nil)
2121
WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0)
2222
if err != nil {
@@ -28,7 +28,6 @@ func init() {
2828
// https://raw.githubusercontent.com/corkami/pics/master/binary/WAV.png
2929
// http://www.topherlee.com/software/pcm-tut-wavformat.html
3030
func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) {
31-
3231
// read all the file
3332
if transcoder.Verbose {
3433
graphIn(in)
@@ -143,11 +142,16 @@ func mulaw2WavLpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (
143142
}
144143

145144
return nil
146-
147145
}
148146

149147
func graphIn(in *AudioFileIn) {
150148
log.Println("[WARNING] Reading the whole file into memory. This may take a while...")
149+
// check if in is *bytes.Buffer
150+
if _, ok := in.Data.(*bytes.Buffer); ok {
151+
log.Println("Input file is a bytes.Buffer")
152+
return
153+
}
154+
151155
// make an independent copy of the file
152156
file := in.Data.(*os.File)
153157
f, err := os.Open(file.Name())
@@ -179,6 +183,11 @@ func graphIn(in *AudioFileIn) {
179183

180184
func graphOut(in *AudioFileIn, out *AudioFileOut) {
181185
log.Println("[WARNING] Reading the whole file into memory. This may take a while...")
186+
// check if in is *bytes.Buffer
187+
if _, ok := in.Data.(*bytes.Buffer); ok {
188+
log.Println("Input file is a bytes.Buffer")
189+
return
190+
}
182191
file := in.Data.(*os.File)
183192
f, err := os.Open(file.Name())
184193
if err != nil {

pkg/transcoder/mulaw2wavlogpcm.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
)
1515

1616
func init() {
17-
1817
err := error(nil)
1918
WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0)
2019
if err != nil {
@@ -26,7 +25,6 @@ func init() {
2625
// https://raw.githubusercontent.com/corkami/pics/master/binary/WAV.png
2726
// http://www.topherlee.com/software/pcm-tut-wavformat.html
2827
func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) {
29-
3028
// read all the file
3129
if transcoder.Verbose {
3230
graphIn(in)
@@ -141,5 +139,4 @@ func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder)
141139
}
142140

143141
return nil
144-
145142
}

pkg/transcoder/resampling_general.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
var doOnceResampling sync.Once
1313

1414
func ResampleBytes(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) error {
15-
1615
bitsProcessed, err := differentSampleRate(in, out, transcoder)
1716
if err != nil {
1817
return err

pkg/transcoder/router.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ import (
99
"github.com/pablodz/sopro/pkg/resampler"
1010
)
1111

12-
var WIDTH_TERMINAL = 80
13-
var HEIGHT_TERMINAL = 30
12+
var (
13+
WIDTH_TERMINAL = 80
14+
HEIGHT_TERMINAL = 30
15+
)
1416

1517
const ErrUnsupportedConversion = "unsupported conversion"
1618

1719
func (t *Transcoder) Mulaw2Wav(in *AudioFileIn, out *AudioFileOut) error {
18-
1920
inSpace := in.Config.(audioconfig.MulawConfig).Encoding
2021
outSpace := out.Config.(audioconfig.WavConfig).Encoding
2122

2223
switch {
23-
case t.MethodT != method.BIT_LOOKUP_TABLE &&
24+
case t.MethodT == method.BIT_LOOKUP_TABLE &&
2425
inSpace == encoding.SPACE_LOGARITHMIC &&
2526
outSpace == encoding.SPACE_LINEAR:
2627
return mulaw2WavLpcm(in, out, t)
27-
case t.MethodT != method.BIT_LOOKUP_TABLE &&
28+
case t.MethodT == method.BIT_LOOKUP_TABLE &&
2829
inSpace == encoding.SPACE_LOGARITHMIC &&
2930
outSpace == encoding.SPACE_LOGARITHMIC:
3031
return mulaw2WavLogpcm(in, out, t)
3132
default:
3233
return fmt.Errorf(
33-
"%s: %s -> %s",
34+
"[%s] %s: %s -> %s",
35+
method.METHODS[t.MethodT],
3436
ErrUnsupportedConversion,
3537
encoding.ENCODINGS[inSpace],
3638
encoding.ENCODINGS[outSpace],
@@ -40,7 +42,6 @@ func (t *Transcoder) Mulaw2Wav(in *AudioFileIn, out *AudioFileOut) error {
4042
}
4143

4244
func (t *Transcoder) Wav2Wav(in *AudioFileIn, out *AudioFileOut) error {
43-
4445
inSpace := in.Config.(audioconfig.WavConfig).Encoding
4546
outSpace := out.Config.(audioconfig.WavConfig).Encoding
4647

pkg/transcoder/transcoding_general.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
var doOnceTranscoding sync.Once
1313

1414
func TranscodeBytes(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) error {
15-
1615
equalEncod := (transcoder.SourceConfigs.Encoding == transcoder.TargetConfigs.Encoding)
1716
bitsProcessed := 0
1817
err := error(nil)

0 commit comments

Comments
 (0)