Download Jar. Libraryless. Click here for Pure Java version (2766L/20K).
1  | !7  | 
2  | |
3  | p {
 | 
4  | temp WaveDecoder decoder = new(new FileInputStream(loadLibrary(#1400093)));  | 
5  | float[] samples = new float[1024];  | 
6  | int readSamples = 0;  | 
7  | while ((readSamples = decoder.readSamples(samples)) > 0)  | 
8  | 		print("read " + readSamples + " samples");
 | 
9  | }  | 
10  | |
11  | |
12  | // https://github.com/s4l4x/audio-analysis/blob/master/src/com/badlogic/audio/io/WaveDecoder.java  | 
13  | |
14  | /**  | 
15  | * A simple class that can read in the PCM data from a  | 
16  | * Wav file, converting the data to signed 32-bit floats  | 
17  | * in the range [-1,1], merging stereo channels to a mono  | 
18  | * channel for processing. This only supports 16-bit signed  | 
19  | * stereo and mono Wav files with a sampling rate of 44100.  | 
20  | *  | 
21  | * @author mzechner  | 
22  | *  | 
23  | */  | 
24  | sclass WaveDecoder /*implements com.badlogic.audio.io.Decoder*/ implements AutoCloseable {
 | 
25  | /** inverse max short value as float **/  | 
26  | private final float MAX_VALUE = 1.0f / Short.MAX_VALUE;  | 
27  | |
28  | /** the input stream we read from **/  | 
29  | EndianDataInputStream in;  | 
30  | |
31  | /** number of channels **/  | 
32  | private final int channels;  | 
33  | |
34  | /** sample rate in Herz**/  | 
35  | private final float sampleRate;  | 
36  | |
37  | 	*(InputStream stream) ctex {
 | 
38  | in = new EndianDataInputStream( new BufferedInputStream( stream, 1024*1024) );  | 
39  | if( !in.read4ByteString().equals( "RIFF" ) )  | 
40  | throw new IllegalArgumentException( "not a wav" );  | 
41  | |
42  | in.readIntLittleEndian();  | 
43  | |
44  | if( !in.read4ByteString().equals( "WAVE" ) )  | 
45  | throw new IllegalArgumentException( "expected WAVE tag" );  | 
46  | |
47  | if( !in.read4ByteString().equals( "fmt " ) )  | 
48  | throw new IllegalArgumentException( "expected fmt tag" );  | 
49  | |
50  | if( in.readIntLittleEndian() != 16 )  | 
51  | throw new IllegalArgumentException( "expected wave chunk size to be 16" );  | 
52  | |
53  | if( in.readShortLittleEndian() != 1 )  | 
54  | throw new IllegalArgumentException( "expected format to be 1" );  | 
55  | |
56  | channels = in.readShortLittleEndian();  | 
57  | sampleRate = in.readIntLittleEndian();  | 
58  | if( sampleRate != 44100 )  | 
59  | throw new IllegalArgumentException( "Not 44100 sampling rate" );  | 
60  | in.readIntLittleEndian();  | 
61  | in.readShortLittleEndian();  | 
62  | int fmt = in.readShortLittleEndian();  | 
63  | |
64  | if( fmt != 16 )  | 
65  | throw new IllegalArgumentException( "Only 16-bit signed format supported" );  | 
66  | |
67  | if( !in.read4ByteString().equals( "data" ) )  | 
68  | throw new RuntimeException( "expected data tag" );  | 
69  | |
70  | in.readIntLittleEndian();  | 
71  | }  | 
72  | |
73  | /**  | 
74  | * Tries to read in samples.length samples, merging stereo to a mono  | 
75  | * channel by averaging and converting non float formats to float 32-bit.  | 
76  | * Returns the number of samples actually read. Guarantees that samples.length  | 
77  | * samples are read in if there was enough data in the stream.  | 
78  | *  | 
79  | * @param samples The samples array to write the samples to  | 
80  | * @return The number of samples actually read.  | 
81  | */  | 
82  | 	public int readSamples(float[] samples) {
 | 
83  | int readSamples = 0;  | 
84  | 		for (int i = 0; i < samples.length; i++) {
 | 
85  | float sample = 0;  | 
86  | 			try {
 | 
87  | for( int j = 0; j < channels; j++ )  | 
88  | 				{
 | 
89  | int shortValue = in.readShortLittleEndian( );  | 
90  | sample += (shortValue * MAX_VALUE);  | 
91  | }  | 
92  | sample /= channels;  | 
93  | samples[i] = sample;  | 
94  | readSamples++;  | 
95  | }  | 
96  | 			catch (Exception ex) { break; }
 | 
97  | }  | 
98  | |
99  | ret readSamples;  | 
100  | }  | 
101  | |
102  | 	public void close() ctex {
 | 
103  | in.close();  | 
104  | }  | 
105  | }  | 
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1016679 | 
| Snippet name: | WAV Decoding Spike | 
| Eternal ID of this version: | #1016679/3 | 
| Text MD5: | 01abf123185b139f4aad029b5eb702d6 | 
| Transpilation MD5: | e29c77d565c9d415f0423c5021472ddb | 
| Author: | stefan | 
| Category: | javax / sound | 
| Type: | JavaX source code (desktop) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2018-06-28 00:09:35 | 
| Source code size: | 3268 bytes / 105 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 630 / 1533 | 
| Version history: | 2 change(s) | 
| Referenced in: | [show references] |