Copy/Paste Audio Samples in Cocoa

199 Views Asked by At

I am creating a little audio editor using the Cocoa frameworks for Mac OS X. I implemented "copy" by writing the selected samples to a temporary WAV file, then using NSSound to load the URL and then push the data to the pasteboard (writeToPasteboard:).

Now I am working on paste. I create an NSSound from the pasteboard and am now stuck. How do I get access to the samples?

1

There are 1 best solutions below

0
Peter Hosey On

You don't. NSSound doesn't give you access to raw sample data.

You'll need to use the Audio Toolbox framework instead. It provides two APIs for reading and writing audio in files: Audio File Services is the lower-level of the two, whereas Extended Audio File Services supports compression and other features.

You may want to use the callbacks-based APIs in Audio File Services, since these could save you from having to use a temporary file. Instead, you'd set an NSMutableData object as the “client data” of the audio file, then implement “write” by copying the bytes into the relevant range of that data (setting its length first if needed) and “read” by copying some range of the data out.