2005-06-02 18:16:43 +00:00
|
|
|
//
|
|
|
|
// SoundFile.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 1/15/05.
|
|
|
|
// Copyright 2005 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "SoundFile.h"
|
|
|
|
|
2005-06-29 15:28:20 +00:00
|
|
|
#import "FlacFile.h"
|
|
|
|
#import "AACFile.h"
|
|
|
|
#import "MonkeysFile.h"
|
|
|
|
#import "MPEGFile.h"
|
|
|
|
#import "MusepackFile.h"
|
|
|
|
#import "VorbisFile.h"
|
|
|
|
#import "WaveFile.h"
|
|
|
|
#import "WavPackFile.h"
|
|
|
|
#import "ShnFile.h"
|
2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
@implementation SoundFile
|
|
|
|
|
2005-06-05 18:52:35 +00:00
|
|
|
/*- (void)seek:(unsigned long)position
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
unsigned long time;
|
|
|
|
unsigned long frame;
|
|
|
|
|
|
|
|
frame = position/channels/(bitsPerSample/8);
|
|
|
|
time = (unsigned long)(((double)frame/(frequency/1000.0)));
|
|
|
|
|
|
|
|
currentPosition = position;
|
|
|
|
|
2005-06-05 18:52:35 +00:00
|
|
|
time = [self seekToTime:time];
|
|
|
|
position = time * (frequency/1000.0)*chanels*(bitsPerSample/8)
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2005-06-05 18:52:35 +00:00
|
|
|
*/
|
2005-06-02 18:16:43 +00:00
|
|
|
- (double)length
|
|
|
|
{
|
|
|
|
return (totalSize/channels/(bitsPerSample/8)/(frequency/1000.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)bitRate
|
|
|
|
{
|
|
|
|
return bitRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
//this should be done by the soundfile....not seek...
|
2005-06-06 17:47:29 +00:00
|
|
|
- (double)seekToTime:(double)milliseconds
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2005-06-29 15:28:20 +00:00
|
|
|
return -1.0;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-29 15:28:20 +00:00
|
|
|
/*
|
2005-06-02 18:16:43 +00:00
|
|
|
@class FlacFile;
|
|
|
|
@class MonkeysFile;
|
|
|
|
@class MPEGFile;
|
|
|
|
@class MusepackFile;
|
|
|
|
@class VorbisFile;
|
|
|
|
@class WaveFile;
|
|
|
|
@class AACFile;
|
2005-06-06 17:47:29 +00:00
|
|
|
@class WavPackFile;
|
|
|
|
@class ShnFile;
|
2005-06-29 15:28:20 +00:00
|
|
|
*/
|
2005-06-02 18:16:43 +00:00
|
|
|
+ (SoundFile *)soundFileFromFilename:(NSString *)filename
|
|
|
|
{
|
|
|
|
SoundFile *soundFile;
|
|
|
|
|
2005-06-29 15:28:20 +00:00
|
|
|
DBLog(@"FILENAME: %@", [filename pathExtension]);
|
|
|
|
|
|
|
|
if (([[filename pathExtension] caseInsensitiveCompare:@"wav"] == NSOrderedSame) || ([[filename pathExtension] caseInsensitiveCompare:@"aiff"] == NSOrderedSame) || ([[filename pathExtension] caseInsensitiveCompare:@"aif"] == NSOrderedSame))
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[WaveFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"ogg"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[VorbisFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"mpc"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[MusepackFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"flac"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[FlacFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"ape"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[MonkeysFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"mp3"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[MPEGFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"aac"] == NSOrderedSame)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
soundFile = [[AACFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"wv"] == NSOrderedSame)
|
2005-06-06 17:47:29 +00:00
|
|
|
{
|
|
|
|
soundFile = [[WavPackFile alloc] init];
|
|
|
|
}
|
2005-06-29 15:28:20 +00:00
|
|
|
else if ([[filename pathExtension] caseInsensitiveCompare:@"shn"] == NSOrderedSame)
|
2005-06-06 17:47:29 +00:00
|
|
|
{
|
|
|
|
soundFile = [[ShnFile alloc] init];
|
|
|
|
}
|
2005-06-02 18:16:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
soundFile = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return soundFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (SoundFile *)open:(NSString *)filename
|
|
|
|
{
|
|
|
|
SoundFile *soundFile;
|
|
|
|
BOOL b;
|
|
|
|
|
|
|
|
soundFile = [SoundFile soundFileFromFilename:filename];
|
|
|
|
|
|
|
|
b = [soundFile open:[filename UTF8String]];
|
|
|
|
if (b == YES)
|
|
|
|
return soundFile;
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (SoundFile *)readInfo:(NSString *)filename
|
|
|
|
{
|
|
|
|
BOOL b;
|
|
|
|
SoundFile *soundFile;
|
|
|
|
|
|
|
|
soundFile = [SoundFile soundFileFromFilename:filename];
|
|
|
|
|
|
|
|
b = [soundFile readInfo:[filename UTF8String]];
|
2005-06-29 15:28:20 +00:00
|
|
|
|
|
|
|
[soundFile close];
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
if (b == YES)
|
|
|
|
return soundFile;
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reset
|
|
|
|
{
|
2005-06-05 18:52:35 +00:00
|
|
|
[self seekToTime:0.0];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)getFormat:(AudioStreamBasicDescription *)sourceStreamFormat
|
|
|
|
{
|
|
|
|
// NSLog(@"Getting format!");
|
|
|
|
sourceStreamFormat->mFormatID = kAudioFormatLinearPCM;
|
|
|
|
sourceStreamFormat->mFormatFlags = 0;
|
|
|
|
|
|
|
|
sourceStreamFormat->mSampleRate = frequency;
|
|
|
|
|
|
|
|
sourceStreamFormat->mBitsPerChannel = bitsPerSample;
|
|
|
|
|
|
|
|
sourceStreamFormat->mBytesPerFrame = (bitsPerSample/8)*channels;
|
|
|
|
sourceStreamFormat->mChannelsPerFrame = channels;
|
|
|
|
|
|
|
|
sourceStreamFormat->mFramesPerPacket = 1;
|
|
|
|
sourceStreamFormat->mBytesPerPacket = (bitsPerSample/8)*channels;
|
|
|
|
sourceStreamFormat->mReserved = 0;
|
|
|
|
|
|
|
|
if (isBigEndian == YES)
|
|
|
|
{
|
|
|
|
sourceStreamFormat->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
2005-06-06 17:47:29 +00:00
|
|
|
sourceStreamFormat->mFormatFlags |= kLinearPCMFormatFlagIsAlignedHigh;
|
|
|
|
// sourceStreamFormat->mFormatFlags |= kLinearPCMFormatFlagIsNonMixable;
|
2005-06-02 18:16:43 +00:00
|
|
|
// NSLog(@"FUCKER IS BIG ENDIAN");
|
|
|
|
}
|
|
|
|
if (isUnsigned == NO)
|
|
|
|
sourceStreamFormat->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-06-29 15:28:20 +00:00
|
|
|
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(const char *)filename
|
|
|
|
{
|
|
|
|
// NSLog(@"WRONG OPEN!!!");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)readInfo:(const char *)filename
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned long)currentPosition
|
|
|
|
{
|
|
|
|
return currentPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned long)totalSize
|
|
|
|
{
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UInt16)channels
|
|
|
|
{
|
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UInt16)bitsPerSample
|
|
|
|
{
|
|
|
|
return bitsPerSample;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UInt32)frequency
|
|
|
|
{
|
|
|
|
return frequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(BOOL)isBigEndian
|
|
|
|
{
|
|
|
|
return isBigEndian;
|
|
|
|
}
|
|
|
|
-(BOOL)isUnsigned
|
|
|
|
{
|
|
|
|
return isUnsigned;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|