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)

1  
lib 1004796 // mp3transform
2  
3  
import org.mp3transform.Bitstream;
4  
import org.mp3transform.Decoder;
5  
import org.mp3transform.Header;
6  
import org.mp3transform.wav.*;
7  
8  
static void mp3ToWAV(S in, S out) ctex {
9  
  mp3ToWAV(new File(in), new File(out));
10  
}
11  
12  
static File mp3ToWAV(File in, File out) ctex {
13  
  mp3ToWAV(new BufferedInputStream(new FileInputStream(in)), out);
14  
  ret out;
15  
}
16  
17  
static void mp3ToWAV(InputStream in, File out) ctex {
18  
  class WavConverter extends Decoder {
19  
    void convert(String sourceFileName, String destFileName)
20  
            throws IOException {
21  
        InputStream fileIn = new FileInputStream(sourceFileName);
22  
        BufferedInputStream in = new BufferedInputStream(fileIn, 128 * 1024);
23  
        convert(in, destFileName);
24  
        in.close();
25  
    }
26  
27  
    void convert(InputStream sourceStream, String destFileName)
28  
          throws IOException {
29  
        int frameCount = Integer.MAX_VALUE;
30  
        WavConverter decoder = new WavConverter();
31  
        Bitstream stream = new Bitstream(sourceStream);
32  
        frameCount = Integer.MAX_VALUE;
33  
        try {
34  
            for (int frame = 0; frame < frameCount; frame++) {
35  
                Header header = stream.readFrame();
36  
                if (header == null) {
37  
                    break;
38  
                }
39  
                if (decoder.channels == 0) {
40  
                    int channels = (header.mode() == Header.MODE_SINGLE_CHANNEL) ? 1
41  
                            : 2;
42  
                    int freq = header.frequency();
43  
                    decoder.initOutputBuffer(channels, freq, destFileName);
44  
                }
45  
                decoder.decodeFrame(header, stream);
46  
                stream.closeFrame();
47  
            }
48  
        } finally {
49  
            decoder.close();
50  
        }
51  
    }
52  
53  
    private short[] buffer;
54  
    private WaveFileWriter outWave;
55  
    int channels;
56  
57  
    public void initOutputBuffer(int numberOfChannels, int freq, String fileName)
58  
            throws IOException {
59  
        super.initOutputBuffer(null, numberOfChannels);
60  
        channels = numberOfChannels;
61  
        buffer = new short[BUFFER_SIZE];
62  
        for (int i = 0; i < numberOfChannels; ++i) {
63  
            bufferPointer[i] = (short) i;
64  
        }
65  
        outWave = nuObject(WaveFileWriter.class, fileName, freq, (short) 16, (short) numberOfChannels);
66  
    }
67  
    
68  
    public void appendSamples(int channel, double[] f) {
69  
        int p = bufferPointer[channel];
70  
        for (int i = 0; i < 32; i++) {
71  
            double sample = f[i];
72  
            short s = ((sample > 32767.0f) ? 32767
73  
                    : ((sample < -32768.0f) ? -32768 : (short) sample));
74  
            buffer[p] = s;
75  
            p += channels;
76  
        }
77  
        bufferPointer[channel] = p;
78  
    }    
79  
80  
    public void writeBuffer() throws IOException {
81  
        call(outWave, "writeData", buffer, bufferPointer[0]);
82  
        for (int i = 0; i < channels; ++i) {
83  
            bufferPointer[i] = i;
84  
        }
85  
    }
86  
87  
    public void close() throws IOException {
88  
      callOpt(outWave, "close");
89  
    }
90  
  }
91  
  
92  
  new WavConverter().convert(in, f2s(out));
93  
}

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: 670 / 607
Version history: 1 change(s)
Referenced in: [show references]