To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.
root / src / portaudio_20161030_catalina_patch / bindings / java / jportaudio / jtests / com / portaudio / PlaySine.java @ 162:d43aab368df9
History | View | Annotate | Download (2.38 KB)
| 1 |
|
|---|---|
| 2 |
/** @file
|
| 3 |
@ingroup bindings_java
|
| 4 |
|
| 5 |
@brief Example that shows how to play sine waves using JPortAudio.
|
| 6 |
*/
|
| 7 |
package com.portaudio; |
| 8 |
|
| 9 |
import com.portaudio.TestBasic.SineOscillator; |
| 10 |
|
| 11 |
public class PlaySine |
| 12 |
{
|
| 13 |
/**
|
| 14 |
* Write a sine wave to the stream.
|
| 15 |
* @param stream
|
| 16 |
* @param framesPerBuffer
|
| 17 |
* @param numFrames
|
| 18 |
* @param sampleRate
|
| 19 |
*/
|
| 20 |
private void writeSineData( BlockingStream stream, int framesPerBuffer, |
| 21 |
int numFrames, int sampleRate ) |
| 22 |
{
|
| 23 |
float[] buffer = new float[framesPerBuffer * 2]; |
| 24 |
SineOscillator osc1 = new SineOscillator( 200.0, sampleRate ); |
| 25 |
SineOscillator osc2 = new SineOscillator( 300.0, sampleRate ); |
| 26 |
int framesLeft = numFrames;
|
| 27 |
while( framesLeft > 0 ) |
| 28 |
{
|
| 29 |
int index = 0; |
| 30 |
int framesToWrite = (framesLeft > framesPerBuffer) ? framesPerBuffer
|
| 31 |
: framesLeft; |
| 32 |
for( int j = 0; j < framesToWrite; j++ ) |
| 33 |
{
|
| 34 |
buffer[index++] = (float) osc1.next();
|
| 35 |
buffer[index++] = (float) osc2.next();
|
| 36 |
} |
| 37 |
stream.write( buffer, framesToWrite ); |
| 38 |
framesLeft -= framesToWrite; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/**
|
| 43 |
* Create a stream on the default device then play sine waves.
|
| 44 |
*/
|
| 45 |
public void play() |
| 46 |
{
|
| 47 |
PortAudio.initialize(); |
| 48 |
|
| 49 |
// Get the default device and setup the stream parameters.
|
| 50 |
int deviceId = PortAudio.getDefaultOutputDevice();
|
| 51 |
DeviceInfo deviceInfo = PortAudio.getDeviceInfo( deviceId ); |
| 52 |
double sampleRate = deviceInfo.defaultSampleRate;
|
| 53 |
System.out.println( " deviceId = " + deviceId ); |
| 54 |
System.out.println( " sampleRate = " + sampleRate ); |
| 55 |
System.out.println( " device name = " + deviceInfo.name ); |
| 56 |
|
| 57 |
StreamParameters streamParameters = new StreamParameters();
|
| 58 |
streamParameters.channelCount = 2;
|
| 59 |
streamParameters.device = deviceId; |
| 60 |
streamParameters.suggestedLatency = deviceInfo.defaultLowOutputLatency; |
| 61 |
System.out.println( " suggestedLatency = " |
| 62 |
+ streamParameters.suggestedLatency ); |
| 63 |
|
| 64 |
int framesPerBuffer = 256; |
| 65 |
int flags = 0; |
| 66 |
|
| 67 |
// Open a stream for output.
|
| 68 |
BlockingStream stream = PortAudio.openStream( null, streamParameters,
|
| 69 |
(int) sampleRate, framesPerBuffer, flags );
|
| 70 |
|
| 71 |
int numFrames = (int) (sampleRate * 4); // enough for 4 seconds |
| 72 |
|
| 73 |
stream.start(); |
| 74 |
|
| 75 |
writeSineData( stream, framesPerBuffer, numFrames, (int) sampleRate );
|
| 76 |
|
| 77 |
stream.stop(); |
| 78 |
stream.close(); |
| 79 |
|
| 80 |
PortAudio.terminate(); |
| 81 |
System.out.println( "JPortAudio test complete." ); |
| 82 |
} |
| 83 |
|
| 84 |
public static void main( String[] args ) |
| 85 |
{
|
| 86 |
PlaySine player = new PlaySine();
|
| 87 |
player.play(); |
| 88 |
} |
| 89 |
} |