My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package com.badlogic.gdx.tests;

import java.nio.ShortBuffer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.analysis.AudioTools;
import com.badlogic.gdx.audio.io.VorbisDecoder;
import com.badlogic.gdx.audio.io.WavDecoder;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.tests.utils.GdxTest;

public class WavTest extends GdxTest {
/** the file to playback **/
private static final String FILE = "data/sell_buy_item.wav";
/** a VorbisDecoder to read PCM data from the ogg file **/
WavDecoder decoder;
/** an AudioDevice for playing back the PCM data **/
AudioDevice device;

@Override
public void create () {
// Create the decoder and log some properties.
decoder = new WavDecoder(Gdx.files.internal(FILE));
Gdx.app.log("WavTest", "channels: " + decoder.getChannels() + ", rate: " + decoder.getRate() + ", length: " + decoder.getLength());

// Create an audio device for playback
device = Gdx.audio.newAudioDevice(decoder.getRate(), decoder.getChannels() == 1? true: false);

// start a thread for playback
Thread playbackThread = new Thread(new Runnable() {
@Override
public void run() {
int readSamples = 0;
// we need a short[] to pass the data to the AudioDevice
short[] samples = new short[2048];

// read until we reach the end of the file
while((readSamples = decoder.readSamples(samples, 0, samples.length)) > 0) {
Gdx.app.log("WavTest", "read " + readSamples + " samples");
// write the samples to the AudioDevice
device.writeSamples(samples, 0, readSamples);
}
}
});
playbackThread.setDaemon(true);
playbackThread.start();
}

@Override
public void dispose() {
// we should synchronize with the thread here
// left as an excercise to the reader :)
device.dispose();
decoder.dispose();
}

@Override
public boolean needsGL20 () {
return false;
}
}

Change log

r3208 by badlogicgames on Jan 3, 2012   Diff
[added] WavTest
[fixed] Vorbis and SoundTouch tests to use
new Decoder interface.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1992 bytes, 62 lines
Powered by Google Project Hosting