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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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.transform.SoundTouch;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.tests.utils.GdxTest;

/**
* Demonstrates how to read an OGG file and play it back with a {@link VorbisDecoder} as
* well as how to alter the audio pitch with the {@link SoundTouch} API exposed in the
* gdx-audio extension.
* @author mzechner
*
*/
public class SoundTouchTest extends GdxTest {
/** the file to playback **/
private static final String FILE = "data/cloudconnected.ogg";
/** a VorbisDecoder to read PCM data from the ogg file **/
VorbisDecoder decoder;
/** an AudioDevice for playing back the PCM data **/
AudioDevice device;
/** SoundTouch instance to modify the PCM data **/
SoundTouch soundTouch;

@Override
public void create () {
// copy ogg file to SD card, can't playback from assets
FileHandle externalFile = Gdx.files.external("tmp/test.ogg");
Gdx.files.internal(FILE).copyTo(externalFile);

// Create the decoder and log some properties. Note that we need
// and external or absolute file!
decoder = new VorbisDecoder(externalFile);
Gdx.app.log("SoundTouchTest", "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);

// Create the SoundTouch instance
soundTouch = new SoundTouch();
soundTouch.setChannels(decoder.getChannels());
soundTouch.setSampleRate(decoder.getRate());
soundTouch.setPitchSemiTones(2);

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

// read until we reach the end of the file, we read the file
// fully as SoundTouch#receiveSamples returns uneven numbers
// of samples from time to time. Could be solved with a ring
// buffer :)
while((readSamples = decoder.readSamples(samples, 0, samples.length)) > 0) {
// process samples with sound touch, divide by 2 for stereo samples
// as one sample contains both left and right channel data in SoundTouch.
soundTouch.putSamples(samples, 0, readSamples / decoder.getChannels());
}

// read the samples from SoundTouch and play them back
while((readSamples = soundTouch.receiveSamples(samples, 0, samples.length / decoder.getChannels())) > 0) {
device.writeSamples(samples, 0, readSamples * decoder.getChannels());
}
}
});
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();
// kill the file again
Gdx.files.external("tmp/test.ogg").delete();
}

@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

r3201 by badlogicgames on Jan 3, 2012   Diff
[added] SoundTouchTest, demonstrating
use of SoundTouch API
r3200 by badlogicgames on Jan 3, 2012   Diff
[added] SoundTouchTest
[fixed] SoundTouch fully operational
All revisions of this file

File info

Size: 3336 bytes, 92 lines
Powered by Google Project Hosting