My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
#import <AudioToolbox/AudioToolbox.h>

static BOOL g_Verbose = 0;

static void usage( void )
{
printf( "usage: pcm2aif -pcmFile <inputFile> -aifFile <outputFile> -sampleRate <sampleRate>\n" );
exit( 0 );
}

static NSData* readPCM( NSString *filename )
{
return [NSData dataWithContentsOfFile:filename];
}

static int writeAIFF( NSString *fullPath, Float64 sampleRate, NSData *sourceData )
{
NSString *parentDir = [fullPath stringByDeletingLastPathComponent];
NSString *fileName = [fullPath lastPathComponent];
const char *filePath = [fullPath fileSystemRepresentation];
const char *fileSystemPath;
FSRef parentDirRef;
FSRef existingFileRef;
FSRef newFileRef;
OSStatus status;
AudioFileID newFileID;
AudioStreamBasicDescription sourceFormat;
AudioStreamBasicDescription destFormat;
AudioConverterRef audioConverter;

if ( [parentDir length] == 0 ) {
// Must be working in the current directory
parentDir = @"./";
}

fileSystemPath = [parentDir fileSystemRepresentation];

// If the file already exists, blow it away first. That way, AudioFileCreate won't fail.
if ( noErr == FSPathMakeRef( (UInt8*)filePath, &existingFileRef, NULL ) ) {
FSDeleteObject( &existingFileRef );
}

if ( FSPathMakeRef( (UInt8*)fileSystemPath, &parentDirRef, NULL ) ) {
[NSException raise:@"AudioConverterFailure" format:@"FSPathMakeRef failed"];
}

memset( &sourceFormat, 0, sizeof(AudioStreamBasicDescription) );
memset( &destFormat, 0, sizeof(AudioStreamBasicDescription) );

sourceFormat.mSampleRate = sampleRate;
sourceFormat.mFormatID = kAudioFormatLinearPCM;
sourceFormat.mFormatFlags = ( kAudioFormatFlagIsFloat );
if ( CFByteOrderGetCurrent() == CFByteOrderBigEndian ) {
sourceFormat.mFormatFlags |= kAudioFormatFlagIsBigEndian;
}
sourceFormat.mBytesPerFrame = sizeof(float);
sourceFormat.mFramesPerPacket = 1;
sourceFormat.mBytesPerPacket = sizeof(float) * sourceFormat.mFramesPerPacket;
sourceFormat.mChannelsPerFrame = 1;
sourceFormat.mBitsPerChannel = 32;

destFormat.mSampleRate = sampleRate;
destFormat.mFormatID = kAudioFormatLinearPCM;
destFormat.mFormatFlags = ( kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagIsBigEndian );
destFormat.mBytesPerPacket = 2;
destFormat.mFramesPerPacket = 1;
destFormat.mBytesPerFrame = 2;
destFormat.mChannelsPerFrame = 1;
destFormat.mBitsPerChannel = 16;

status = AudioFileCreate( &parentDirRef, (CFStringRef)fileName, kAudioFileAIFFType, &destFormat, 0, &newFileRef, &newFileID);
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioFileCreate failed (status=%d)", status];
}

status = AudioConverterNew( &sourceFormat, &destFormat, &audioConverter );
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioConverterNew failed (status=%d)", status];
}

unsigned int length = [sourceData length] / sizeof( float );
NSMutableData *outData = [NSMutableData dataWithLength:(length * destFormat.mBytesPerFrame)];
void *outBytes = [outData mutableBytes];
UInt32 outBytesLength = [outData length];
const void *inBytes = [sourceData bytes];
UInt32 inBytesLength = length * sourceFormat.mBytesPerFrame;

status = AudioConverterConvertBuffer( audioConverter, inBytesLength, (void*)inBytes, &outBytesLength, outBytes );
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioConverterConvertBuffer failed (status=%d)", status];
}

status = AudioFileWriteBytes( newFileID, 0, 0, &outBytesLength, outBytes );
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioFileWriteBytes failed (status=%d)", status];
}

status = AudioConverterDispose( audioConverter );
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioConverterDispose failed (status=%d)", status];
}

status = AudioFileClose( newFileID );
if ( noErr != status ) {
[NSException raise:@"AudioConverterFailure" format:@"AudioFileClose failed (status=%d)", status];
}

return 0;

}

int main( int argc, char *argv[] )
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSUserDefaults *args = [NSUserDefaults standardUserDefaults];

NSString *pcmFilename = [args stringForKey:@"pcmFile"];
NSString *aifFilename = [args stringForKey:@"aifFile"];
Float64 sampleRate = [args floatForKey:@"sampleRate"];
g_Verbose = [args boolForKey:@"verbose"];

if ( !pcmFilename || !aifFilename || ( sampleRate == 0.0 ) ) {
usage();
}

pcmFilename = [pcmFilename stringByExpandingTildeInPath];
aifFilename = [aifFilename stringByExpandingTildeInPath];

NSFileManager *fm = [NSFileManager defaultManager];
if ( ![fm fileExistsAtPath:pcmFilename] ) {
printf( "PCM file does not exist.\n" );
exit(0);
}

// Read the binary data from the pcm file - it's just a collection of floats
NSData *pcmData = readPCM( pcmFilename );
if ( !pcmData ) {
printf( "Could not read PCM data.\n" );
exit(0);
}

if ( [fm fileExistsAtPath:aifFilename] ) {
printf( "Overwriting existing AIF file.\n" );
}

// Write the data out to an aiff file
writeAIFF( aifFilename, sampleRate, pcmData );

[pool release];
return 0;
}

Change log

r5 by liscio on Dec 10, 2007   Diff
Initial import
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 5897 bytes, 154 lines
Powered by Google Project Hosting