My favorites
▼
|
Sign in
audio-analysis
A small project for for an audio analysis tutorial
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
com
/
badlogic
/
audio
/
io
/
AudioDevice.java
‹r3
r32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.badlogic.audio.io;
import java.io.FileInputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.AudioFormat.Encoding;
/**
* Class that allows directly passing PCM float mono
* data to the sound card for playback. The sampling
* rate of the PCM data must be 44100Hz.
*
* @author mzechner
*
*/
public class AudioDevice
{
/** the buffer size in samples **/
private final static int BUFFER_SIZE = 1024;
/** the java sound line we write our samples to **/
private final SourceDataLine out;
/** buffer for BUFFER_SIZE 16-bit samples **/
private byte[] buffer = new byte[BUFFER_SIZE*2];
/**
* Constructor, initializes the audio system for
* 44100Hz 16-bit signed mono output.
*
* @throws Exception in case the audio system could not be initialized
*/
public AudioDevice( ) throws Exception
{
AudioFormat format = new AudioFormat( Encoding.PCM_SIGNED, 44100, 16, 1, 2, 44100, false );
out = AudioSystem.getSourceDataLine( format );
out.open(format);
out.start();
}
/**
* Writes the given samples to the audio device. The samples
* have to be sampled at 44100Hz, mono and have to be in
* the range [-1,1].
*
* @param samples The samples.
*/
public void writeSamples( float[] samples )
{
fillBuffer( samples );
out.write( buffer, 0, buffer.length );
}
private void fillBuffer( float[] samples )
{
for( int i = 0, j = 0; i < samples.length; i++, j+=2 )
{
short value = (short)(samples[i] * Short.MAX_VALUE);
buffer[j] = (byte)(value | 0xff);
buffer[j+1] = (byte)(value >> 8 );
}
}
public static void main( String[] argv ) throws Exception
{
float[] samples = new float[1024];
WaveDecoder reader = new WaveDecoder( new FileInputStream( "samples/sample.wav" ) );
AudioDevice device = new AudioDevice( );
while( reader.readSamples( samples ) > 0 )
{
device.writeSamples( samples );
}
Thread.sleep( 10000 );
}
}
Show details
Hide details
Change log
r5
by badlogicgames on Feb 19, 2010
Diff
[No log message]
Go to:
...dlogic/audio/io/AudioDevice.java
...io/io/EndianDataInputStream.java
...badlogic/audio/io/PCMFormat.java
...dlogic/audio/io/WaveDecoder.java
Project members,
sign in
to write a code review
Older revisions
r3
by badlogicgames on Feb 19, 2010
Diff
[No log message]
r2
by badlogicgames on Feb 19, 2010
Diff
[No log message]
All revisions of this file
File info
Size: 2118 bytes, 80 lines
View raw file
Powered by
Google Project Hosting