PortAudio: PlaySine.java Source File

PortAudio 2.0

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

PlaySine.java
Go to the documentation of this file.
1
7 package com.portaudio;
8
9 import com.portaudio.TestBasic.SineOscillator;
10
11 public class PlaySine
12 {
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
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 }