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

105
LINES

< > BotCompany Repo | #1016679 // WAV Decoding Spike

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (2766L/20K).

!7

p {
	temp WaveDecoder decoder = new(new FileInputStream(loadLibrary(#1400093)));
	float[] samples = new float[1024];
	int readSamples = 0;
	while ((readSamples = decoder.readSamples(samples)) > 0)
		print("read " + readSamples + " samples");
}


// https://github.com/s4l4x/audio-analysis/blob/master/src/com/badlogic/audio/io/WaveDecoder.java

/**
 * A simple class that can read in the PCM data from a
 * Wav file, converting the data to signed 32-bit floats
 * in the range [-1,1], merging stereo channels to a mono
 * channel for processing. This only supports 16-bit signed
 * stereo and mono Wav files with a sampling rate of 44100.
 * 
 * @author mzechner
 *
 */
sclass WaveDecoder /*implements com.badlogic.audio.io.Decoder*/ implements AutoCloseable {
	/** inverse max short value as float **/
	private final float MAX_VALUE = 1.0f / Short.MAX_VALUE;
	
	/** the input stream we read from **/
	EndianDataInputStream in;
	
	/** number of channels **/
	private final int channels;
	
	/** sample rate in Herz**/
	private final float sampleRate;
	
	*(InputStream stream) ctex {
		in = new EndianDataInputStream( new BufferedInputStream( stream, 1024*1024) );		
		if( !in.read4ByteString().equals( "RIFF" ) )
			throw new IllegalArgumentException( "not a wav" );
		
		in.readIntLittleEndian();
		
		if( !in.read4ByteString().equals( "WAVE" ) )
			throw new IllegalArgumentException( "expected WAVE tag" );
		
		if( !in.read4ByteString().equals( "fmt " ) )
			throw new IllegalArgumentException( "expected fmt tag" );
		
		if( in.readIntLittleEndian() != 16 )
			throw new IllegalArgumentException( "expected wave chunk size to be 16" );
		
		if( in.readShortLittleEndian() != 1 )
			throw new IllegalArgumentException( "expected format to be 1" );
		
		channels = in.readShortLittleEndian();
		sampleRate = in.readIntLittleEndian();
		if( sampleRate != 44100 )
			throw new IllegalArgumentException( "Not 44100 sampling rate" );
		in.readIntLittleEndian();
		in.readShortLittleEndian();
		int fmt = in.readShortLittleEndian();

		if( fmt != 16 )
			throw new IllegalArgumentException( "Only 16-bit signed format supported" );
		
		if( !in.read4ByteString().equals( "data" ) )
			throw new RuntimeException( "expected data tag" );
				
		in.readIntLittleEndian();
	}
	
	/**
	 * Tries to read in samples.length samples, merging stereo to a mono
	 * channel by averaging and converting non float formats to float 32-bit.
	 * Returns the number of samples actually read. Guarantees that samples.length
	 * samples are read in if there was enough data in the stream.
	 * 
	 * @param samples The samples array to write the samples to
	 * @return The number of samples actually read.
	 */
	public int readSamples(float[] samples) {
		int readSamples = 0;
		for (int i = 0; i < samples.length; i++) {
			float sample = 0; 
			try {
				for( int j = 0; j < channels; j++ )
				{
					int shortValue = in.readShortLittleEndian( );
					sample += (shortValue * MAX_VALUE);
				}
				sample /= channels;
				samples[i] = sample;
				readSamples++;
			}
			catch (Exception ex) { break; }
		}
		
		ret readSamples; 
	}
	
	public void close() ctex {
	  in.close();
	}
}

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: 263 / 695
Version history: 2 change(s)
Referenced in: [show references]