|
| 1 | +package transcoder |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/pablodz/sopro/pkg/audioconfig" |
| 11 | + "github.com/pablodz/sopro/pkg/cpuarch" |
| 12 | + "github.com/pablodz/sopro/pkg/encoding" |
| 13 | + "golang.org/x/term" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + |
| 18 | + err := error(nil) |
| 19 | + WIDTH_TERMINAL, HEIGHT_TERMINAL, err = term.GetSize(0) |
| 20 | + if err != nil { |
| 21 | + log.Fatal(err) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// TODO: split functions for different sizes of files |
| 26 | +// Transcode an ulaw file to a wav file (large files supported) |
| 27 | +// https://raw.githubusercontent.com/corkami/pics/master/binary/WAV.png |
| 28 | +// http://www.topherlee.com/software/pcm-tut-wavformat.html |
| 29 | +func mulaw2WavLogpcm(in *AudioFileIn, out *AudioFileOut, transcoder *Transcoder) (err error) { |
| 30 | + |
| 31 | + // read all the file |
| 32 | + if transcoder.Verbose { |
| 33 | + graphIn(in) |
| 34 | + } |
| 35 | + |
| 36 | + // Get the WAV file configuration |
| 37 | + channels := out.Config.(audioconfig.WavConfig).Channels |
| 38 | + sampleRate := out.Config.(audioconfig.WavConfig).SampleRate |
| 39 | + bitsPerSample := out.Config.(audioconfig.WavConfig).BitDepth |
| 40 | + transcoder.SourceConfigs.Encoding = in.Config.(audioconfig.MulawConfig).Encoding |
| 41 | + transcoder.TargetConfigs.Encoding = out.Config.(audioconfig.WavConfig).Encoding |
| 42 | + transcoder.BitDepth = bitsPerSample |
| 43 | + |
| 44 | + if transcoder.SourceConfigs.Endianness == cpuarch.NOT_FILLED && transcoder.TargetConfigs.Endianness == cpuarch.NOT_FILLED { |
| 45 | + transcoder.SourceConfigs.Endianness = cpuarch.LITTLE_ENDIAN // replace with cpuarch.GetEndianess() |
| 46 | + transcoder.TargetConfigs.Endianness = cpuarch.LITTLE_ENDIAN |
| 47 | + } |
| 48 | + |
| 49 | + transcoder.Println( |
| 50 | + "\n[Format] ", in.Format, "=>", out.Format, |
| 51 | + "\n[Encoding] ", encoding.ENCODINGS[in.Config.(audioconfig.MulawConfig).Encoding], "=>", encoding.ENCODINGS[out.Config.(audioconfig.WavConfig).Encoding], |
| 52 | + "\n[Channels] ", in.Config.(audioconfig.MulawConfig).Channels, "=>", channels, |
| 53 | + "\n[SampleRate] ", in.Config.(audioconfig.MulawConfig).SampleRate, "=>", sampleRate, "kHz", |
| 54 | + "\n[BitDepth] ", in.Config.(audioconfig.MulawConfig).BitDepth, "=>", bitsPerSample, "bytes", |
| 55 | + "\n[Transcoder][Source][Encoding]", encoding.ENCODINGS[transcoder.SourceConfigs.Encoding], |
| 56 | + "\n[Transcoder][Target][Encoding]", encoding.ENCODINGS[transcoder.TargetConfigs.Encoding], |
| 57 | + "\n[Transcoder][BitDepth] ", transcoder.BitDepth, |
| 58 | + "\n[Transcoder][Endianness] ", cpuarch.ENDIANESSES[cpuarch.GetEndianess()], |
| 59 | + ) |
| 60 | + |
| 61 | + // Create a buffered reader and writer |
| 62 | + in.Reader = bufio.NewReader(in.Data) |
| 63 | + out.Writer = bufio.NewWriter(out.Data) |
| 64 | + out.Length = 0 |
| 65 | + |
| 66 | + headersWav := []byte{ |
| 67 | + 'R', 'I', 'F', 'F', // Chunk ID |
| 68 | + 0, 0, 0, 0, // Chunk size |
| 69 | + 'W', 'A', 'V', 'E', // Format |
| 70 | + 'f', 'm', 't', ' ', // Sub-chunk 1 ID |
| 71 | + 16, 0, 0, 0, // Sub-chunk 1 size |
| 72 | + audioconfig.WAVE_FORMAT_MULAW, 0, // Audio format (1 = PCM) |
| 73 | + byte(channels), 0, // Number of channels |
| 74 | + byte(sampleRate & 0xFF), // sample rate (low) |
| 75 | + byte(sampleRate >> 8 & 0xFF), // sample rate (mid) |
| 76 | + byte(sampleRate >> 16 & 0xFF), // sample rate (high) |
| 77 | + byte(sampleRate >> 24 & 0xFF), // sample rate (high) |
| 78 | + byte(sampleRate * channels * (bitsPerSample / 8) & 0xFF), // byte rate (low) |
| 79 | + byte(sampleRate * channels * (bitsPerSample / 8) >> 8 & 0xFF), // byte rate (mid) |
| 80 | + byte(sampleRate * channels * (bitsPerSample / 8) >> 16 & 0xFF), // byte rate (high) |
| 81 | + byte(sampleRate * channels * (bitsPerSample / 8) >> 24 & 0xFF), // byte rate (high) |
| 82 | + byte(channels * (bitsPerSample / 8)), 0, // block align |
| 83 | + byte(bitsPerSample), 0, // bits per sample |
| 84 | + 'd', 'a', 't', 'a', |
| 85 | + 0, 0, 0, 0, |
| 86 | + } |
| 87 | + out.Writer.Write(headersWav) |
| 88 | + out.Length += len(headersWav) |
| 89 | + |
| 90 | + if transcoder.Verbose { |
| 91 | + audioconfig.PrintWavHeaders(headersWav) |
| 92 | + } |
| 93 | + |
| 94 | + // Copy the data from the input file to the output file in chunks |
| 95 | + if err = TranscodeBytes(in, out, transcoder); err != nil { |
| 96 | + return fmt.Errorf("error converting bytes: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + // Flush the output file |
| 100 | + if err := out.Writer.Flush(); err != nil { |
| 101 | + return fmt.Errorf("error flushing output file: %v", err) |
| 102 | + } |
| 103 | + transcoder.Println("Wrote", out.Length, "bytes to output file") |
| 104 | + |
| 105 | + // Update the file size and data size fields |
| 106 | + fileFixer := out.Data.(*os.File) |
| 107 | + r, err := fileFixer.Seek(4, io.SeekStart) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("error seeking file: %v", err) |
| 110 | + } |
| 111 | + transcoder.Println("Seeked to:", r) |
| 112 | + fileSize := []byte{ |
| 113 | + byte((out.Length - 8) & 0xff), |
| 114 | + byte((out.Length - 8) >> 8 & 0xff), |
| 115 | + byte((out.Length - 8) >> 16 & 0xff), |
| 116 | + byte((out.Length - 8) >> 24 & 0xff), |
| 117 | + } |
| 118 | + n, err := fileFixer.Write(fileSize) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("error writing file size: %v", err) |
| 121 | + } |
| 122 | + transcoder.Println("File size:", fmt.Sprintf("% 02x", fileSize), "bytes written:", n) |
| 123 | + dataSize := []byte{ |
| 124 | + byte((out.Length - 44) & 0xff), |
| 125 | + byte((out.Length - 44) >> 8 & 0xff), |
| 126 | + byte((out.Length - 44) >> 16 & 0xff), |
| 127 | + byte((out.Length - 44) >> 24 & 0xff), |
| 128 | + } |
| 129 | + r, err = fileFixer.Seek(40, io.SeekStart) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("[2]error seeking file: %v", err) |
| 132 | + } |
| 133 | + transcoder.Println("Seeked to:", r) |
| 134 | + n, err = fileFixer.Write(dataSize) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("error writing data size: %v", err) |
| 137 | + } |
| 138 | + transcoder.Println("Data size:", fmt.Sprintf("% 02x", dataSize), "bytes written:", n) |
| 139 | + |
| 140 | + if transcoder.Verbose { |
| 141 | + graphOut(in, out) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | + |
| 146 | +} |
0 commit comments