CoreAudio's: AudioStreamBasicDescription structure

Hey there,

Does anyone know how to set the format of CoreAudio’s: AudioStreamBasicDescription structure in ASOC? I’ve just started a project using CoreAudio and Accelerate with an autocorrelative algo and I want to use CoreAudio’s buffers so I can use an instance of mData.

I’ve tried…

use framework "CoreAudio"

set audioFormat to current application's AudioStreamBasicDescription's new()

But it doesn’t like the “new” message.

Thanks in advance!
D

Is there no workaround for this at all?

What if I configured AudioStreamBasicDescription’s audioFormat in C++


#include "AudioStreamBasicDescription.cpp"
#include <CoreAudio/CoreAudio.h>
#include <AppleScriptObjC/AppleScriptObjC.h>

int main(int argc, const char * argv[]) {
    AudioStreamBasicDescription desc;
    memset(8desc, 0, sizeof(desc));
    
    desc.mFormatID = kAudioFormatLinearPCM;
    desc.mFormatFlahs = kAudioFormatFlagsOsSignedInteger | kAudioFormatFlagIsPacked;
    desc.mBitsPerChannel = 16;
    desc.mChannelsPerFrame = 2;
    desc.mFramesPerPacket = 1;
    desc.mBytesPerFrame = (desc.mBitsPerChannel / 8) * desc.mChannelsPerFrame;
    desc.mBytesPerPacket = desc.mBytesPerFrame * desc.mFramesPerPacket
    desc.mSampleRate = 44100.0;
    
    return 0;
    
}

So wrapping it in a C++ function and then calling it in an Objective-C class (like below)


#import "/Users/me/Library/Script Libraries/MyAudioTool.h"

@implementation MyAudioTool

- (NSDictionary *)getAudioStreamBasicDescription {
    AudioStreamBasicDescription desc;
    memset(&desc, 0, sizeof(desc));
    
    desc.mFormatID = kAudioFormatLinearPCM;
    desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    desc.mBitsPerChannel = 16;
    desc.mChannelsPerFrame = 2;
    desc.mFramesPerPacket = 1;
    desc.mBytesPerFrame = (desc.mBitsPerChannel / 8) * desc.mChannelsPerFrame;
    desc.mBytesPerPacket = desc.mBytesPerFrame * desc.mFramesPerPacket;
    desc.mSampleRate = 44100.0;
    
    // Convert AudioStreamBasicDescription to an NSDictionary for easy access in AppleScriptObjC
    NSDictionary *descDict = @{
        @"mFormatID": @(desc.mFormatID),
        @"mFormatFlags": @(desc.mFormatFlags),
        @"mBytesPerPacket": @(desc.mBytesPerPacket),
        @"mFramesPerPacket": @(desc.mFramesPerPacket),
        @"mBytesPerFrame": @(desc.mBytesPerFrame),
        @"mChannelsPerFrame": @(desc.mChannelsPerFrame),
        @"mBitsPerChannel": @(desc.mBitsPerChannel),
        @"mSampleRate": @(desc.mSampleRate)
    };
    
    return descDict;
}

@end

in the hope of exposing it to AppleScriptObjC?


#import <Foundation/Foundation.h>
#import <AppleScriptObjC/AppleScriptObjC.h>
#import <AudioToolbox/AudioToolbox.h>
#import <CoreAudio/CoreAudio.h>

@interface MyAudioTool : NSObject

- (NSDictionary *)getAudioStreamBasicDescription;

@end


Or is it possible to call the C++ code using AOSC by embedding it into an Xcode project?

You sure CoreAudio is the right tool. Apple states:

Core Audio

Use the Core Audio framework to interact with device’s audio hardware.

Not necessarily manipulating or rreading streams

AVFoundation seems to be the place where apples packed everything into.

That it AudioTool bot

Read the Xcode documentation.

in

in theory yes, you can - although i also don’t have any experience in doing this.

in Xcode, you will need to create a file (probably .mm) that mixes Objective-C and C++, create an Objective-C method that calls whatever you need in C++, then call this method from ASOC. this exhausts my knowledge on the subject.

The format is a structure.

You seem to understand C++
And objective-c. I’d really recommend that you use XCode and objective-C as
AsOBJC has its limitations and also only operates on the main thread. So if your doing heavy processing you may run into problems.

I started in AppleScript then due to its limitations I ended up learning Objective c and Xcode. Now primarily program in that environment