0

Quick overview: I'm developing a Mac Swift desktop audio application. I've run into a situation that seems to require me to hit the AudioToolbox C api's in order to get an AudioFileMarkerList. There doesn't seem to be support for this in any of the newer AVStuff, so it seems you still need to work with the AudioToolbox API.

I would love to hear from someone experienced with dealing with these C structs and even better, linking them with Swift. Or, if there is another way to retrieve markers from soundfiles that I'm missing - I'd love to know that too.

Ryan Francesconi
  • 988
  • 7
  • 17

1 Answers1

1

Here is the answer, in case anyone comes across this in the future.

// get size of markers property (dictionary)
UInt32          propSize;
UInt32          writable;

[EZAudioUtilities checkResult:AudioFileGetPropertyInfo( self.audioFileID,
                                                       kAudioFilePropertyMarkerList,
                                                       &propSize,
                                                       &writable)
                    operation:"Failed to get the size of the marker list"];

size_t length = NumBytesToNumAudioFileMarkers( propSize );

// allocate enough space for the markers.
AudioFileMarkerList markers[ length ];

if ( length > 0 ) {
    // pull marker list
    [EZAudioUtilities checkResult:AudioFileGetProperty( self.audioFileID,
                                                       kAudioFilePropertyMarkerList,
                                                       &propSize,
                                                       &markers)
                        operation:"Failed to get the markers list"];

} else {
    return NULL;
}

//NSLog(@"# of markers: %d\n", markers->mNumberMarkers );
Ryan Francesconi
  • 988
  • 7
  • 17
  • Is there any way to convert this to Swift? I've been trying everything for a week https://stackoverflow.com/questions/44743239/swift-retrieve-audio-file-marker-list-from-url – MysteryPancake Jul 01 '17 at 08:47
  • I find it makes sense to leave C code in C, but it should work in swift as well. If I get a moment soon I'll convert it for you. – Ryan Francesconi Jul 07 '17 at 18:20
  • See here: https://stackoverflow.com/questions/44743239/swift-retrieve-audio-file-marker-list-from-url/45316203#45316203 for an almost working Swift example. Overall, it's much simpler to just use Obj-C though for this. Less code. – Ryan Francesconi Jul 26 '17 at 02:09
  • I think you are allocating too much memory, the correct way would be AudioFileMarkerList* markers; markers = reinterpret_cast(new Byte[length]); – Alex Darsonik Sep 05 '17 at 08:21