• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.audio.AudioDevice;
21 import com.badlogic.gdx.tests.utils.GdxTest;
22 
23 public class AudioDeviceTest extends GdxTest {
24 	Thread thread;
25 	boolean stop = false;
26 
27 	@Override
create()28 	public void create () {
29 		if (thread == null) {
30 			final AudioDevice device = Gdx.app.getAudio().newAudioDevice(44100, false);
31 			thread = new Thread(new Runnable() {
32 				@Override
33 				public void run () {
34 					final float frequency = 440;
35 					float increment = (float)(2 * Math.PI) * frequency / 44100; // angular increment for each sample
36 					float angle = 0;
37 					float samples[] = new float[1024];
38 
39 					while (!stop) {
40 						for (int i = 0; i < samples.length; i += 2) {
41 							samples[i] = 0.5f * (float)Math.sin(angle);
42 							samples[i + 1] = 2 * samples[i];
43 							angle += increment;
44 						}
45 
46 						device.writeSamples(samples, 0, samples.length);
47 					}
48 
49 					device.dispose();
50 				}
51 			});
52 			thread.start();
53 		}
54 	}
55 
56 	@Override
dispose()57 	public void dispose () {
58 		stop = true;
59 		try {
60 			thread.join();
61 		} catch (InterruptedException e) {
62 			e.printStackTrace();
63 		}
64 	}
65 }
66