Code Monkey home page Code Monkey logo

Comments (2)

appdog avatar appdog commented on July 18, 2024

Some sample code to trigger the error:

In the calling code add this

#import "ALSamplePlayer.h"
@property (nonatomic, retain) ALSamplePlayer *samplePlayer;
_samplePlayer = [[ALSamplePlayer alloc] initWithAudioController:_audioController];
if (YES)     [_samplePlayer schedulePlaybackWithSecondsFromNow:2.0];     // Gives the -10863 error, could it be because of AudioTimeStamp is out of date 'then'?
else        [_samplePlayer play];                                       // Works

ALSamplePlayer.h

#import <Foundation/Foundation.h>
#import "TheAmazingAudioEngine.h"

@interface ALSamplePlayer : NSObject

- (id)initWithAudioController:(AEAudioController*) audioController;
- (void)schedulePlaybackWithSecondsFromNow:(NSTimeInterval)seconds;
- (void)play;

@end

ALSamplePlayer.m

#import "ALSamplePlayer.h"

#define checkResult(result,operation) (_checkResult((result),(operation),strrchr(__FILE__, '/')+1,__LINE__))
static inline BOOL _checkResult(OSStatus result, const char *operation, const char* file, int line) {
    if ( result != noErr ) {
        int fourCC = CFSwapInt32HostToBig(result);
        NSLog(@"%s:%d: %s result %d %08X %4.4s\n", file, line, operation, (int)result, (int)result, (char*)&fourCC);
        return NO;
    }
    return YES;
}

@interface ALSamplePlayer () {

    AudioFileID _audioUnitFile;
}

@property (nonatomic, retain) AEBlockScheduler *scheduler;
@property (nonatomic, retain) AEAudioUnitChannel *audioUnitPlayer;
@property (nonatomic, retain) AEAudioController *audioController;

@end


@implementation ALSamplePlayer

- (id)initWithAudioController:(AEAudioController*) audioController {
    if (self = [super init]) {
        _audioController = audioController;
        [self setupPlayer];
        [self setupScheduler];
    }
    return self;
}

- (void)setupPlayer{

    // Create an audio unit channel (a file player)
    self.audioUnitPlayer = [[[AEAudioUnitChannel alloc] initWithComponentDescription:AEAudioComponentDescriptionMake(kAudioUnitManufacturer_Apple, kAudioUnitType_Generator, kAudioUnitSubType_AudioFilePlayer)
                                                                     audioController:_audioController
                                                                               error:NULL] autorelease];
    [_audioController addChannels:[NSArray arrayWithObjects:_audioUnitPlayer, nil]];
}

- (void)setupScheduler{

    self.scheduler = [[AEBlockScheduler alloc] initWithAudioController:_audioController];
    [_audioController addTimingReceiver:_scheduler];
}

- (void)schedulePlaybackWithSecondsFromNow:(NSTimeInterval)seconds{

    [self.scheduler scheduleBlock:^(const AudioTimeStamp *intervalStartTime, UInt32 offsetInFrames) {
        [self.audioController addChannels:[NSArray arrayWithObject:self.audioUnitPlayer]];
        NSLog(@"schedulePlayback triggered");

    } atTime:[AEBlockScheduler timestampWithSecondsFromNow:seconds] timingContext:AEAudioTimingContextOutput identifier:@"test"];
}

- (void)play {

    if ( !_audioUnitFile ) {
        NSURL *playerFile = [[NSBundle mainBundle] URLForResource:@"Southern Rock Drums" withExtension:@"m4a"];
        checkResult(AudioFileOpenURL((CFURLRef)playerFile, kAudioFileReadPermission, 0, &_audioUnitFile), "AudioFileOpenURL");
    }

    // Set the file to play
    checkResult(AudioUnitSetProperty(_audioUnitPlayer.audioUnit, kAudioUnitProperty_ScheduledFileIDs, kAudioUnitScope_Global, 0, &_audioUnitFile, sizeof(_audioUnitFile)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduledFileIDs)");

    // Determine file properties
    UInt64 packetCount;
    UInt32 size = sizeof(packetCount);
    checkResult(AudioFileGetProperty(_audioUnitFile, kAudioFilePropertyAudioDataPacketCount, &size, &packetCount),
                "AudioFileGetProperty(kAudioFilePropertyAudioDataPacketCount)");

    AudioStreamBasicDescription dataFormat;
    size = sizeof(dataFormat);
    checkResult(AudioFileGetProperty(_audioUnitFile, kAudioFilePropertyDataFormat, &size, &dataFormat),
                "AudioFileGetProperty(kAudioFilePropertyDataFormat)");

    // Assign the region to play
    ScheduledAudioFileRegion region;
    memset (&region.mTimeStamp, 0, sizeof(region.mTimeStamp));
    region.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
    region.mTimeStamp.mSampleTime = 0;
    region.mCompletionProc = NULL;
    region.mCompletionProcUserData = NULL;
    region.mAudioFile = _audioUnitFile;
    region.mLoopCount = 0;
    region.mStartFrame = 0;
    region.mFramesToPlay = packetCount * dataFormat.mFramesPerPacket;
    checkResult(AudioUnitSetProperty(_audioUnitPlayer.audioUnit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, &region, sizeof(region)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduledFileRegion)");

    // Prime the player by reading some frames from disk
    UInt32 defaultNumberOfFrames = 0;
    checkResult(AudioUnitSetProperty(_audioUnitPlayer.audioUnit, kAudioUnitProperty_ScheduledFilePrime, kAudioUnitScope_Global, 0, &defaultNumberOfFrames, sizeof(defaultNumberOfFrames)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduledFilePrime)");

    // Set the start time (now = -1)
    AudioTimeStamp startTime;
    memset (&startTime, 0, sizeof(startTime));
    startTime.mFlags = kAudioTimeStampSampleTimeValid;
    startTime.mSampleTime = -1; 
    checkResult(AudioUnitSetProperty(_audioUnitPlayer.audioUnit, kAudioUnitProperty_ScheduleStartTimeStamp, kAudioUnitScope_Global, 0, &startTime, sizeof(startTime)),
                "AudioUnitSetProperty(kAudioUnitProperty_ScheduleStartTimeStamp)");
}

@end

from theamazingaudioengine.

michaeltyson avatar michaeltyson commented on July 18, 2024

Actually, this isn't a bug. You can't (a) call objective-C from the schedule block as it's running on the realtime thread, or (b) manipulate the audio graph like that from the realtime thread. That's why you're getting -10863 (or 'kAudioUnitErr_CannotDoInCurrentContext')

from theamazingaudioengine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.