Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1004794 // mp3ToWAV - might work now

JavaX fragment (include)

lib 1004796 // mp3transform

import org.mp3transform.Bitstream;
import org.mp3transform.Decoder;
import org.mp3transform.Header;
import org.mp3transform.wav.*;

static void mp3ToWAV(S in, S out) ctex {
  mp3ToWAV(new File(in), new File(out));
}

static File mp3ToWAV(File in, File out) ctex {
  mp3ToWAV(new BufferedInputStream(new FileInputStream(in)), out);
  ret out;
}

static void mp3ToWAV(InputStream in, File out) ctex {
  class WavConverter extends Decoder {
    void convert(String sourceFileName, String destFileName)
            throws IOException {
        InputStream fileIn = new FileInputStream(sourceFileName);
        BufferedInputStream in = new BufferedInputStream(fileIn, 128 * 1024);
        convert(in, destFileName);
        in.close();
    }

    void convert(InputStream sourceStream, String destFileName)
          throws IOException {
        int frameCount = Integer.MAX_VALUE;
        WavConverter decoder = new WavConverter();
        Bitstream stream = new Bitstream(sourceStream);
        frameCount = Integer.MAX_VALUE;
        try {
            for (int frame = 0; frame < frameCount; frame++) {
                Header header = stream.readFrame();
                if (header == null) {
                    break;
                }
                if (decoder.channels == 0) {
                    int channels = (header.mode() == Header.MODE_SINGLE_CHANNEL) ? 1
                            : 2;
                    int freq = header.frequency();
                    decoder.initOutputBuffer(channels, freq, destFileName);
                }
                decoder.decodeFrame(header, stream);
                stream.closeFrame();
            }
        } finally {
            decoder.close();
        }
    }

    private short[] buffer;
    private WaveFileWriter outWave;
    int channels;

    public void initOutputBuffer(int numberOfChannels, int freq, String fileName)
            throws IOException {
        super.initOutputBuffer(null, numberOfChannels);
        channels = numberOfChannels;
        buffer = new short[BUFFER_SIZE];
        for (int i = 0; i < numberOfChannels; ++i) {
            bufferPointer[i] = (short) i;
        }
        outWave = nuObject(WaveFileWriter.class, fileName, freq, (short) 16, (short) numberOfChannels);
    }
    
    public void appendSamples(int channel, double[] f) {
        int p = bufferPointer[channel];
        for (int i = 0; i < 32; i++) {
            double sample = f[i];
            short s = ((sample > 32767.0f) ? 32767
                    : ((sample < -32768.0f) ? -32768 : (short) sample));
            buffer[p] = s;
            p += channels;
        }
        bufferPointer[channel] = p;
    }    

    public void writeBuffer() throws IOException {
        call(outWave, "writeData", buffer, bufferPointer[0]);
        for (int i = 0; i < channels; ++i) {
            bufferPointer[i] = i;
        }
    }

    public void close() throws IOException {
      callOpt(outWave, "close");
    }
  }
  
  new WavConverter().convert(in, f2s(out));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004794
Snippet name: mp3ToWAV - might work now
Eternal ID of this version: #1004794/2
Text MD5: cf8d25a9fbb72a44e93fd7015f2e8925
Author: stefan
Category: javax / sound
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-22 16:04:43
Source code size: 3137 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 668 / 604
Version history: 1 change(s)
Referenced in: [show references]