Hello guys! I think I am very close to being able to do an audio copy
out of my app and have GarageBand recognize it. The copy from my app
happens without indicent. It causes paste to become available from
GarageBand. But then, I get "Unsupported file format" "The pasted
audio data uses an unsupported file format". GarageBand supports
uncompressed audio files with 44.1khz sample rate and 16-bit depth."
So that is exactly what I am doing. I must have the format wrong.
Can anybody see anything glaringly wrong in this code (that implements
the audioCopyWrite functions by casting to the appropriate type and
assigning... not sure if there are endian problems in flags, etc.)
I implement this file format as best as I can tell:
http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CAFSpec/CAF_overview/CAF_overview.html#//apple_ref/doc/uid/TP40001862-CH209-TPXREF101
If I paste a string of bits and note that it's audio, the format being
wrong seems to be the only thing it could be. (I also noted that
there doesn't seem to be any kind of end-of-stream marker, that it
uses buffer size to figure that out...doesn't seem right because you
need to be able to parse these as they pass through a data pipe.).
Anyways... here:
void audioCopy()
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
// Write the straight loop in
int cursor=0;
NSLog(@"writing CAF format to UIPasteboard");
NSLog(@"writing type,version,flags");
//Type,Version,Flags
audioCopyWrite32(copyBuffer8,&cursor,'caff'); //type of file
(magic)
audioCopyWrite16(copyBuffer8,&cursor,1); //version
audioCopyWrite16(copyBuffer8,&cursor,0); //flags
//Chunk header, type, len
audioCopyWrite32(copyBuffer8,&cursor,'desc'); //description header
follows
audioCopyWrite64(copyBuffer8,&cursor,32); //32 to bytes
required for it
//Description
audioCopyWrite64(copyBuffer8,&cursor,44100); //rate
audioCopyWrite32(copyBuffer8,&cursor,'lpcm'); //format
audioCopyWrite32(copyBuffer8,&cursor,0); //flags -- little
endian
audioCopyWrite32(copyBuffer8,&cursor,4); //bytes per packet
audioCopyWrite32(copyBuffer8,&cursor,1); //frames per packet
audioCopyWrite32(copyBuffer8,&cursor,2); //channels
audioCopyWrite32(copyBuffer8,&cursor,16); //bits per channel
NSLog(@"writing dataheader");
audioCopyWrite32(copyBuffer8,&cursor,'data'); //data follows
audioCopyWrite64(copyBuffer8,&cursor,loop.size*4); //4 bytes per
stereo sample
NSLog(@"writing stereo 16bit 44.1hz stereo data of length
%d",loop.size);
for(int i=0; i<loop.size; i++)
{
//Is left or right first? It will matter when I get paste to
work on GB
int16_t sampleL = floatToSample16(loopBufferL[i +
loop.firstOffset + loop.secondOffset]);
int16_t sampleR = floatToSample16(loopBufferR[i +
loop.firstOffset + loop.secondOffset]);
audioCopyWrite16(copyBuffer8,&cursor,sampleL);
audioCopyWrite16(copyBuffer8,&cursor,sampleR);
}
//This actually causes Garage Band to try.. formatting just seems
wrong
NSLog(@"copy data to pasteboard");
NSData *dataFile = [NSData dataWithBytes:copyBuffer8
length:cursor];
[board setData:dataFile forPasteboardType:
(NSString*)kUTTypeAudio];
}