MAD Decoder: Support changing format
Support the weird scenario of format changes mid-stream. Probably highly unlikely, and likely to break things if it does occur, but whatever, it might actually happen in some weird file. Signed-off-by: Christopher Snowhill <kode54@gmail.com>CQTexperiment
parent
53209485c0
commit
ca3dec4497
|
@ -584,9 +584,46 @@
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)syncFormat:(BOOL)updateNow {
|
||||||
|
float _sampleRate = _frame.header.samplerate;
|
||||||
|
int _channels = MAD_NCHANNELS(&_frame.header);
|
||||||
|
int _layer = 3;
|
||||||
|
|
||||||
|
switch(_frame.header.layer) {
|
||||||
|
case MAD_LAYER_I:
|
||||||
|
_layer = 1;
|
||||||
|
break;
|
||||||
|
case MAD_LAYER_II:
|
||||||
|
_layer = 2;
|
||||||
|
break;
|
||||||
|
case MAD_LAYER_III:
|
||||||
|
_layer = 3;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL changed = (_sampleRate != sampleRate ||
|
||||||
|
_channels != channels ||
|
||||||
|
_layer != layer);
|
||||||
|
|
||||||
|
if(changed && updateNow) {
|
||||||
|
sampleRate = _sampleRate;
|
||||||
|
channels = _channels;
|
||||||
|
layer = _layer;
|
||||||
|
|
||||||
|
[self willChangeValueForKey:@"properties"];
|
||||||
|
[self didChangeValueForKey:@"properties"];
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
- (int)readAudio:(void *)buffer frames:(UInt32)frames {
|
- (int)readAudio:(void *)buffer frames:(UInt32)frames {
|
||||||
int framesRead = 0;
|
int framesRead = 0;
|
||||||
|
|
||||||
|
[self syncFormat:YES];
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
long framesRemaining = frames - framesRead;
|
long framesRemaining = frames - framesRead;
|
||||||
long framesToCopy = (_outputFrames > framesRemaining ? framesRemaining : _outputFrames);
|
long framesToCopy = (_outputFrames > framesRemaining ? framesRemaining : _outputFrames);
|
||||||
|
@ -614,6 +651,13 @@
|
||||||
|
|
||||||
[self writeOutput];
|
[self writeOutput];
|
||||||
// DLog(@"Wrote output");
|
// DLog(@"Wrote output");
|
||||||
|
|
||||||
|
if([self syncFormat:NO]) {
|
||||||
|
if(framesRead)
|
||||||
|
break;
|
||||||
|
else
|
||||||
|
[self syncFormat:YES];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[self updateMetadata];
|
[self updateMetadata];
|
||||||
|
|
Loading…
Reference in New Issue