Skip to content

Commit

Permalink
Merge pull request #171 from syedhali/output_rewrite
Browse files Browse the repository at this point in the history
Rewrote EZOutput using AUGraph
  • Loading branch information
syedhali committed Jun 30, 2015
2 parents c7727f8 + 6a44909 commit f98d520
Show file tree
Hide file tree
Showing 16 changed files with 1,366 additions and 553 deletions.
34 changes: 25 additions & 9 deletions EZAudio/EZAudioDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
*/
+ (NSArray *)inputDevices;

//------------------------------------------------------------------------------

/**
Enumerates all the available output devices and returns the result in an NSArray of EZAudioDevice instances.
@return An NSArray of output EZAudioDevice instances.
*/
+ (NSArray *)outputDevices;

#if TARGET_OS_IPHONE

/**
Expand All @@ -42,6 +50,13 @@
*/
+ (EZAudioDevice *)currentInputDevice;

/**
Provides the current EZAudioDevice that is being used to output audio.
- iOS only
@return An EZAudioDevice instance representing the currently selected ouotput device.
*/
+ (EZAudioDevice *)currentOutputDevice;

//------------------------------------------------------------------------------

/**
Expand All @@ -52,23 +67,24 @@
+ (void)enumerateInputDevicesUsingBlock:(void(^)(EZAudioDevice *device,
BOOL *stop))block;

#elif TARGET_OS_MAC
//------------------------------------------------------------------------------

/**
Enumerates all the available devices and returns the result in an NSArray of EZAudioDevice instances.
- OSX only
@return An NSArray of input and output EZAudioDevice instances.
Enumerates all the available output devices.
- iOS only
@param block When enumerating this block executes repeatedly for each EZAudioDevice found. It contains two arguments - first, the EZAudioDevice found, then a pointer to a stop BOOL to allow breaking out of the enumeration)
*/
+ (NSArray *)devices;
+ (void)enumerateOutputDevicesUsingBlock:(void (^)(EZAudioDevice *device,
BOOL *stop))block;

//------------------------------------------------------------------------------
#elif TARGET_OS_MAC

/**
Enumerates all the available output devices and returns the result in an NSArray of EZAudioDevice instances.
Enumerates all the available devices and returns the result in an NSArray of EZAudioDevice instances.
- OSX only
@return An NSArray of output EZAudioDevice instances.
@return An NSArray of input and output EZAudioDevice instances.
*/
+ (NSArray *)outputDevices;
+ (NSArray *)devices;

//------------------------------------------------------------------------------

Expand Down
61 changes: 61 additions & 0 deletions EZAudio/EZAudioDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ + (EZAudioDevice *)currentInputDevice

//------------------------------------------------------------------------------

+ (EZAudioDevice *)currentOutputDevice
{
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionPortDescription *port = [[[session currentRoute] outputs] firstObject];
AVAudioSessionDataSourceDescription *dataSource = [session outputDataSource];
EZAudioDevice *device = [[EZAudioDevice alloc] init];
device.port = port;
device.dataSource = dataSource;
return device;
}

//------------------------------------------------------------------------------

+ (NSArray *)inputDevices
{
__block NSMutableArray *devices = [NSMutableArray array];
Expand All @@ -61,6 +74,18 @@ + (NSArray *)inputDevices

//------------------------------------------------------------------------------

+ (NSArray *)outputDevices
{
__block NSMutableArray *devices = [NSMutableArray array];
[self enumerateOutputDevicesUsingBlock:^(EZAudioDevice *device, BOOL *stop)
{
[devices addObject:device];
}];
return devices;
}

//------------------------------------------------------------------------------

+ (void)enumerateInputDevicesUsingBlock:(void (^)(EZAudioDevice *, BOOL *))block
{
if (!block)
Expand Down Expand Up @@ -101,6 +126,42 @@ + (void)enumerateInputDevicesUsingBlock:(void (^)(EZAudioDevice *, BOOL *))block

//------------------------------------------------------------------------------

+ (void)enumerateOutputDevicesUsingBlock:(void (^)(EZAudioDevice *, BOOL *))block
{
if (!block)
{
return;
}

AVAudioSessionRouteDescription *currentRoute = [[AVAudioSession sharedInstance] currentRoute];
NSArray *portDescriptions = [currentRoute outputs];

BOOL stop;
for (AVAudioSessionPortDescription *outputDevicePortDescription in portDescriptions)
{
// add any additional sub-devices
NSArray *dataSources = [outputDevicePortDescription dataSources];
if (dataSources.count)
{
for (AVAudioSessionDataSourceDescription *outputDeviceDataSourceDescription in dataSources)
{
EZAudioDevice *device = [[EZAudioDevice alloc] init];
device.port = outputDevicePortDescription;
device.dataSource = outputDeviceDataSourceDescription;
block(device, &stop);
}
}
else
{
EZAudioDevice *device = [[EZAudioDevice alloc] init];
device.port = outputDevicePortDescription;
block(device, &stop);
}
}
}

//------------------------------------------------------------------------------

- (NSString *)name
{
NSMutableString *name = [NSMutableString string];
Expand Down
8 changes: 5 additions & 3 deletions EZAudio/EZAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ -(void)setAudioFile:(EZAudioFile *)audioFile {
_audioFile = [EZAudioFile audioFileWithURL:audioFile.url];
_audioFile.delegate = self;
NSAssert(_output,@"No output was found, this should by default be the EZOutput shared instance");
[_output setAudioStreamBasicDescription:self.audioFile.clientFormat];
[_output setInputFormat:self.audioFile.clientFormat];
}

-(void)setOutput:(EZOutput*)output {
_output = output;
_output.outputDataSource = self;
_output.dataSource = self;
}

#pragma mark - Methods
Expand Down Expand Up @@ -278,9 +278,10 @@ -(void)audioFile:(EZAudioFile *)audioFile updatedPosition:(SInt64)framePosition
}

#pragma mark - EZOutputDataSource
-(void) output:(EZOutput *)output
-(OSStatus) output:(EZOutput *)output
shouldFillAudioBufferList:(AudioBufferList *)audioBufferList
withNumberOfFrames:(UInt32)frames
timestamp:(const AudioTimeStamp *)timestamp
{
if (self.audioFile)
{
Expand All @@ -294,6 +295,7 @@ -(void) output:(EZOutput *)output
[self seekToFrame:0];
}
}
return noErr;
}

@end
11 changes: 11 additions & 0 deletions EZAudio/EZAudioUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ typedef struct
TPCircularBuffer circularBuffer;
} EZPlotHistoryInfo;

//------------------------------------------------------------------------------

/**
A data structure that holds information about a node in the context of an AUGraph.
*/
typedef struct
{
AudioUnit audioUnit;
AUNode node;
} EZAudioNodeInfo;

//------------------------------------------------------------------------------
#pragma mark - Types
//------------------------------------------------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions EZAudio/EZMicrophone.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//------------------------------------------------------------------------------

/**
The delegate for the EZMicrophone provides a receiver for the incoming audio data events. When the microphone has been successfully internally configured it will try to send its delegate an AudioStreamBasicDescription describing the format of the incoming audio data.
The EZMicrophoneDelegate for the EZMicrophone provides a receiver for the incoming audio data events. When the microphone has been successfully internally configured it will try to send its delegate an AudioStreamBasicDescription describing the format of the incoming audio data.
The audio data itself is sent back to the delegate in various forms:
Expand All @@ -55,10 +55,9 @@
///-----------------------------------------------------------

/**
Called anytime the input device changes on an `EZMicrophone` instance. Mac only.
Called anytime the input device changes on an `EZMicrophone` instance.
@param microphone The instance of the EZMicrophone that triggered the event.
@param device The instance of the new EZAudioDevice the microphone is using to pull input.
@param notification Incase the device changed because of a notification (like from AVAudioSession) then we provide that notification to give the full context of the change.
*/
- (void)microphone:(EZMicrophone *)microphone changedDevice:(EZAudioDevice *)device;

Expand All @@ -77,7 +76,7 @@
///-----------------------------------------------------------

/**
Returns back a float array of the audio received. This occurs on the background thread so any drawing code must explicity perform its functions on the main thread.
This method provides an array of float arrays of the audio received, each float array representing a channel of audio data This occurs on the background thread so any drawing code must explicity perform its functions on the main thread.
@param microphone The instance of the EZMicrophone that triggered the event.
@param buffer The audio data as an array of float arrays. In a stereo signal buffer[0] represents the left channel while buffer[1] would represent the right channel.
@param bufferSize The size of each of the buffers (the length of each float array).
Expand Down Expand Up @@ -304,7 +303,9 @@
*/
- (AudioUnit *)audioUnit;

//------------------------------------------------------------------------------
#pragma mark - Setters
//------------------------------------------------------------------------------

///-----------------------------------------------------------
/// @name Customizing The Microphone Stream Format
Expand Down
8 changes: 5 additions & 3 deletions EZAudio/EZMicrophone.m
Original file line number Diff line number Diff line change
Expand Up @@ -535,21 +535,23 @@ - (void)setDevice:(EZAudioDevice *)device
- (void)setOutput:(EZOutput *)output
{
_output = output;
[_output setAudioStreamBasicDescription:self.audioStreamBasicDescription];
_output.outputDataSource = self;
_output.inputFormat = self.audioStreamBasicDescription;
_output.dataSource = self;
}

//------------------------------------------------------------------------------
#pragma mark - EZOutputDataSource
//------------------------------------------------------------------------------

- (void) output:(EZOutput *)output
- (OSStatus) output:(EZOutput *)output
shouldFillAudioBufferList:(AudioBufferList *)audioBufferList
withNumberOfFrames:(UInt32)frames
timestamp:(const AudioTimeStamp *)timestamp
{
memcpy(audioBufferList,
self.info->audioBufferList,
sizeof(AudioBufferList) + (self.info->audioBufferList->mNumberBuffers - 1)*sizeof(AudioBuffer));
return noErr;
}

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit f98d520

Please sign in to comment.