-
Notifications
You must be signed in to change notification settings - Fork 0
/
IIWavRenderer.m
188 lines (151 loc) · 4.76 KB
/
IIWavRenderer.m
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// CueRendering.m
// iTunesImport
//
// Created by Alexander Strange on 5/17/08.
//
#import "IIWavRenderer.h"
#import "IITemporaryFile.h"
#import <QTKit/QTKit.h>
#import "QuickTimeUtils.h"
static AudioStreamBasicDescription CDASBD = {
44100,
kAudioFormatLinearPCM,
kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked,
sizeof(SInt16) * 2,
1,
sizeof(SInt16) * 2,
2,
sizeof(SInt16) * 8
};
static AudioStreamBasicDescription SetCDDAASBD(MovieAudioExtractionRef aeref)
{
AudioChannelLayout layout = (AudioChannelLayout){kAudioChannelLayoutTag_Stereo};
MovieAudioExtractionSetProperty(aeref, kQTPropertyClass_MovieAudioExtraction_Audio, kQTMovieAudioExtractionAudioPropertyID_AudioChannelLayout, sizeof(AudioChannelLayout), &layout);
MovieAudioExtractionSetProperty(aeref, kQTPropertyClass_MovieAudioExtraction_Audio, kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription, sizeof(AudioStreamBasicDescription), &CDASBD);
return CDASBD;
}
static void WriteWavTo(NSString *path, short *buf, UInt32 len)
{
UInt32 byteLen = len*2*sizeof(SInt16);
struct wav_header {
FourCharCode riff;
UInt32 totalSize;
FourCharCode wave;
FourCharCode fmt_;
FourCharCode fmtLen;
short twocc;
short channels;
UInt32 srate;
UInt32 bps;
short bytesPerFrame;
short bits;
FourCharCode data;
UInt32 dataLen;
} __attribute__((packed)) wav = {
EndianU32_NtoB('RIFF'),
EndianU32_NtoL(byteLen + 8 + 8 + 0x10),
EndianU32_NtoB('WAVE'),
EndianU32_NtoB('fmt '),
EndianU32_NtoL(0x10),
EndianU16_NtoL(0x01),
EndianU16_NtoL(2),
EndianU32_NtoL(44100),
EndianU32_NtoL(44100*2*2),
EndianU16_NtoL(4),
EndianU16_NtoL(16),
EndianU32_NtoB('data'),
EndianU32_NtoL(byteLen)
};
FILE *f = fopen([path UTF8String], "wb");
fwrite(&wav, sizeof(wav), 1, f);
fwrite(buf, 1, byteLen, f);
fclose(f);
}
@implementation IIWavRenderer
- (id)initWithAudioFile:(NSString*)path {return nil;}
- (unsigned)samples {return 0;}
- (NSString*)pathForWavWithName:(NSString*)name {return nil;}
- (NSString*)pathForWavWithName:(NSString*)name fromSample:(unsigned)startSample length:(unsigned)length {return nil;}
@end
@interface IIQTWavRenderer : IIWavRenderer
{
MovieAudioExtractionRef aeref;
Movie movie;
QTMovie *qtMovie;
UInt32 length;
AudioStreamBasicDescription asbd;
}
@end
@implementation IIQTWavRenderer
- (id)initWithAudioFile:(NSString*)path
{
if (self = [super init]) {
//EnterMoviesOnThread(0);
NSError *err;
qtMovie = [[QTMovie movieWithFile:path error:&err] retain];
movie = [qtMovie quickTimeMovie];
if (!movie) {
NSLog(@"error %@ opening movie %@", err, path);
[self release];
return nil;
}
SetMovieActive(movie, YES);
MovieAudioExtractionBegin(movie, 0, &aeref);
asbd = SetCDDAASBD(aeref);
SetMovieTimeScale(movie, asbd.mSampleRate);
length = ceil(GetMovieExtractionDuration(movie) * asbd.mSampleRate);
//NSLog(@"length = %f s %u samples", GetMovieExtractionDuration(movie), length);
}
return self;
}
-(void)dealloc
{
MovieAudioExtractionEnd(aeref);
[qtMovie release];
[super dealloc];
}
- (unsigned)samples
{
return length;
}
- (UInt32) decompressFrom:(UInt32)from into:(short*)buf length:(UInt32)len
{
OSErr err;
TimeRecord t;
t.scale = asbd.mSampleRate;
t.base = GetMovieTimeBase(movie);
t.value.hi = 0;
t.value.lo = from;
err = MovieAudioExtractionSetProperty(aeref,
kQTPropertyClass_MovieAudioExtraction_Movie,
kQTMovieAudioExtractionMoviePropertyID_CurrentTime,
sizeof(t), &t);
if (err) NSLog(@"upon SetProperty, err %d, err2 %d", err, GetMoviesError());
AudioBufferList bufList = (AudioBufferList){1,{2,len*sizeof(SInt16)*2,buf}};
UInt32 realLen = len, flags;
err = MovieAudioExtractionFillBuffer(aeref, &realLen, &bufList, &flags);
if (err) NSLog(@"upon FillBuffer, err %d, err2 %d", err, GetMoviesError());
return realLen;
}
- (NSString*)pathForWavWithName:(NSString*)name fromSample:(unsigned)startSample length:(unsigned)wavLen
{
SInt16 *buf = malloc(wavLen * sizeof(SInt16) * 2);
UInt32 decompLen = [self decompressFrom:startSample into:buf length:wavLen];
if (decompLen != wavLen)
NSLog(@"decompression length mismatch: wanted %u got %u", wavLen, (unsigned)decompLen);
// XXX not GC safe
NSString *tempPath = [[IITemporaryFile temporaryFileWithName:name] path];
WriteWavTo(tempPath, buf, MIN(decompLen, wavLen));
free(buf);
return tempPath;
}
- (NSString*)pathForWavWithName:(NSString*)name
{
return [self pathForWavWithName:name fromSample:0 length:length];
}
@end
IIWavRenderer *GetRendererForAudioFile(NSString *path)
{
return [[[IIQTWavRenderer alloc] initWithAudioFile:path] autorelease];
}