2005-06-02 18:16:43 +00:00
|
|
|
//
|
|
|
|
// WaveFile.m
|
|
|
|
// zyVorbis
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 1/31/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 "WaveFile.h"
|
|
|
|
|
|
|
|
@implementation WaveFile
|
|
|
|
|
|
|
|
- (BOOL)open:(const char *)filename
|
|
|
|
{
|
|
|
|
sndFile = sf_open(filename, SFM_READ, &info);
|
|
|
|
|
|
|
|
if (sndFile == NULL)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
return [self readInfo];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)readInfo
|
|
|
|
{
|
2006-06-19 00:39:41 +00:00
|
|
|
bitrate = 0;
|
2005-06-02 18:16:43 +00:00
|
|
|
frequency = info.samplerate;
|
|
|
|
channels = info.channels;
|
|
|
|
isUnsigned = NO;
|
|
|
|
|
|
|
|
switch (info.format & SF_FORMAT_ENDMASK)
|
|
|
|
{
|
|
|
|
case SF_ENDIAN_FILE:
|
|
|
|
if (((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AIFF) || ((info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU))
|
|
|
|
isBigEndian = YES;
|
|
|
|
else
|
|
|
|
isBigEndian = NO;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SF_ENDIAN_CPU:
|
2006-04-21 20:43:47 +00:00
|
|
|
isBigEndian = hostIsBigEndian();
|
2006-04-18 21:16:01 +00:00
|
|
|
//DBLog(@"&CPU ENDIAN");
|
2005-06-02 18:16:43 +00:00
|
|
|
break;
|
|
|
|
case SF_ENDIAN_LITTLE:
|
|
|
|
isBigEndian = NO;
|
|
|
|
// DBLog(@"&LITTLE INDIAN");
|
|
|
|
break;
|
|
|
|
case SF_ENDIAN_BIG:
|
|
|
|
isBigEndian = YES;
|
|
|
|
// DBLog(@"&BIG ENDIAN");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isBigEndian = NO;
|
|
|
|
// DBLog(@"&WHAT THE FUCK IS GOING ON?!!!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info.format & SF_FORMAT_SUBMASK)
|
|
|
|
{
|
|
|
|
case SF_FORMAT_PCM_S8:
|
|
|
|
bitsPerSample = 8;
|
|
|
|
break;
|
|
|
|
case SF_FORMAT_PCM_16:
|
|
|
|
bitsPerSample = 16;
|
|
|
|
break;
|
|
|
|
case SF_FORMAT_PCM_24:
|
|
|
|
bitsPerSample = 24;
|
|
|
|
break;
|
|
|
|
case SF_FORMAT_PCM_32:
|
|
|
|
bitsPerSample = 32;
|
|
|
|
break;
|
|
|
|
case SF_FORMAT_PCM_U8:
|
|
|
|
isUnsigned = YES;
|
|
|
|
bitsPerSample = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBLog(@"BITS PER SAMPLE NOT DEFINED");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalSize = info.frames*channels*bitsPerSample/8;
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)readInfo:(const char *)filename
|
|
|
|
{
|
2005-06-29 15:28:20 +00:00
|
|
|
[self open:filename];
|
|
|
|
|
|
|
|
return YES;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
|
|
|
|
{
|
|
|
|
int numread;
|
|
|
|
|
|
|
|
numread = sf_read_raw(sndFile, buf, size);
|
|
|
|
/*
|
|
|
|
if (isBigEndian == YES)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
for (n = 0; n < numread/2; n++)
|
|
|
|
{
|
|
|
|
((UInt16 *)buf)[n] = CFSwapInt16LittleToHost(((UInt16 *)buf)[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return numread;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
if (sndFile)
|
|
|
|
sf_close(sndFile);
|
|
|
|
sndFile = NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-05 18:52:35 +00:00
|
|
|
- (double)seekToTime:(double)milliseconds
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
sf_seek(sndFile, frequency*((double)milliseconds/1000.0), SEEK_SET);
|
2005-06-05 18:52:35 +00:00
|
|
|
|
|
|
|
return milliseconds;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|