1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#import "XADWinZipWavPackHandle.h"
#import "XADException.h"
#define SamplesPerBuffer 4096
extern WavpackStreamReader inputreader;
@implementation XADWinZipWavPackHandle
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
{
if((self=[super initWithHandle:handle length:length]))
{
context=NULL;
buffer=NULL;
}
return self;
}
-(void)dealloc
{
if(context) WavpackCloseFile(context);
free(buffer);
[super dealloc];
}
-(void)resetBlockStream
{
if(context) { WavpackCloseFile(context); context=NULL; }
free(buffer); buffer=NULL;
char error[80];
context=WavpackOpenFileInputEx(&inputreader,input,NULL,error,OPEN_WRAPPER,0);
if(!context) [NSException raise:@"WavPackException" format:@"Error opening WavPack stream: %s",error];
int numchannels=WavpackGetNumChannels(context);
buffer=malloc(numchannels*SamplesPerBuffer*4);
header=YES;
}
-(int)produceBlockAtOffset:(off_t)pos
{
if(header)
{
header=NO;
headerlength=WavpackGetWrapperBytes(context);
[self setBlockPointer:WavpackGetWrapperData(context)];
return headerlength;
}
else
{
int numchannels=WavpackGetNumChannels(context);
int bytespersample=WavpackGetBytesPerSample(context);
int numsamples=WavpackUnpackSamples(context,(int32_t *)buffer,SamplesPerBuffer);
if(numsamples)
{
int32_t *expandedptr=(int32_t *)buffer;
uint8_t *compactptr=buffer;
switch(bytespersample)
{
case 1:
for(int i=0;i<numsamples*numchannels;i++)
{
int32_t value=*expandedptr++;
*compactptr++=(value+0x80)&0xff;
}
break;
case 2:
for(int i=0;i<numsamples*numchannels;i++)
{
int32_t value=*expandedptr++;
*compactptr++=value&0xff;
*compactptr++=(value>>8)&0xff;
}
break;
case 3:
for(int i=0;i<numsamples*numchannels;i++)
{
int32_t value=*expandedptr++;
*compactptr++=value&0xff;
*compactptr++=(value>>8)&0xff;
*compactptr++=(value>>16)&0xff;
}
break;
case 4:
for(int i=0;i<numsamples*numchannels;i++)
{
int32_t value=*expandedptr++;
*compactptr++=value&0xff;
*compactptr++=(value>>8)&0xff;
*compactptr++=(value>>16)&0xff;
*compactptr++=(value>>24)&0xff;
}
break;
}
[self setBlockPointer:buffer];
return numchannels*numsamples*bytespersample;
}
else
{
[self setBlockPointer:WavpackGetWrapperData(context)+headerlength];
[self endBlockStream];
return WavpackGetWrapperBytes(context)-headerlength;
}
}
}
@end
static int32_t ReadBytes(void *context,void *data,int32_t numbytes)
{
CSInputBuffer *input=context;
uint8_t *buffer=data;
for(int i=0;i<numbytes;i++)
{
if(CSInputAtEOF(input)) return i;
buffer[i]=CSInputNextByte(input);
}
return numbytes;
}
static uint32_t GetPos(void *context)
{
CSInputBuffer *input=context;
return CSInputBufferOffset(input);
}
static int SetPosAbs(void *context,uint32_t pos)
{
CSInputBuffer *input=context;
CSInputSeekToBufferOffset(input,pos);
return 0;
}
static int SetPosRel(void *context,int32_t delta,int mode)
{
CSInputBuffer *input=context;
switch(mode)
{
case SEEK_SET: CSInputSeekToBufferOffset(input,delta); break;
case SEEK_CUR: CSInputSeekToBufferOffset(input,CSInputBufferOffset(input)+delta); break;
case SEEK_END: [XADException raiseNotSupportedException]; break;
}
return 0;
}
static int PushBackByte(void *context,int c) { [XADException raiseNotSupportedException]; return EOF; }
static uint32_t GetLength(void *context) { return 0; }
static int CanSeek(void *context) { return 1; }
WavpackStreamReader inputreader=
{
.read_bytes=ReadBytes,
.get_pos=GetPos,
.set_pos_abs=SetPosAbs,
.set_pos_rel=SetPosRel,
.push_back_byte=PushBackByte,
.get_length=GetLength,
.can_seek=CanSeek,
.write_bytes=NULL,
};
|