Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
thuydx55 committed Jan 31, 2016
2 parents 086d627 + 4ec22d1 commit 897d893
Show file tree
Hide file tree
Showing 522 changed files with 13,263 additions and 17,628 deletions.
6 changes: 3 additions & 3 deletions EZAudio.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "EZAudio"
s.version = "1.1.2"
s.version = "1.1.4"
s.summary = "A simple, intuitive audio framework for iOS and OSX useful for anyone doing audio processing and/or audio-based visualizations."
s.homepage = "https://github.com/syedhali/EZAudio"
s.screenshots = "https://s3-us-west-1.amazonaws.com/ezaudio-media/EZAudioSummary.png"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Syed Haris Ali" => "[email protected]" }
s.ios.deployment_target = '6.0'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.8'
s.source = { :git => "https://github.com/syedhali/EZAudio.git", :tag => s.version }
s.exclude_files = [ 'EZAudio/VERSION', 'EZAudio/TPCircularBuffer.{h,c}' ]
Expand All @@ -19,7 +19,7 @@ Pod::Spec.new do |s|
end

s.subspec 'Full' do |full|
full.dependency 'TPCircularBuffer', '~> 1.1'
full.dependency 'TPCircularBuffer', '1.1'
full.dependency 'EZAudio/Core'
end
end
23 changes: 19 additions & 4 deletions EZAudio/EZAudioFloatConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ OSStatus EZAudioFloatConverterCallback(AudioConverterRef inAudioConv
void *inUserData)
{
AudioBufferList *sourceBuffer = (AudioBufferList *)inUserData;

memcpy(ioData,
sourceBuffer,
sizeof(AudioBufferList) + (sourceBuffer->mNumberBuffers - 1) * sizeof(AudioBuffer));
sourceBuffer = NULL;

return noErr;
}

Expand Down Expand Up @@ -197,19 +200,31 @@ - (void)convertDataFromAudioBufferList:(AudioBufferList *)audioBufferList
toFloatBuffers:(float **)buffers
packetDescriptions:(AudioStreamPacketDescription *)packetDescriptions
{
if (frames == 0)
if (frames != 0)
{
//
// Make sure the data size coming in is consistent with the number
// of frames we're actually getting
//
for (int i = 0; i < audioBufferList->mNumberBuffers; i++) {
audioBufferList->mBuffers[i].mDataByteSize = frames * self.info->inputFormat.mBytesPerFrame;
}

}
else
{
//
// Fill out the audio converter with the source buffer
//
[EZAudioUtilities checkResult:AudioConverterFillComplexBuffer(self.info->converterRef,
EZAudioFloatConverterCallback,
audioBufferList,
&frames,
self.info->floatAudioBufferList,
packetDescriptions ? packetDescriptions : self.info->packetDescriptions)
operation:"Failed to fill complex buffer in float converter"];

//
// Copy the converted buffers into the float buffer array stored
// in memory
//
for (int i = 0; i < self.info->floatAudioBufferList->mNumberBuffers; i++)
{
memcpy(buffers[i],
Expand Down
21 changes: 21 additions & 0 deletions EZAudio/EZAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@

@class EZAudioPlayer;

//------------------------------------------------------------------------------
#pragma mark - Data Structures
//------------------------------------------------------------------------------

typedef NS_ENUM(NSUInteger, EZAudioPlayerState)
{
EZAudioPlayerStateEndOfFile,
EZAudioPlayerStatePaused,
EZAudioPlayerStatePlaying,
EZAudioPlayerStateReadyToPlay,
EZAudioPlayerStateSeeking,
EZAudioPlayerStateUnknown,
};

//------------------------------------------------------------------------------
#pragma mark - Notifications
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -151,6 +165,13 @@ reachedEndOfAudioFile:(EZAudioFile *)audioFile;
*/
@property (nonatomic, assign) BOOL shouldLoop;

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

/**
An EZAudioPlayerState value representing the current internal playback and file state of the EZAudioPlayer instance.
*/
@property (nonatomic, assign, readonly) EZAudioPlayerState state;

//------------------------------------------------------------------------------
#pragma mark - Initializers
//------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions EZAudio/EZAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
NSString * const EZAudioPlayerDidReachEndOfFileNotification = @"EZAudioPlayerDidReachEndOfFileNotification";
NSString * const EZAudioPlayerDidSeekNotification = @"EZAudioPlayerDidSeekNotification";

//------------------------------------------------------------------------------
#pragma mark - EZAudioPlayer (Interface Extension)
//------------------------------------------------------------------------------

@interface EZAudioPlayer ()
@property (nonatomic, assign, readwrite) EZAudioPlayerState state;
@end

//------------------------------------------------------------------------------
#pragma mark - EZAudioPlayer (Implementation)
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -179,6 +187,7 @@ + (instancetype)sharedAudioPlayer
- (void)setup
{
self.output = [EZOutput output];
self.state = EZAudioPlayerStateReadyToPlay;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -317,6 +326,7 @@ - (void)setVolume:(float)volume
- (void)play
{
[self.output startPlayback];
self.state = EZAudioPlayerStatePlaying;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -344,13 +354,16 @@ - (void)playAudioFile:(EZAudioFile *)audioFile
- (void)pause
{
[self.output stopPlayback];
self.state = EZAudioPlayerStatePaused;
}

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

- (void)seekToFrame:(SInt64)frame
{
self.state = EZAudioPlayerStateSeeking;
[self.audioFile seekToFrame:frame];
self.state = self.isPlaying ? EZAudioPlayerStatePlaying : EZAudioPlayerStatePaused;
[[NSNotificationCenter defaultCenter] postNotificationName:EZAudioPlayerDidSeekNotification
object:self];
}
Expand Down Expand Up @@ -384,6 +397,7 @@ - (OSStatus) output:(EZOutput *)output
{
[self pause];
[self seekToFrame:0];
self.state = EZAudioPlayerStateEndOfFile;
[[NSNotificationCenter defaultCenter] postNotificationName:EZAudioPlayerDidReachEndOfFileNotification
object:self];
}
Expand Down
49 changes: 37 additions & 12 deletions EZAudio/EZMicrophone.m
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ - (void)setAudioStreamBasicDescription:(AudioStreamBasicDescription)asbd
numberOfChannels:self.info->streamFormat.mChannelsPerFrame];
}

// set new stream format
//
// Set new stream format
//
self.info->streamFormat = asbd;
[EZAudioUtilities checkResult:AudioUnitSetProperty(self.info->audioUnit,
kAudioUnitProperty_StreamFormat,
Expand All @@ -447,18 +449,21 @@ - (void)setAudioStreamBasicDescription:(AudioStreamBasicDescription)asbd
sizeof(asbd))
operation:"Failed to set stream format on output scope"];

// allocate float buffers
//
// Allocate scratch buffers
//
UInt32 maximumBufferSize = [self maximumBufferSize];
BOOL isInterleaved = [EZAudioUtilities isInterleaved:asbd];
UInt32 channels = asbd.mChannelsPerFrame;
self.floatConverter = [[EZAudioFloatConverter alloc] initWithInputFormat:asbd];
self.info->floatData = [EZAudioUtilities floatBuffersWithNumberOfFrames:maximumBufferSize
numberOfChannels:channels];
self.info->audioBufferList = [EZAudioUtilities audioBufferListWithNumberOfFrames:maximumBufferSize
numberOfChannels:channels
interleaved:isInterleaved];

// notify delegate
numberOfChannels:channels
interleaved:isInterleaved];
//
// Notify delegate
//
if ([self.delegate respondsToSelector:@selector(microphone:hasAudioStreamBasicDescription:)])
{
[self.delegate microphone:self hasAudioStreamBasicDescription:asbd];
Expand Down Expand Up @@ -528,10 +533,14 @@ - (void)setDevice:(EZAudioDevice *)device
operation:"Couldn't set default device on I/O unit"];
#endif

// store device
//
// Store device
//
_device = device;

// notify delegate
//
// Notify delegate
//
if ([self.delegate respondsToSelector:@selector(microphone:changedDevice:)])
{
[self.delegate microphone:self changedDevice:device];
Expand Down Expand Up @@ -603,15 +612,27 @@ static OSStatus EZAudioMicrophoneCallback(void *inRefCon,
EZMicrophone *microphone = (__bridge EZMicrophone *)inRefCon;
EZMicrophoneInfo *info = (EZMicrophoneInfo *)microphone.info;

// render audio into buffer
//
// Make sure the size of each buffer in the stored buffer array
// is properly set using the actual number of frames coming in!
//
for (int i = 0; i < info->audioBufferList->mNumberBuffers; i++) {
info->audioBufferList->mBuffers[i].mDataByteSize = inNumberFrames * info->streamFormat.mBytesPerFrame;
}

//
// Render audio into buffer
//
OSStatus result = AudioUnitRender(info->audioUnit,
ioActionFlags,
inTimeStamp,
inBusNumber,
inNumberFrames,
info->audioBufferList);

// notify delegate of new buffer list to process
//
// Notify delegate of new buffer list to process
//
if ([microphone.delegate respondsToSelector:@selector(microphone:hasBufferList:withBufferSize:withNumberOfChannels:)])
{
[microphone.delegate microphone:microphone
Expand All @@ -620,10 +641,14 @@ static OSStatus EZAudioMicrophoneCallback(void *inRefCon,
withNumberOfChannels:info->streamFormat.mChannelsPerFrame];
}

// notify delegate of new float data processed
//
// Notify delegate of new float data processed
//
if ([microphone.delegate respondsToSelector:@selector(microphone:hasAudioReceived:withBufferSize:withNumberOfChannels:)])
{
// convert to float
//
// Convert to float
//
[microphone.floatConverter convertDataFromAudioBufferList:info->audioBufferList
withNumberOfFrames:inNumberFrames
toFloatBuffers:info->floatData];
Expand Down
8 changes: 4 additions & 4 deletions EZAudio/EZRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ - (void)appendDataFromBufferList:(AudioBufferList *)bufferList

//
// Perform the write
//
//
[EZAudioUtilities checkResult:ExtAudioFileWrite(self.info->extAudioFileRef,
bufferSize,
bufferList)
operation:"Failed to write audio data to recorded audio file"];
bufferSize,
bufferList)
operation:"Failed to write audio data to recorded audio file"];

//
// Notify delegate
Expand Down
39 changes: 26 additions & 13 deletions EZAudio/TPCircularBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@
#include "TPCircularBuffer.h"
#include <mach/mach.h>
#include <stdio.h>
#include <stdlib.h>

#define reportResult(result,operation) (_reportResult((result),(operation),strrchr(__FILE__, '/')+1,__LINE__))
static inline bool _reportResult(kern_return_t result, const char *operation, const char* file, int line) {
if (result != ERR_SUCCESS) {
if ( result != ERR_SUCCESS ) {
printf("%s:%d: %s: %s\n", file, line, operation, mach_error_string(result));
return false;
}
return true;
}

bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {

bool _TPCircularBufferInit(TPCircularBuffer *buffer, int32_t length, size_t structSize) {

assert(length > 0);

if ( structSize != sizeof(TPCircularBuffer) ) {
fprintf(stderr, "TPCircularBuffer: Header version mismatch. Check for old versions of TPCircularBuffer in your project\n");
abort();
}

// Keep trying until we get our buffer, needed to handle race conditions
int retries = 3;
while ( true) {
while ( true ) {

buffer->length = (int32_t)round_page(length); // We need whole page sizes

Expand All @@ -55,8 +63,8 @@ bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {
&bufferAddress,
buffer->length * 2,
VM_FLAGS_ANYWHERE); // allocate anywhere it'll fit
if (result != ERR_SUCCESS) {
if (retries-- == 0) {
if ( result != ERR_SUCCESS ) {
if ( retries-- == 0 ) {
reportResult(result, "Buffer allocation");
return false;
}
Expand All @@ -68,8 +76,8 @@ bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {
result = vm_deallocate(mach_task_self(),
bufferAddress + buffer->length,
buffer->length);
if (result != ERR_SUCCESS) {
if (retries-- == 0) {
if ( result != ERR_SUCCESS ) {
if ( retries-- == 0 ) {
reportResult(result, "Buffer deallocation");
return false;
}
Expand All @@ -92,8 +100,8 @@ bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {
&cur_prot, // unused protection struct
&max_prot, // unused protection struct
VM_INHERIT_DEFAULT);
if (result != ERR_SUCCESS) {
if (retries-- == 0) {
if ( result != ERR_SUCCESS ) {
if ( retries-- == 0 ) {
reportResult(result, "Remap buffer memory");
return false;
}
Expand All @@ -102,9 +110,9 @@ bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {
continue;
}

if (virtualAddress != bufferAddress+buffer->length) {
if ( virtualAddress != bufferAddress+buffer->length ) {
// If the memory is not contiguous, clean up both allocated buffers and try again
if (retries-- == 0) {
if ( retries-- == 0 ) {
printf("Couldn't map buffer memory to end of buffer\n");
return false;
}
Expand All @@ -117,6 +125,7 @@ bool TPCircularBufferInit(TPCircularBuffer *buffer, int length) {
buffer->buffer = (void*)bufferAddress;
buffer->fillCount = 0;
buffer->head = buffer->tail = 0;
buffer->atomic = true;

return true;
}
Expand All @@ -130,7 +139,11 @@ void TPCircularBufferCleanup(TPCircularBuffer *buffer) {

void TPCircularBufferClear(TPCircularBuffer *buffer) {
int32_t fillCount;
if (TPCircularBufferTail(buffer, &fillCount)) {
if ( TPCircularBufferTail(buffer, &fillCount) ) {
TPCircularBufferConsume(buffer, fillCount);
}
}

void TPCircularBufferSetAtomic(TPCircularBuffer *buffer, bool atomic) {
buffer->atomic = atomic;
}
Loading

0 comments on commit 897d893

Please sign in to comment.