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 Java wrapper for the PortAudio API.
43 */
44 package com.portaudio;
45 
46 /**
47  * Java methods that call PortAudio via JNI. This is a portable audio I/O
48  * library that can be used as an alternative to JavaSound.
49  *
50  * Please see the PortAudio documentation for a full explanation.
51  *
52  * http://portaudio.com/docs/
53  * http://portaudio.com/docs/v19-doxydocs/portaudio_8h.html
54  *
55  * This Java binding does not support audio callbacks because an audio callback
56  * should never block. Calling into a Java virtual machine might block for
57  * garbage collection or synchronization. So only the blocking read/write mode
58  * is supported.
59  *
60  * @see BlockingStream
61  * @see DeviceInfo
62  * @see HostApiInfo
63  * @see StreamInfo
64  * @see StreamParameters
65  *
66  * @author Phil Burk
67  *
68  */
69 public class PortAudio
70 {
71 	public final static int FLAG_CLIP_OFF = (1 << 0);
72 	public final static int FLAG_DITHER_OFF = (1 << 1);
73 
74 	/** Sample Formats */
75 	public final static int FORMAT_FLOAT_32 = (1 << 0);
76 	public final static int FORMAT_INT_32 = (1 << 1); // not supported
77 	public final static int FORMAT_INT_24 = (1 << 2); // not supported
78 	public final static int FORMAT_INT_16 = (1 << 3);
79 	public final static int FORMAT_INT_8 = (1 << 4); // not supported
80 	public final static int FORMAT_UINT_8 = (1 << 5); // not supported
81 
82 	/** These HOST_API_TYPES will not change in the future. */
83 	public final static int HOST_API_TYPE_DEV = 0;
84 	public final static int HOST_API_TYPE_DIRECTSOUND = 1;
85 	public final static int HOST_API_TYPE_MME = 2;
86 	public final static int HOST_API_TYPE_ASIO = 3;
87 	/** Apple Sound Manager. Obsolete. */
88 	public final static int HOST_API_TYPE_SOUNDMANAGER = 4;
89 	public final static int HOST_API_TYPE_COREAUDIO = 5;
90 	public final static int HOST_API_TYPE_OSS = 7;
91 	public final static int HOST_API_TYPE_ALSA = 8;
92 	public final static int HOST_API_TYPE_AL = 9;
93 	public final static int HOST_API_TYPE_BEOS = 10;
94 	public final static int HOST_API_TYPE_WDMKS = 11;
95 	public final static int HOST_API_TYPE_JACK = 12;
96 	public final static int HOST_API_TYPE_WASAPI = 13;
97 	public final static int HOST_API_TYPE_AUDIOSCIENCE = 14;
98 	public final static int HOST_API_TYPE_COUNT = 15;
99 
100 	static
101 	{
102 		String os = System.getProperty( "os.name" ).toLowerCase();
103 		// On Windows we have separate libraries for 32 and 64-bit JVMs.
104 		if( os.indexOf( "win" ) >= 0 )
105 		{
106 			if( System.getProperty( "os.arch" ).contains( "64" ) )
107 			{
108 				System.loadLibrary( "jportaudio_x64" );
109 			}
110 			else
111 			{
112 				System.loadLibrary( "jportaudio_x86" );
113 			}
114 		}
115 		else
116 		{
117 			System.loadLibrary( "jportaudio" );
118 		}
119 		System.out.println( "---- JPortAudio version " + getVersion() + ", "
120 				+ getVersionText() );
121 	}
122 
123 	/**
124 	 * @return the release number of the currently running PortAudio build, eg
125 	 *         1900.
126 	 */
getVersion()127 	public native static int getVersion();
128 
129 	/**
130 	 * @return a textual description of the current PortAudio build, eg
131 	 *         "PortAudio V19-devel 13 October 2002".
132 	 */
getVersionText()133 	public native static String getVersionText();
134 
135 	/**
136 	 * Library initialization function - call this before using PortAudio. This
137 	 * function initializes internal data structures and prepares underlying
138 	 * host APIs for use. With the exception of getVersion(), getVersionText(),
139 	 * and getErrorText(), this function MUST be called before using any other
140 	 * PortAudio API functions.
141 	 */
initialize()142 	public native static void initialize();
143 
144 	/**
145 	 * Library termination function - call this when finished using PortAudio.
146 	 * This function deallocates all resources allocated by PortAudio since it
147 	 * was initialized by a call to initialize(). In cases where Pa_Initialise()
148 	 * has been called multiple times, each call must be matched with a
149 	 * corresponding call to terminate(). The final matching call to terminate()
150 	 * will automatically close any PortAudio streams that are still open.
151 	 */
terminate()152 	public native static void terminate();
153 
154 	/**
155 	 * @return the number of available devices. The number of available devices
156 	 *         may be zero.
157 	 */
getDeviceCount()158 	public native static int getDeviceCount();
159 
getDeviceInfo( int index, DeviceInfo deviceInfo )160 	private native static void getDeviceInfo( int index, DeviceInfo deviceInfo );
161 
162 	/**
163 	 * @param index
164 	 *            A valid device index in the range 0 to (getDeviceCount()-1)
165 	 * @return An DeviceInfo structure.
166 	 * @throws RuntimeException
167 	 *             if the device parameter is out of range.
168 	 */
getDeviceInfo( int index )169 	public static DeviceInfo getDeviceInfo( int index )
170 	{
171 		DeviceInfo deviceInfo = new DeviceInfo();
172 		getDeviceInfo( index, deviceInfo );
173 		return deviceInfo;
174 	}
175 
176 	/**
177 	 * @return the number of available host APIs.
178 	 */
getHostApiCount()179 	public native static int getHostApiCount();
180 
getHostApiInfo( int index, HostApiInfo hostApiInfo )181 	private native static void getHostApiInfo( int index,
182 			HostApiInfo hostApiInfo );
183 
184 	/**
185 	 * @param index
186 	 * @return information about the Host API
187 	 */
getHostApiInfo( int index )188 	public static HostApiInfo getHostApiInfo( int index )
189 	{
190 		HostApiInfo hostApiInfo = new HostApiInfo();
191 		getHostApiInfo( index, hostApiInfo );
192 		return hostApiInfo;
193 	}
194 
195 	/**
196 	 * @param hostApiType
197 	 *            A unique host API identifier, for example
198 	 *            HOST_API_TYPE_COREAUDIO.
199 	 * @return a runtime host API index
200 	 */
hostApiTypeIdToHostApiIndex( int hostApiType )201 	public native static int hostApiTypeIdToHostApiIndex( int hostApiType );
202 
203 	/**
204 	 * @param hostApiIndex
205 	 *            A valid host API index ranging from 0 to (getHostApiCount()-1)
206 	 * @param apiDeviceIndex
207 	 *            A valid per-host device index in the range 0 to
208 	 *            (getHostApiInfo(hostApi).deviceCount-1)
209 	 * @return standard PortAudio device index
210 	 */
hostApiDeviceIndexToDeviceIndex( int hostApiIndex, int apiDeviceIndex )211 	public native static int hostApiDeviceIndexToDeviceIndex( int hostApiIndex,
212 			int apiDeviceIndex );
213 
getDefaultInputDevice()214 	public native static int getDefaultInputDevice();
215 
getDefaultOutputDevice()216 	public native static int getDefaultOutputDevice();
217 
getDefaultHostApi()218 	public native static int getDefaultHostApi();
219 
220 	/**
221 	 * @param inputStreamParameters
222 	 *            input description, may be null
223 	 * @param outputStreamParameters
224 	 *            output description, may be null
225 	 * @param sampleRate
226 	 *            typically 44100 or 48000, or maybe 22050, 16000, 8000, 96000
227 	 * @return 0 if supported or a negative error
228 	 */
isFormatSupported( StreamParameters inputStreamParameters, StreamParameters outputStreamParameters, int sampleRate )229 	public native static int isFormatSupported(
230 			StreamParameters inputStreamParameters,
231 			StreamParameters outputStreamParameters, int sampleRate );
232 
openStream( BlockingStream blockingStream, StreamParameters inputStreamParameters, StreamParameters outputStreamParameters, int sampleRate, int framesPerBuffer, int flags )233 	private native static void openStream( BlockingStream blockingStream,
234 			StreamParameters inputStreamParameters,
235 			StreamParameters outputStreamParameters, int sampleRate,
236 			int framesPerBuffer, int flags );
237 
238 	/**
239 	 *
240 	 * @param inputStreamParameters
241 	 *            input description, may be null
242 	 * @param outputStreamParameters
243 	 *            output description, may be null
244 	 * @param sampleRate
245 	 *            typically 44100 or 48000, or maybe 22050, 16000, 8000, 96000
246 	 * @param framesPerBuffer
247 	 * @param flags
248 	 * @return
249 	 */
openStream( StreamParameters inputStreamParameters, StreamParameters outputStreamParameters, int sampleRate, int framesPerBuffer, int flags )250 	public static BlockingStream openStream(
251 			StreamParameters inputStreamParameters,
252 			StreamParameters outputStreamParameters, int sampleRate,
253 			int framesPerBuffer, int flags )
254 	{
255 		BlockingStream blockingStream = new BlockingStream();
256 		openStream( blockingStream, inputStreamParameters,
257 				outputStreamParameters, sampleRate, framesPerBuffer, flags );
258 		return blockingStream;
259 	}
260 
261 }
262