2005-06-02 14:16:43 -04:00
|
|
|
//
|
|
|
|
// VorbisFile.m
|
|
|
|
// zyVorbis
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 1/22/05.
|
2005-07-02 17:02:06 -04:00
|
|
|
// Copyright 2005 Vincent Spader All rights reserved.
|
2005-06-02 14:16:43 -04:00
|
|
|
//
|
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
#import "VorbisDecoder.h"
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
#import "Logging.h"
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
@implementation VorbisDecoder
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2007-03-01 22:36:52 -03:00
|
|
|
size_t sourceRead(void *buf, size_t size, size_t nmemb, void *datasource)
|
2005-06-02 14:16:43 -04:00
|
|
|
{
|
2007-03-01 22:36:52 -03:00
|
|
|
id source = (id)datasource;
|
|
|
|
|
|
|
|
return [source read:buf amount:(size*nmemb)];
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceSeek(void *datasource, ogg_int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
id source = (id)datasource;
|
|
|
|
return ([source seek:offset whence:whence] ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceClose(void *datasource)
|
|
|
|
{
|
|
|
|
id source = (id)datasource;
|
|
|
|
[source close];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long sourceTell(void *datasource)
|
|
|
|
{
|
|
|
|
id source = (id)datasource;
|
|
|
|
|
|
|
|
return [source tell];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
|
|
|
source = [s retain];
|
|
|
|
|
|
|
|
ov_callbacks callbacks = {
|
2013-10-03 05:00:58 -03:00
|
|
|
.read_func = sourceRead,
|
|
|
|
.seek_func = sourceSeek,
|
|
|
|
.close_func = sourceClose,
|
|
|
|
.tell_func = sourceTell
|
2007-03-01 22:36:52 -03:00
|
|
|
};
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2007-03-01 22:36:52 -03:00
|
|
|
if (ov_open_callbacks(source, &vorbisRef, NULL, 0, callbacks) != 0)
|
|
|
|
{
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"FAILED TO OPEN VORBIS FILE");
|
2005-06-02 14:16:43 -04:00
|
|
|
return NO;
|
2007-03-01 22:36:52 -03:00
|
|
|
}
|
2005-06-02 14:16:43 -04:00
|
|
|
|
|
|
|
vorbis_info *vi;
|
|
|
|
|
|
|
|
vi = ov_info(&vorbisRef, -1);
|
2007-02-28 23:56:42 -03:00
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
bitrate = (vi->bitrate_nominal/1000.0);
|
|
|
|
channels = vi->channels;
|
2005-06-02 14:16:43 -04:00
|
|
|
frequency = vi->rate;
|
2007-02-28 23:56:42 -03:00
|
|
|
|
2007-03-01 22:36:52 -03:00
|
|
|
seekable = ov_seekable(&vorbisRef);
|
|
|
|
|
2007-11-24 17:16:27 -03:00
|
|
|
totalFrames = ov_pcm_total(&vorbisRef, -1);
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2007-03-03 14:19:37 -03:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2005-06-02 14:16:43 -04:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2007-11-24 17:16:27 -03:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
2005-06-02 14:16:43 -04:00
|
|
|
{
|
|
|
|
int numread;
|
2007-03-03 20:05:15 -03:00
|
|
|
int total = 0;
|
2005-06-02 14:16:43 -04:00
|
|
|
|
2007-03-03 19:55:26 -03:00
|
|
|
if (currentSection != lastSection) {
|
|
|
|
vorbis_info *vi;
|
|
|
|
vi = ov_info(&vorbisRef, -1);
|
|
|
|
|
|
|
|
bitrate = (vi->bitrate_nominal/1000.0);
|
|
|
|
channels = vi->channels;
|
|
|
|
frequency = vi->rate;
|
2007-03-02 21:33:36 -03:00
|
|
|
|
2007-03-03 19:55:26 -03:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
}
|
2007-03-03 20:05:15 -03:00
|
|
|
|
|
|
|
do {
|
|
|
|
lastSection = currentSection;
|
2013-10-05 18:15:09 -03:00
|
|
|
float ** pcm;
|
|
|
|
numread = ov_read_float(&vorbisRef, &pcm, frames - total, ¤tSection);
|
2007-03-03 20:05:15 -03:00
|
|
|
if (numread > 0) {
|
2013-10-05 18:15:09 -03:00
|
|
|
for (int i = 0; i < channels; i++) {
|
|
|
|
for (int j = 0; j < numread; j++) {
|
|
|
|
((float *)buf)[(total + j) * channels + i] = pcm[i][j];
|
|
|
|
}
|
|
|
|
}
|
2007-03-03 20:05:15 -03:00
|
|
|
total += numread;
|
2007-03-03 19:55:26 -03:00
|
|
|
}
|
|
|
|
|
2007-03-03 20:05:15 -03:00
|
|
|
if (currentSection != lastSection) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-05 18:15:09 -03:00
|
|
|
} while (total != frames && numread != 0);
|
2007-03-03 20:05:15 -03:00
|
|
|
|
2013-10-05 18:15:09 -03:00
|
|
|
return total;
|
2005-06-02 14:16:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
ov_clear(&vorbisRef);
|
2007-10-13 03:09:46 -04:00
|
|
|
|
|
|
|
[source close];
|
2007-03-01 22:36:52 -03:00
|
|
|
[source release];
|
2005-06-02 14:16:43 -04:00
|
|
|
}
|
|
|
|
|
2007-11-24 17:16:27 -03:00
|
|
|
- (long)seek:(long)frame
|
2005-06-02 14:16:43 -04:00
|
|
|
{
|
2007-11-24 17:16:27 -03:00
|
|
|
ov_pcm_seek(&vorbisRef, frame);
|
2005-06-05 14:52:35 -04:00
|
|
|
|
2007-11-24 17:16:27 -03:00
|
|
|
return frame;
|
2005-06-02 14:16:43 -04:00
|
|
|
}
|
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels], @"channels",
|
2013-10-05 18:15:09 -03:00
|
|
|
[NSNumber numberWithInt:32], @"bitsPerSample",
|
|
|
|
[NSNumber numberWithBool:YES], @"floatingPoint",
|
2007-02-24 17:36:27 -03:00
|
|
|
[NSNumber numberWithFloat:frequency], @"sampleRate",
|
2007-11-24 17:16:27 -03:00
|
|
|
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
|
2007-02-24 17:36:27 -03:00
|
|
|
[NSNumber numberWithInt:bitrate], @"bitrate",
|
2007-05-27 11:11:30 -04:00
|
|
|
[NSNumber numberWithBool:([source seekable] && seekable)], @"seekable",
|
2013-10-05 18:15:09 -03:00
|
|
|
@"host", @"endian",
|
2007-02-24 17:36:27 -03:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
2007-10-14 14:31:20 -03:00
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"ogg",nil];
|
|
|
|
}
|
|
|
|
|
2007-10-14 15:39:58 -03:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"application/ogg", @"application/x-ogg", @"audio/x-vorbis+ogg", nil];
|
|
|
|
}
|
2007-02-24 17:36:27 -03:00
|
|
|
|
2005-06-02 14:16:43 -04:00
|
|
|
@end
|