I'm looking for a way to combine separate audio files in one audio file. The starting separate files i can have are WAV or CAF (doesnt matter to me) and I would like to stitch them together into one file such as MIDI or anything other file type thats possible. I'm not sure if this is possible on iPhone but I would be able to make a mac desktop application to export to if needed to do the stitching together there. So pretty much what I'm asking is does anybody know of a not too complex (but share anyway even if insanely complex) way on iPhone OR OS X to combine separate audio files into one file. Thanks!
There is a great audio editor that is free and is compatible with several platforms (Mac, Windows, Linux). It is called Audacity, and can be found here:
Oh thanks for your help but I guess i didnt convey that I need to do this programmatically in my own app I am creating. I need a framework or some classes that can combine audio files. I need to create my own app that can combine audio files, not use one that already exists.
Nitrex... havent seen you around here in a while... how's things been?
Anyways, I don't know about combining audio files of different types, but for same type audio files, same compression, same everything:
Could you just read in file1 using NSFileHandle's readData, write it out to another file, do the same for file2, just append it to the end of file 1. The only thing you need to determine is how many bytes are at the front of file2 that you can skip because they have unneeded file information that's not part of the audio.
Ah... since you asked about OS X, I assumed you were just looking for a tool, not necessarily code.
I have seen some threads about using ffmpeg on the iphone. ffmpeg is generally used for Video, but works with audio as well. You might try searching on using ffmpeg on the iphone as the source code for that is freely available as well.
Nitrex... havent seen you around here in a while... how's things been?
Anyways, I don't know about combining audio files of different types, but for same type audio files, same compression, same everything:
Could you just read in file1 using NSFileHandle's readData, write it out to another file, do the same for file2, just append it to the end of file 1. The only thing you need to determine is how many bytes are at the front of file2 that you can skip because they have unneeded file information that's not part of the audio.
Yea i figured it was something like that except I don't know how to do that. Any advice on how you would go about it such as a class reference to look at or some methods you would be using. By the way all files would be of the same type, i just meant if WAV was not supported I can change all my files to CAF.
Ah... since you asked about OS X, I assumed you were just looking for a tool, not necessarily code.
I have seen some threads about using ffmpeg on the iphone. ffmpeg is generally used for Video, but works with audio as well. You might try searching on using ffmpeg on the iphone as the source code for that is freely available as well.
Thanks for the info ill check that out. By asking about OS X i meant if there was no such technology on the iPhone I could make an OS X desktop application to handle this code and export data from the iPhone app to it.
This should get you on the right track. Mix and match pseudo code
Code:
-(void) concatFiles( NSString* path1, NSString* path2, NSString* combinedPath )
{
NSFileHandle * f1Handle = [NSFileHandle fileHandleForReadingAtPath: path1];
NSFileHandle *f2Handle = [NSFileHandle fielHandleForReadingAtPath: path2];
//might need to check if file exists first, if not create it using NSFileManager's createFileAtPath
NSFileHandle * combinedHandle = [NSFileHandle fileHandleForWritingAtPath: combinedPath];
// read data in small chunks to avoid hogging memory.
NSData* read = [f1Handle readDataOfLength: CHUNK_SIZE] ;
while( length of read != 0 )
{
[combineHandle writeData: read];
read = [f1Handle readDataOfLength: CHUNK_SIZE] ;
}
[f1Handle close];
//now do the same for f2Handle
//might need to ignore a few bytes in the front of file2 that isnt audio
[f2Handle readDataOfLength: HEADER_LENGTH];
NSData* read = [f2Handle readDataOfLength: CHUNK_SIZE] ;
while (... )
{
}
[f2Handle close];
[combineHandle close];
}
This should get you on the right track. Mix and match pseudo code
Code:
-(void) concatFiles( NSString* path1, NSString* path2, NSString* combinedPath )
{
NSFileHandle * f1Handle = [NSFileHandle fileHandleForReadingAtPath: path1];
NSFileHandle *f2Handle = [NSFileHandle fielHandleForReadingAtPath: path2];
//might need to check if file exists first, if not create it using NSFileManager's createFileAtPath
NSFileHandle * combinedHandle = [NSFileHandle fileHandleForWritingAtPath: combinedPath];
// read data in small chunks to avoid hogging memory.
NSData* read = [f1Handle readDataOfLength: CHUNK_SIZE] ;
while( length of read != 0 )
{
[combineHandle writeData: read];
read = [f1Handle readDataOfLength: CHUNK_SIZE] ;
}
[f1Handle close];
//now do the same for f2Handle
//might need to ignore a few bytes in the front of file2 that isnt audio
[f2Handle readDataOfLength: HEADER_LENGTH];
NSData* read = [f2Handle readDataOfLength: CHUNK_SIZE] ;
while (... )
{
}
[f2Handle close];
[combineHandle close];
}
Wow thank you so much that will probably do it. That is so awesome! I haven't done anything with the NSFileHandle yet so I didn't know about it and it's methods. What do you recommend for the values of CHUNK_SIZE and HEADER_LENGTH?
Also in reference to me not being here for a while I have been very busy making tons of apps for the app store with various companies. I am also involved with the creation of highly developed new tutorial website that will be ready soon. Also busy finishing high school =P.
Just wondering if anyone has implemented the code shown above. Below is the code I've got working so far. I'm guessing I got the HEADER_LENGTH right (anything less creates a file Quicktime can't play). My problem with the code below is that the file at combinedPath seems to only have the data for the first file. Is there a footer in audio files that has to be removed from the first file (similar to how we ignore the header of the second)? When debugging it runs through the second while loop several times so I think it's writing to the combined path but for some reason QuickTime thinks the file ends after the duration of the first audio.
[EDIT: After digging around the interwebs some more I think the problem is that the header info for the first file includes a duration property which is telling QuickTime the length of File 1. If anyone has any advice on how to override the header with the length of both files combined I think that would solve my problem]
Code:
#define CHUNK_SIZE 1000
#define HEADER_LENGTH 251
-(void) concatFiles:(NSString*) path1 And: (NSString*) path2 SaveTo:(NSString*) combinedPath
{
NSFileManager *fm = [[NSFileManager alloc] init];
NSFileHandle *f1Handle = [NSFileHandle fileHandleForReadingAtPath: path1];
NSFileHandle *f2Handle = [NSFileHandle fileHandleForReadingAtPath: path2];
//might need to check if file exists first, if not create it using NSFileManager's createFileAtPath
if(![fm fileExistsAtPath:combinedPath]){
[fm createFileAtPath:combinedPath contents:nil attributes:nil];
}
NSFileHandle *combinedHandle = [NSFileHandle fileHandleForWritingAtPath: combinedPath];
NSMutableData *read = [[NSMutableData alloc] initWithLength:CHUNK_SIZE];
// read data in small chunks to avoid hogging memory.
[read setData:[f1Handle readDataOfLength: CHUNK_SIZE]];
while([read length] != 0)
{
[combinedHandle writeData: read];
[read setData:[f1Handle readDataOfLength: CHUNK_SIZE]];
}
//now do the same for f2Handle
//might need to ignore a few bytes in the front of file2 that isnt audio
[f2Handle readDataOfLength: HEADER_LENGTH];
[read setData:[f2Handle readDataOfLength: CHUNK_SIZE]];
while([read length] != 0)
{
[combinedHandle writeData: read];
[read setData:[f2Handle readDataOfLength: CHUNK_SIZE]];
}
[f1Handle closeFile];
[f2Handle closeFile];
[combinedHandle closeFile];
}