Re: AudioStream format problem
Re: AudioStream format problem
- Subject: Re: AudioStream format problem
- From: Jeff Moore <email@hidden>
- Date: Mon, 6 Jan 2003 11:54:46 -0800
kAudioHardwareBadStreamError is only ever returned when the
AudioStreamID passed to a HAL API call doesn't refer to any actual
stream the HAL knows about. Note that changing the format of a stream
will usually result in a kAudioDevicePropertyStreams notification which
indicates that all the AudioStreamIDs on the given device are now
invalid. Likely, somewhere an AudioStreamID is being cached across this
notification.
On Monday, December 30, 2002, at 02:08 PM, Harold Mills wrote:
Hello,
I'm using the CoreAudio HAL Java API to capture data from an audio
input
stream. Unfortunately I can't seem to change the input data format
without
corrupting the stream, causing the next method that attempts to
operate on
it to throw a CAException. The program below illustrates the problem,
as
described in the header comment. Could somebody please tell me what I'm
doing wrong?
It seems awkward to me to get the list of supported formats as in the
getSupportedAudioFormats and getAudioFormat methods. Is there a
simpler way?
Thank you,
Harold Mills
Cornell Bioacoustics Research Program
/*
This program demonstrates a problem I've encountered trying to set the
format
of a Macintosh CoreAudio input stream. The program gets an input
stream and
the physical formats it supports, and then attempts to set the stream's
format
to one of the supported formats. The stream's current format is printed
before
and after the set operation.
If the format being set is the stream's current format, the program
produces
the expected output, similar to the following:
com.apple.audio.util.AudioStreamDescription[SampleRate=12000.0,FormatID
=lpcm
,FormatFlags=0xc,BytesPerPacket=1,FramesPerPacket=1,BytesPerFrame=1,Cha
nnels
PerFrame=1,BitsPerChannel=8]
com.apple.audio.util.AudioStreamDescription[SampleRate=12000.0,FormatID
=lpcm
,FormatFlags=0xc,BytesPerPacket=1,FramesPerPacket=1,BytesPerFrame=1,Cha
nnels
PerFrame=1,BitsPerChannel=8]
However, if the format being set differs from the stream's current
format,
we get something like the following:
com.apple.audio.util.AudioStreamDescription[SampleRate=4000.0,FormatID=
lpcm,
FormatFlags=0xc,BytesPerPacket=1,FramesPerPacket=1,BytesPerFrame=1,Chan
nelsP
erFrame=1,BitsPerChannel=8]
Exception in thread "main"
com.apple.audio.CAException[JCoreAudio:1.2.111],561214578=kAudioHardwar
eBadS
treamError
at com.apple.audio.CAException.checkError(CAException.java:60)
at
com.apple.audio.hardware.AudioStream.getPropertyInfoSize(AudioStream.ja
va:98
)
at CoreAudioProblem1.showCurrentFormat(CoreAudioProblem1.java:119)
at CoreAudioProblem1.main(CoreAudioProblem1.java:51)
The exception is thrown from the second call to showCurrentFormat in
the
main method.
I am using MacOS 10.2.2 and a Griffin Technologies, Inc. iMic USB audio
device.
Harold Mills
Cornell Bioacoustics Research Program
*/
import com.apple.audio.*;
import com.apple.audio.hardware.*;
import com.apple.audio.util.*;
public class CoreAudioProblem1 {
public static void main(String[] inArgs) throws CAException {
AudioStream stream =
AudioHardware.getAudioDevices()[2].getInputStreams()[0];
showCurrentFormat(stream);
AudioStreamDescription[] formats =
getSupportedAudioFormats(stream);
// change the array index in the following to select a
different
format
stream.setProperty(
null, 0, AHConstants.kAudioStreamPropertyPhysicalFormat,
formats[0]);
showCurrentFormat(stream);
}
public static AudioStreamDescription[] getSupportedAudioFormats(
AudioStream inStream
) throws CAException {
int formatArraySize = inStream.getPropertyInfoSize(
0, AHConstants.kAudioStreamPropertyPhysicalFormats);
CAMemoryObject formatArray = new
CAMemoryObject(formatArraySize,
true);
inStream.getProperty(
0, AHConstants.kAudioStreamPropertyPhysicalFormats,
formatArray);
int numFormats = formatArraySize /
AudioStreamDescription.kNativeSize;
AudioStreamDescription[] formats = new
AudioStreamDescription[numFormats];
for (int i = 0; i != numFormats; ++i) {
formats[i] = getAudioFormat(formatArray, i);
}
return formats;
}
private static AudioStreamDescription getAudioFormat(
CAMemoryObject inFormatArray,
int inIndex
) throws CAException {
AudioStreamDescription format = new
AudioStreamDescription(true);
int offset = inIndex * AudioStreamDescription.kNativeSize;
format.setSampleRate(inFormatArray.getDoubleAt(offset));
offset += 8;
format.setFormatID(inFormatArray.getIntAt(offset));
offset += 4;
format.setFormatFlags(inFormatArray.getIntAt(offset));
offset += 4;
format.setBytesPerPacket(inFormatArray.getIntAt(offset));
offset += 4;
format.setFramesPerPacket(inFormatArray.getIntAt(offset));
offset += 4;
format.setBytesPerFrame(inFormatArray.getIntAt(offset));
offset += 4;
format.setChannelsPerFrame(inFormatArray.getIntAt(offset));
offset += 4;
format.setBitsPerChannel(inFormatArray.getIntAt(offset));
offset += 4;
return format;
}
private static void showCurrentFormat(AudioStream inStream)
throws CAException {
int formatSize = inStream.getPropertyInfoSize(
0, AHConstants.kAudioStreamPropertyPhysicalFormat);
CAMemoryObject formatData = new CAMemoryObject(formatSize,
true);
inStream.getProperty(
0, AHConstants.kAudioStreamPropertyPhysicalFormat,
formatData);
AudioStreamDescription format = getAudioFormat(formatData, 0);
System.out.println(format.toString());
}
}
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
--
Jeff Moore
Core Audio
Apple
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.