Derived from: public BSubscriber
Declared in: <media/AudioSubscriber.h>
BAudioSubscriber objects perform two functions:
Ultimately, the first point is the more interesting of the two: Recording, generating, and manipulating sound data is a bit more amusing than simply setting the volume levels of the hardware devices. But to understand how and what data is received by your BAudioSubscriber objects, and what happens when you broadcast data through an object, you should first understand how the hardware is configured. The next section examines the sound hardware; following that is a description of the sound data that appears in your application.
The sound hardware consists of a number of physical devices (jacks, converters, and the like), a signal path that routes audio data between these devices, and "control points" along the signal path that let you adjust the format and flow of the audio data. These elements are depicted in the following illustration.
There are three analog audio input devices:
Note that the microphone signal doesn't have a through path.
To bring an analog signal into your application (so you can record it, for example), the signal must pass through the input MUX:
There are two sound data converters, the analog-to-digital converter (ADC) and the digital-to-analog converter (DAC):
Acting as a sort of "short-circuit" between these two devices is the loopback:
The ADC stream and DAC stream are the centerpieces of the BAudioSubscriber class. By subscribing to the ADC stream you can receive the samples that are emitted by the ADC; and by subscribing to the DAC stream, you can send buffers of digital sound data to the DAC.
To enter the ADC stream you must create a BAudioSubscriber, subscribe to the stream (by passing B_ADC_STREAM as the first argument to Subscribe()), and then call EnterStream(). At that point, your object will begin receiving buffers of ADC-converted data from the Audio Server. The buffers show up as arguments to the object's stream function.
Similarly, the DAC stream universe is broached by subscribing to and entering the B_DAC_STREAM.
If you're unfamiliar with the concepts of subscription, entering a stream, and the stream function, take a break and read the BSubscriber specification.
The output devices take analog signals and broadcast them to hardware that can turn the signals into sound.
The BAudioSubscriber class defines a number of functions that control the sound hardware and that query the state of the hardware. Note that you can call these functions without first subscribing to one or the other of the audio streams.
To set the volume level of a particular sound device, you use BAudioSubscriber's SetVolume() function. The function takes three arguments:
The device constants are listed below; they correspond to the named control points shown in the hardware diagram:
All volume levels are floating-point numbers in the range [0.0, 1.0], where 0.0 is inaudible, and 1.0 is maximum volume. If you're setting a single-channel device (the speaker), the left channel level is used--the value you pass as the right channel level is ignored. If you want to set one channel of a stereo device but leave the other at its present level, pass the B_NO_CHANGE constant for the no-change channel.
In the example below, a BAudioSubscriber is used to set the volume of the CD-through signal:
BAudioSubscriber *setter = BAudioSubscriber("setter"); /* Set the right channel of the CD through signal * to half the maximum volume, and leave the left channel * alone. */ setter->SetVolume(B_CD_THROUGH, B_NO_CHANGE, 0.5);
To mute a device, you disable it; or, more precisely, you set it to be not enabled. This is done through the EnableDevice() function. As with SetVolume(), the function's first argument is the constant that represents the device you want to control. The second argument is a boolean that states whether you want to enable (TRUE) or disable (FALSE) the device. For example:
/* Mute the internal speaker. */ setter->EnableDevice(B_SPEAKER_OUT, FALSE);
The GetVolume() and IsDeviceEnabled() functions retrieve the current volume and enabled state of a given device. (As a convenience, GetVolume() returns volume and enabled status; see the function description for details.)
To select the analog device that will feed into the MUX, you use the SetADCInput() function (the signal into the MUX goes to the ADC, hence the name of the function). The input devices are represented by these constants:
The ADCInput() function returns the current input device.
The microphone's 20 dB boost is toggled through the BoostMic() function. The state of the boost is retrieved by IsMicBoosted().
Sounds are propagated by the continuous fluctuation of air pressure. This fluctuation is called a sound wave. The digital representation of a sound wave consists of a series of discrete measurements of the instantaneous pressure (or amplitude) of the wave at precise, (typically) equally-spaced points in time. Each measurement is called a sample . There are five attributes that characterize a digital sound sample:
The Be sound hardware (both the ADC and the DAC) allows the following sound attribute settings:
The ADC and DAC use the same sampling rate. You can set the sampling rate through BAudioSubscriber's SetSamplingRate() function, but you can't specify which device you intend the setting to apply to: It always applies to both.
As for the other sound data parameters (sample size, byte order, channel count, and sample format), the ADC and the DAC maintain independent settings. For example, you can set the DAC to expect two-byte linear samples while the ADC produces one-byte mu-law samples. The functions that set these sound format attributes are SetDACSampleInfo() and SetADCSampleInfo(). Your BAudioSubscriber needn't subscribe before setting the DAC or ADC parameters.
A BAudioSubscriber object receives buffers of sound data from one of the Audio Server's two buffer streams:
The ADC stream isn't automatically connected to the DAC stream. If you want to grab data from the ADC and send it to the DAC, you have to subscribe to both streams through two separate BAudioSubscriber objects, and then coordinate the hand off of data from the ADC subscriber to the DAC subscriber.
BAudioSubscriber(const char *name)
Creates and returns a new BAudioSubscriber object. The object is given the name that you pass as name; the name is provided as a convenience and needn't be unique.
After creating a BAudioSubscriber, you typically do the following (in this order):
See also: BSubscriber::Subscribe() , BSubscriber::EnterStream()
virtual ~BAudioSubscriber(void)
Destroys the BAudioSubscriber.
long ADCInput(void) long SetADCInput(long device)
These functions get and set the device that feeds into the MUX (and so to the ADC, hence the name). Valid device constants are:
You don't need to be subscribed to the ADC stream in order to call these functions.
long BoostMic(bool boost) bool IsMicBoosted(void)
BoostMic() enables or disables the 20 dB boost on the microphone signal. IsMicBoosted() returns the state of the boost.
long GetADCSampleInfo(long *bytesPerSample, long *channelCount, long *byteOrder, long *sampleFormat) long GetDACSampleInfo(long *bytesPerSample, long *channelCount, long *byteOrder, long *sampleFormat) long SamplingRate(void)
These functions return the values of the various sound data parameters. GetADC... returns (by reference) the sound parameters that are used in the ADC stream. GetDAC... does the same for the DAC stream. SamplingRate() returns the sampling rate directly; the sampling rate is held in common by the two streams.
See the description of SetADCSampleInfo() for a list of parameter values that you can expect to see.
See also: SetADCSampleInfo()
long SetADCSampleInfo(long bytesPerSample, long channelCount, long byteOrder, long sampleFormat) long SetDACSampleInfo(long bytesPerSample, long channelCount, long byteOrder, long sampleFormat) long SetSamplingRate(long samplingRate)
These functions set the values of the sound data attributes used by (respectively) the ADC stream (SetADC...), DAC stream (SetDAC...), and both streams ( SetSamplingRate()). The arguments to the SetADC... and SetDAC... functions are:
The SetSamplingRate() function sets the sampling rate for both the ADC stream and the DAC stream:
These functions don't flinch at wildly inappropriate parameter settings. The values of the arguments that you pass in are always rounded to the nearest acceptable value for the particular parameter.
See also: GetADCSampleInfo()
long SetVolume(long device, float leftVolume, float rightVolume) long GetVolume(long device, float *leftVolume, float *rightVolume, bool isEnabled) long EnableDevice(long device, bool enable) bool IsDeviceEnabled(long device)
These functions set and return (by reference) the left and right volume levels, and the enabled status of the device that's identified by the first argument. Valid device constants are:
Volume values are floating-point numbers that are clipped within the range [0.0, 1.0]. Across this range, the amplitude of a sound waveform is increased logarithmically; this results, perceptually, in a linear increase in volume.
Note that the speaker is monophonic; when you set or retrieve the volume of the B_SPEAKER_OUT device, only the leftVolume argument is used.
You needn't be subscribed to call these functions.
The Be Book, HTML Edition, for Developer Release 8 of the Be Operating System.
Copyright © 1996 Be, Inc. All rights reserved.
Be, the Be logo, BeBox, BeOS, BeWare, and GeekPort are trademarks of Be, Inc.
Last modified September 6, 1996.