2005-06-02 18:16:43 +00:00
|
|
|
//
|
|
|
|
// VorbisFile.m
|
|
|
|
// zyVorbis
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 1/22/05.
|
2005-07-02 21:02:06 +00:00
|
|
|
// Copyright 2005 Vincent Spader All rights reserved.
|
2005-06-02 18:16:43 +00:00
|
|
|
//
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "VorbisDecoder.h"
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation VorbisDecoder
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
size_t sourceRead(void *buf, size_t size, size_t nmemb, void *datasource)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
id source = (__bridge id)datasource;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
|
|
|
return [source read:buf amount:(size*nmemb)];
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceSeek(void *datasource, ogg_int64_t offset, int whence)
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
id source = (__bridge id)datasource;
|
2007-03-02 01:36:52 +00:00
|
|
|
return ([source seek:offset whence:whence] ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sourceClose(void *datasource)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long sourceTell(void *datasource)
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
id source = (__bridge id)datasource;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
|
|
|
return [source tell];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
source = s;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
|
|
|
ov_callbacks callbacks = {
|
2013-10-03 08:00:58 +00:00
|
|
|
.read_func = sourceRead,
|
|
|
|
.seek_func = sourceSeek,
|
|
|
|
.close_func = sourceClose,
|
|
|
|
.tell_func = sourceTell
|
2007-03-02 01:36:52 +00:00
|
|
|
};
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
if (ov_open_callbacks((__bridge void *)(source), &vorbisRef, NULL, 0, callbacks) != 0)
|
2007-03-02 01:36:52 +00:00
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FAILED TO OPEN VORBIS FILE");
|
2005-06-02 18:16:43 +00:00
|
|
|
return NO;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
vorbis_info *vi;
|
|
|
|
|
|
|
|
vi = ov_info(&vorbisRef, -1);
|
2007-03-01 02:56:42 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
bitrate = (vi->bitrate_nominal/1000.0);
|
|
|
|
channels = vi->channels;
|
2005-06-02 18:16:43 +00:00
|
|
|
frequency = vi->rate;
|
2007-03-01 02:56:42 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
seekable = ov_seekable(&vorbisRef);
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
totalFrames = ov_pcm_total(&vorbisRef, -1);
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-03-03 17:19:37 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
int numread;
|
2007-03-03 23:05:15 +00:00
|
|
|
int total = 0;
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-03-03 22:55:26 +00: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-03 00:33:36 +00:00
|
|
|
|
2007-03-03 22:55:26 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
}
|
2007-03-03 23:05:15 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
lastSection = currentSection;
|
2013-10-05 21:15:09 +00:00
|
|
|
float ** pcm;
|
|
|
|
numread = ov_read_float(&vorbisRef, &pcm, frames - total, ¤tSection);
|
2007-03-03 23:05:15 +00:00
|
|
|
if (numread > 0) {
|
2013-10-05 21:15:09 +00: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 23:05:15 +00:00
|
|
|
total += numread;
|
2007-03-03 22:55:26 +00:00
|
|
|
}
|
|
|
|
|
2007-03-03 23:05:15 +00:00
|
|
|
if (currentSection != lastSection) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
} while (total != frames && numread != 0);
|
2007-03-03 23:05:15 +00:00
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
return total;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
ov_clear(&vorbisRef);
|
|
|
|
}
|
|
|
|
|
2016-06-19 19:57:18 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (long)seek:(long)frame
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
ov_pcm_seek(&vorbisRef, frame);
|
2005-06-05 18:52:35 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
return frame;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels], @"channels",
|
2013-10-05 21:15:09 +00:00
|
|
|
[NSNumber numberWithInt:32], @"bitsPerSample",
|
|
|
|
[NSNumber numberWithBool:YES], @"floatingPoint",
|
2007-02-24 20:36:27 +00:00
|
|
|
[NSNumber numberWithFloat:frequency], @"sampleRate",
|
2007-11-24 20:16:27 +00:00
|
|
|
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
|
2007-02-24 20:36:27 +00:00
|
|
|
[NSNumber numberWithInt:bitrate], @"bitrate",
|
2007-05-27 15:11:30 +00:00
|
|
|
[NSNumber numberWithBool:([source seekable] && seekable)], @"seekable",
|
2013-10-05 21:15:09 +00:00
|
|
|
@"host", @"endian",
|
2007-02-24 20:36:27 +00:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
2007-10-14 17:31:20 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"ogg",nil];
|
|
|
|
}
|
|
|
|
|
2007-10-14 18:39:58 +00:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"application/ogg", @"application/x-ogg", @"audio/x-vorbis+ogg", nil];
|
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 17:54:11 +00:00
|
|
|
+ (float)priority
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
@end
|