Debian Codegraph @

bindings/java/jportaudio/src/com/portaudio/BlockingStream.java

raw
1 /*
2 * Portable Audio I/O Library
3 * Java Binding for PortAudio
4 *
5 * Based on the Open Source API proposed by Ross Bencina
6 * Copyright (c) 2008 Ross Bencina
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files
10 * (the "Software"), to deal in the Software without restriction,
11 * including without limitation the rights to use, copy, modify, merge,
12 * publish, distribute, sublicense, and/or sell copies of the Software,
13 * and to permit persons to whom the Software is furnished to do so,
14 * subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 /*
29 * The text above constitutes the entire PortAudio license; however,
30 * the PortAudio community also makes the following non-binding requests:
31 *
32 * Any person wishing to distribute modifications to the Software is
33 * requested to send the modifications to the original developer so that
34 * they can be incorporated into the canonical version. It is also
35 * requested that these non-binding requests be included along with the
36 * license above.
37 */
38
39 /** @file
40 @ingroup bindings_java
41
42 @brief A blocking read/write stream.
43 */
44 package com.portaudio;
45
46 /**
47 * Represents a stream for blocking read/write I/O.
48 *
49 * This Java object contains the pointer to a PortAudio stream stored as a long.
50 * It is passed to PortAudio when calling stream related functions.
51 *
52 * To create one of these, call PortAudio.openStream().
53 *
54 * @see PortAudio
55 *
56 * @author Phil Burk
57 *
58 */
59 public class BlockingStream
60 {
61 // nativeStream is only accessed by the native code. It contains a pointer
62 // to a PaStream.
63 private long nativeStream;
64 private int inputFormat = -1;
65 private int outputFormat = -1;
66
67 protected BlockingStream()
68 {
69 }
70
71 /**
72 * @return number of frames that can be read without blocking.
73 */
74 public native int getReadAvailable();
75
76 /**
77 * @return number of frames that can be written without blocking.
78 */
79 public native int getWriteAvailable();
80
81 private native boolean readFloats( float[] buffer, int numFrames );
82
83 private native boolean writeFloats( float[] buffer, int numFrames );
84
85 /**
86 * Read 32-bit floating point data from the stream into the array.
87 *
88 * @param buffer
89 * @param numFrames
90 * number of frames to read
91 * @return true if an input overflow occurred
92 */
93 public boolean read( float[] buffer, int numFrames )
94 {
95 if( inputFormat != PortAudio.FORMAT_FLOAT_32 )
96 {
97 throw new RuntimeException(
98 "Tried to read float samples from a non float stream." );
99 }
100 return readFloats( buffer, numFrames );
101 }
102
103 /**
104 * Write 32-bit floating point data to the stream from the array. The data
105 * should be in the range -1.0 to +1.0.
106 *
107 * @param buffer
108 * @param numFrames
109 * number of frames to write
110 * @return true if an output underflow occurred
111 */
112 public boolean write( float[] buffer, int numFrames )
113 {
114 if( outputFormat != PortAudio.FORMAT_FLOAT_32 )
115 {
116 throw new RuntimeException(
117 "Tried to write float samples to a non float stream." );
118 }
119 return writeFloats( buffer, numFrames );
120 }
121
122 private native boolean readShorts( short[] buffer, int numFrames );
123
124 private native boolean writeShorts( short[] buffer, int numFrames );
125
126 /**
127 * Read 16-bit integer data to the stream from the array.
128 *
129 * @param buffer
130 * @param numFrames
131 * number of frames to write
132 * @return true if an input overflow occurred
133 */
134 public boolean read( short[] buffer, int numFrames )
135 {
136 if( inputFormat != PortAudio.FORMAT_INT_16 )
137 {
138 throw new RuntimeException(
139 "Tried to read short samples from a non short stream." );
140 }
141 return readShorts( buffer, numFrames );
142 }
143
144 /**
145 * Write 16-bit integer data to the stream from the array.
146 *
147 * @param buffer
148 * @param numFrames
149 * number of frames to write
150 * @return true if an output underflow occurred
151 */
152 public boolean write( short[] buffer, int numFrames )
153 {
154 if( outputFormat != PortAudio.FORMAT_INT_16 )
155 {
156 throw new RuntimeException(
157 "Tried to write short samples from a non short stream." );
158 }
159 return writeShorts( buffer, numFrames );
160 }
161
162 /**
163 * Atart audio I/O.
164 */
165 public native void start();
166
167 /**
168 * Wait for the stream to play all of the data that has been written then
169 * stop.
170 */
171 public native void stop();
172
173 /**
174 * Stop immediately and lose any data that was written but not played.
175 */
176 public native void abort();
177
178 /**
179 * Close the stream and zero out the pointer. Do not reference the stream
180 * after this.
181 */
182 public native void close();
183
184 public native boolean isStopped();
185
186 public native boolean isActive();
187
188 public String toString()
189 {
190 return "BlockingStream: streamPtr = " + Long.toHexString( nativeStream )
191 + ", inFormat = " + inputFormat + ", outFormat = "
192 + outputFormat;
193 }
194
195 /**
196 * Get audio time related to this stream. Note that it may not start at 0.0.
197 */
198 public native double getTime();
199
200 private native void getInfo( StreamInfo streamInfo );
201
202 public StreamInfo getInfo()
203 {
204 StreamInfo streamInfo = new StreamInfo();
205 getInfo( streamInfo );
206 return streamInfo;
207 }
208 }