cog/Plugins/Vorbis/VorbisDecoder.m

153 lines
2.9 KiB
Matlab
Raw Normal View History

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
//
#import "VorbisDecoder.h"
2005-06-02 18:16:43 +00:00
@implementation VorbisDecoder
2005-06-02 18:16:43 +00:00
size_t sourceRead(void *buf, size_t size, size_t nmemb, void *datasource)
2005-06-02 18:16:43 +00: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 = {
read_func: sourceRead,
seek_func: sourceSeek,
close_func: sourceClose,
tell_func: sourceTell
};
2005-06-02 18:16:43 +00:00
if (ov_open_callbacks(source, &vorbisRef, NULL, 0, callbacks) != 0)
{
NSLog(@"FAILED TO OPEN VORBIS FILE");
2005-06-02 18:16:43 +00:00
return NO;
}
2005-06-02 18:16:43 +00:00
vorbis_info *vi;
vi = ov_info(&vorbisRef, -1);
2005-06-02 18:16:43 +00:00
bitsPerSample = vi->channels * 8;
bitrate = (vi->bitrate_nominal/1000.0);
channels = vi->channels;
2005-06-02 18:16:43 +00:00
frequency = vi->rate;
seekable = ov_seekable(&vorbisRef);
totalFrames = ov_pcm_total(&vorbisRef, -1);
2005-06-02 18:16:43 +00:00
[self willChangeValueForKey:@"properties"];
[self didChangeValueForKey:@"properties"];
2005-06-02 18:16:43 +00:00
return YES;
}
- (int)readAudio:(void *)buf frames:(UInt32)frames
2005-06-02 18:16:43 +00:00
{
int numread;
int total = 0;
2005-06-02 18:16:43 +00:00
if (currentSection != lastSection) {
vorbis_info *vi;
vi = ov_info(&vorbisRef, -1);
bitsPerSample = vi->channels * 8;
bitrate = (vi->bitrate_nominal/1000.0);
channels = vi->channels;
frequency = vi->rate;
[self willChangeValueForKey:@"properties"];
[self didChangeValueForKey:@"properties"];
}
int size = frames * channels * (bitsPerSample/8);
do {
lastSection = currentSection;
numread = ov_read(&vorbisRef, &((char *)buf)[total], size - total, 0, bitsPerSample/8, 1, &currentSection);
if (numread > 0) {
total += numread;
}
if (currentSection != lastSection) {
break;
}
} while (total != size && numread != 0);
return total/(channels * (bitsPerSample/8));
2005-06-02 18:16:43 +00:00
}
- (void)close
{
ov_clear(&vorbisRef);
2007-10-13 07:09:46 +00:00
[source close];
[source release];
2005-06-02 18:16:43 +00:00
}
- (long)seek:(long)frame
2005-06-02 18:16:43 +00:00
{
ov_pcm_seek(&vorbisRef, frame);
return frame;
2005-06-02 18:16:43 +00:00
}
- (NSDictionary *)properties
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:channels], @"channels",
[NSNumber numberWithInt:bitsPerSample], @"bitsPerSample",
[NSNumber numberWithFloat:frequency], @"sampleRate",
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
[NSNumber numberWithInt:bitrate], @"bitrate",
[NSNumber numberWithBool:([source seekable] && seekable)], @"seekable",
nil];
}
+ (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];
}
2005-06-02 18:16:43 +00:00
@end