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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

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.files.FileHandle;
import com.badlogic.gdx.tests.utils.GdxTest;

/**
* Demonstrates how to read and playback an OGG file with the {@link VorbisDecoder} found
* in the gdx-audio extension.
* @author mzechner
*
*/
public class VorbisTest extends GdxTest {
/** the file to playback **/
private static final String FILE = "data/engine-2.ogg";
/** a VorbisDecoder to read PCM data from the ogg file **/
VorbisDecoder decoder;
/** an AudioDevice for playing back the PCM data **/
AudioDevice device;

@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
// an external or absolute file
decoder = new VorbisDecoder(externalFile);
Gdx.app.log("Vorbis", "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) {
// 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();
// kill the file again
Gdx.files.external("tmp/test.ogg").delete();
}

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

Change log

r3346 by badlogicgames on Feb 7, 2012   Diff
[fixed] bug in decoder
Go to: 
Project members, sign in to write a code review

Older revisions

r3208 by badlogicgames on Jan 3, 2012   Diff
[added] WavTest
[fixed] Vorbis and SoundTouch tests to
use new Decoder interface.
r3200 by badlogicgames on Jan 3, 2012   Diff
[added] SoundTouchTest
[fixed] SoundTouch fully operational
r3035 by badlogicgames on Dec 26, 2011   Diff
[removed] vorbis and mpg123 decoder
from gdx core
All revisions of this file

File info

Size: 3107 bytes, 89 lines
Powered by Google Project Hosting