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
|
|
|
|
2022-02-09 21:44:50 +00:00
|
|
|
#import "HTTPSource.h"
|
|
|
|
|
2022-02-12 15:16:59 +00:00
|
|
|
#import "picture.h"
|
|
|
|
|
2022-06-17 17:27:17 +00:00
|
|
|
#import "NSDictionary+Merge.h"
|
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
#import <FLAC/metadata.h>
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation VorbisDecoder
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2021-12-28 08:54:28 +00:00
|
|
|
static const int MAXCHANNELS = 8;
|
|
|
|
static const int chmap[MAXCHANNELS][MAXCHANNELS] = {
|
2022-02-07 05:49:27 +00:00
|
|
|
{
|
|
|
|
0,
|
|
|
|
}, // mono
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
}, // l, r
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
}, // l, c, r -> l, r, c
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
}, // l, r, bl, br
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
}, // l, c, r, bl, br -> l, r, c, bl, br
|
|
|
|
{ 0, 2, 1, 5, 3, 4 }, // l, c, r, bl, br, lfe -> l, r, c, lfe, bl, br
|
|
|
|
{ 0, 2, 1, 6, 5, 3, 4 }, // l, c, r, sl, sr, bc, lfe -> l, r, c, lfe, bc, sl, sr
|
|
|
|
{ 0, 2, 1, 7, 5, 6, 3, 4 } // l, c, r, sl, sr, bl, br, lfe -> l, r, c, lfe, bl, br, sl, sr
|
2021-12-28 08:54:28 +00:00
|
|
|
};
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t sourceRead(void *buf, size_t size, size_t nmemb, void *datasource) {
|
2016-05-05 20:05:39 +00:00
|
|
|
id source = (__bridge id)datasource;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return [source read:buf amount:(size * nmemb)];
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
int sourceClose(void *datasource) {
|
2007-03-02 01:36:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)s {
|
2016-05-05 20:05:39 +00:00
|
|
|
source = s;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
ov_callbacks callbacks = {
|
2022-02-07 05:49:27 +00:00
|
|
|
.read_func = sourceRead,
|
|
|
|
.seek_func = sourceSeek,
|
|
|
|
.close_func = sourceClose,
|
|
|
|
.tell_func = sourceTell
|
2007-03-02 01:36:52 +00:00
|
|
|
};
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(ov_open_callbacks((__bridge void *)(source), &vorbisRef, NULL, 0, callbacks) != 0) {
|
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
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
vorbis_info *vi;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
vi = ov_info(&vorbisRef, -1);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
bitrate = (vi->bitrate_nominal / 1000.0);
|
2007-02-24 20:36:27 +00:00
|
|
|
channels = vi->channels;
|
2005-06-02 18:16:43 +00:00
|
|
|
frequency = vi->rate;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
seekable = ov_seekable(&vorbisRef);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
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"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
metaDict = [NSDictionary dictionary];
|
|
|
|
icyMetaDict = [NSDictionary dictionary];
|
2022-02-12 15:16:59 +00:00
|
|
|
albumArt = [NSData data];
|
2022-07-08 13:26:28 +00:00
|
|
|
|
2022-02-09 21:44:50 +00:00
|
|
|
[self updateMetadata];
|
|
|
|
|
2022-07-21 10:50:39 +00:00
|
|
|
metadataUpdateInterval = frequency;
|
|
|
|
metadataUpdateCount = 0;
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
static void setDictionary(NSMutableDictionary *dict, NSString *tag, NSString *value) {
|
|
|
|
NSMutableArray *array = [dict valueForKey:tag];
|
|
|
|
if(!array) {
|
|
|
|
array = [[NSMutableArray alloc] init];
|
|
|
|
[dict setObject:array forKey:tag];
|
2022-02-12 15:16:59 +00:00
|
|
|
}
|
2022-07-08 13:26:28 +00:00
|
|
|
[array addObject:value];
|
2022-02-12 15:16:59 +00:00
|
|
|
}
|
2022-02-09 23:04:49 +00:00
|
|
|
|
2022-02-12 15:16:59 +00:00
|
|
|
- (void)updateMetadata {
|
|
|
|
vorbis_comment *tags = ov_comment(&vorbisRef, -1);
|
2022-07-08 13:26:28 +00:00
|
|
|
NSMutableDictionary *_metaDict = [[NSMutableDictionary alloc] init];
|
|
|
|
NSData *_albumArt = albumArt;
|
2022-02-12 15:16:59 +00:00
|
|
|
|
|
|
|
if(tags) {
|
2022-07-08 13:26:28 +00:00
|
|
|
for(int i = 0; i < tags->comments; ++i) {
|
|
|
|
FLAC__StreamMetadata_VorbisComment_Entry entry = { .entry = (FLAC__byte *)tags->user_comments[i], .length = tags->comment_lengths[i] };
|
|
|
|
char *name, *value;
|
|
|
|
if(FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(entry, &name, &value)) {
|
|
|
|
NSString *tagName = guess_encoding_of_string(name);
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
tagName = [tagName lowercaseString];
|
|
|
|
|
|
|
|
if([tagName isEqualToString:@"metadata_block_picture"]) {
|
2022-07-13 07:11:51 +00:00
|
|
|
flac_picture_t *picture = flac_picture_parse_from_base64(value);
|
2022-07-08 13:26:28 +00:00
|
|
|
if(picture) {
|
|
|
|
if(picture->binary && picture->binary_length) {
|
|
|
|
_albumArt = [NSData dataWithBytes:picture->binary length:picture->binary_length];
|
|
|
|
}
|
|
|
|
flac_picture_free(picture);
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 07:11:51 +00:00
|
|
|
setDictionary(_metaDict, tagName, guess_encoding_of_string(value));
|
2022-02-09 21:44:50 +00:00
|
|
|
}
|
2022-07-13 07:11:51 +00:00
|
|
|
|
|
|
|
free(value);
|
2022-02-09 21:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
if(![_albumArt isEqualToData:albumArt] ||
|
|
|
|
![_metaDict isEqualToDictionary:metaDict]) {
|
|
|
|
metaDict = _metaDict;
|
2022-02-12 15:16:59 +00:00
|
|
|
albumArt = _albumArt;
|
|
|
|
|
2022-07-13 07:12:35 +00:00
|
|
|
if(![source seekable]) {
|
|
|
|
[self willChangeValueForKey:@"metadata"];
|
|
|
|
[self didChangeValueForKey:@"metadata"];
|
|
|
|
}
|
2022-02-09 21:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateIcyMetadata {
|
2022-02-09 23:04:49 +00:00
|
|
|
if([source seekable]) return;
|
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
NSMutableDictionary *_icyMetaDict = [[NSMutableDictionary alloc] init];
|
2022-02-09 21:44:50 +00:00
|
|
|
|
|
|
|
Class sourceClass = [source class];
|
|
|
|
if([sourceClass isEqual:NSClassFromString(@"HTTPSource")]) {
|
|
|
|
HTTPSource *httpSource = (HTTPSource *)source;
|
|
|
|
if([httpSource hasMetadata]) {
|
|
|
|
NSDictionary *metadata = [httpSource metadata];
|
2022-07-08 13:26:28 +00:00
|
|
|
NSString *_genre = [metadata valueForKey:@"genre"];
|
|
|
|
NSString *_album = [metadata valueForKey:@"album"];
|
|
|
|
NSString *_artist = [metadata valueForKey:@"artist"];
|
|
|
|
NSString *_title = [metadata valueForKey:@"title"];
|
|
|
|
|
|
|
|
if(_genre && [_genre length]) {
|
|
|
|
setDictionary(_icyMetaDict, @"genre", _genre);
|
|
|
|
}
|
|
|
|
if(_album && [_album length]) {
|
|
|
|
setDictionary(_icyMetaDict, @"album", _album);
|
|
|
|
}
|
|
|
|
if(_artist && [_artist length]) {
|
|
|
|
setDictionary(_icyMetaDict, @"artist", _artist);
|
|
|
|
}
|
|
|
|
if(_title && [_title length]) {
|
|
|
|
setDictionary(_icyMetaDict, @"title", _title);
|
|
|
|
}
|
2022-02-09 21:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 13:26:28 +00:00
|
|
|
if(![_icyMetaDict isEqualToDictionary:icyMetaDict]) {
|
|
|
|
icyMetaDict = _icyMetaDict;
|
2022-02-09 21:44:50 +00:00
|
|
|
[self willChangeValueForKey:@"metadata"];
|
|
|
|
[self didChangeValueForKey:@"metadata"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
- (AudioChunk *)readAudio {
|
2005-06-02 18:16:43 +00:00
|
|
|
int numread;
|
2007-03-03 23:05:15 +00:00
|
|
|
int total = 0;
|
2022-07-10 22:14:47 +00:00
|
|
|
int frames = 1024;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(currentSection != lastSection) {
|
2007-03-03 22:55:26 +00:00
|
|
|
vorbis_info *vi;
|
|
|
|
vi = ov_info(&vorbisRef, -1);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
bitrate = (vi->bitrate_nominal / 1000.0);
|
2007-03-03 22:55:26 +00:00
|
|
|
channels = vi->channels;
|
|
|
|
frequency = vi->rate;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-21 10:50:39 +00:00
|
|
|
metadataUpdateInterval = frequency;
|
|
|
|
metadataUpdateCount = 0;
|
|
|
|
|
2007-03-03 22:55:26 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
2022-02-09 21:44:50 +00:00
|
|
|
|
|
|
|
[self updateMetadata];
|
2007-03-03 22:55:26 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
id audioChunkClass = NSClassFromString(@"AudioChunk");
|
|
|
|
AudioChunk *chunk = [[audioChunkClass alloc] initWithProperties:[self properties]];
|
|
|
|
|
|
|
|
float buffer[frames * channels];
|
|
|
|
void *buf = (void *)buffer;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
do {
|
2007-03-03 23:05:15 +00:00
|
|
|
lastSection = currentSection;
|
2022-02-07 05:49:27 +00:00
|
|
|
float **pcm;
|
|
|
|
numread = (int)ov_read_float(&vorbisRef, &pcm, frames - total, ¤tSection);
|
|
|
|
if(numread > 0) {
|
|
|
|
if(channels <= MAXCHANNELS) {
|
|
|
|
for(int i = 0; i < channels; i++) {
|
|
|
|
for(int j = 0; j < numread; j++) {
|
|
|
|
((float *)buf)[(total + j) * channels + i] = pcm[chmap[channels - 1][i]][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(currentSection != lastSection) {
|
2007-03-03 23:05:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
} while(total != frames && numread != 0);
|
|
|
|
|
2022-07-21 10:50:39 +00:00
|
|
|
metadataUpdateCount += total;
|
|
|
|
if(metadataUpdateCount >= metadataUpdateInterval) {
|
|
|
|
metadataUpdateCount -= metadataUpdateInterval;
|
|
|
|
[self updateIcyMetadata];
|
|
|
|
}
|
2022-02-09 21:44:50 +00:00
|
|
|
|
2022-07-10 22:14:47 +00:00
|
|
|
[chunk assignSamples:buffer frameCount:total];
|
|
|
|
|
|
|
|
return chunk;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)close {
|
2005-06-02 18:16:43 +00:00
|
|
|
ov_clear(&vorbisRef);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[self close];
|
2016-06-19 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (long)seek:(long)frame {
|
2007-11-24 20:16:27 +00:00
|
|
|
ov_pcm_seek(&vorbisRef, frame);
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
return frame;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSDictionary *)properties {
|
2022-06-17 13:39:02 +00:00
|
|
|
return @{ @"channels": @(channels),
|
|
|
|
@"bitsPerSample": @(32),
|
|
|
|
@"floatingPoint": @(YES),
|
|
|
|
@"sampleRate": @(frequency),
|
|
|
|
@"totalFrames": @(totalFrames),
|
|
|
|
@"bitrate": @(bitrate),
|
|
|
|
@"seekable": @([source seekable] && seekable),
|
|
|
|
@"codec": @"Ogg Vorbis",
|
|
|
|
@"endian": @"host",
|
|
|
|
@"encoding": @"lossy" };
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 03:56:39 +00:00
|
|
|
- (NSDictionary *)metadata {
|
2022-07-08 13:26:28 +00:00
|
|
|
NSDictionary *dict1 = @{ @"albumArt": albumArt };
|
|
|
|
NSDictionary *dict2 = [dict1 dictionaryByMergingWith:metaDict];
|
|
|
|
NSDictionary *dict3 = [dict2 dictionaryByMergingWith:icyMetaDict];
|
|
|
|
return dict3;
|
2022-02-09 03:56:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)fileTypes {
|
2022-01-19 02:12:57 +00:00
|
|
|
return @[@"ogg"];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)mimeTypes {
|
2022-02-09 21:44:50 +00:00
|
|
|
return @[@"application/ogg", @"application/x-ogg", @"audio/ogg", @"audio/x-vorbis+ogg"];
|
2007-10-14 18:39:58 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (float)priority {
|
|
|
|
return 1.0;
|
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
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)fileTypeAssociations {
|
|
|
|
return @[
|
|
|
|
@[@"Ogg Vorbis File", @"ogg.icns", @"ogg"]
|
|
|
|
];
|
2022-01-18 11:06:03 +00:00
|
|
|
}
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
@end
|