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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* $ProjectName$
* $ProjectRevision$
* -----------------------------------------------------------
* $Id$
* -----------------------------------------------------------
*
* $Author$
*
* Description:
*
* Copyright 2002-2003 Tor-Einar Jarnbjo
* -----------------------------------------------------------
*
* Change History
* -----------------------------------------------------------
* $Log$
* Revision 1.1 2005/07/11 15:42:36 hcl
* Songdb java version, source. only 1.5 compatible
*
* Revision 1.2 2004/09/21 06:39:06 shred
* Importe reorganisiert, damit Eclipse Ruhe gibt. ;-)
*
* Revision 1.1.1.1 2004/04/04 22:09:12 shred
* First Import
*
* Revision 1.2 2003/03/16 01:11:12 jarnbjo
* no message
*
*
*/
package de.jarnbjo.vorbis;
import java.io.IOException;
import de.jarnbjo.util.io.BitInputStream;
class AudioPacket {
private int modeNumber;
private Mode mode;
private Mapping mapping;
private int n; // block size
private boolean blockFlag, previousWindowFlag, nextWindowFlag;
private int windowCenter, leftWindowStart, leftWindowEnd, leftN, rightWindowStart, rightWindowEnd, rightN;
private float[] window;
private float[][] pcm;
private int[][] pcmInt;
private Floor[] channelFloors;
private boolean[] noResidues;
private final static float[][] windows=new float[8][];
protected AudioPacket(final VorbisStream vorbis, final BitInputStream source) throws VorbisFormatException, IOException {
final SetupHeader sHeader=vorbis.getSetupHeader();
final IdentificationHeader iHeader=vorbis.getIdentificationHeader();
final Mode[] modes=sHeader.getModes();
final Mapping[] mappings=sHeader.getMappings();
final Residue[] residues=sHeader.getResidues();
final int channels=iHeader.getChannels();
if(source.getInt(1)!=0) {
throw new VorbisFormatException("Packet type mismatch when trying to create an audio packet.");
}
modeNumber=source.getInt(Util.ilog(modes.length-1));
try {
mode=modes[modeNumber];
}
catch(ArrayIndexOutOfBoundsException e) {
throw new VorbisFormatException("Reference to invalid mode in audio packet.");
}
mapping=mappings[mode.getMapping()];
final int[] magnitudes=mapping.getMagnitudes();
final int[] angles=mapping.getAngles();
blockFlag=mode.getBlockFlag();
final int blockSize0=iHeader.getBlockSize0();
final int blockSize1=iHeader.getBlockSize1();
n=blockFlag?blockSize1:blockSize0;
if(blockFlag) {
previousWindowFlag=source.getBit();
nextWindowFlag=source.getBit();
}
windowCenter=n/2;
if(blockFlag && !previousWindowFlag) {
leftWindowStart=n/4-blockSize0/4;
leftWindowEnd=n/4+blockSize0/4;
leftN=blockSize0/2;
}
else {
leftWindowStart=0;
leftWindowEnd=n/2;
leftN=windowCenter;
}
if(blockFlag && !nextWindowFlag) {
rightWindowStart=n*3/4-blockSize0/4;
rightWindowEnd=n*3/4+blockSize0/4;
rightN=blockSize0/2;
}
else {
rightWindowStart=windowCenter;
rightWindowEnd=n;
rightN=n/2;
}
window=getComputedWindow();//new double[n];
channelFloors=new Floor[channels];
noResidues=new boolean[channels];
pcm=new float[channels][n];
pcmInt=new int[channels][n];
boolean allFloorsEmpty=true;
for(int i=0; i<channels; i++) {
int submapNumber=mapping.getMux()[i];
int floorNumber=mapping.getSubmapFloors()[submapNumber];
Floor decodedFloor=sHeader.getFloors()[floorNumber].decodeFloor(vorbis, source);
channelFloors[i]=decodedFloor;
noResidues[i]=decodedFloor==null;
if(decodedFloor!=null) {
allFloorsEmpty=false;
}
}
if(allFloorsEmpty) {
return;
}
for(int i=0; i<magnitudes.length; i++) {
if(!noResidues[magnitudes[i]] ||
!noResidues[angles[i]]) {
noResidues[magnitudes[i]]=false;
noResidues[angles[i]]=false;
}
}
Residue[] decodedResidues=new Residue[mapping.getSubmaps()];
for(int i=0; i<mapping.getSubmaps(); i++) {
int ch=0;
boolean[] doNotDecodeFlags=new boolean[channels];
for(int j=0; j<channels; j++) {
if(mapping.getMux()[j]==i) {
doNotDecodeFlags[ch++]=noResidues[j];
}
}
int residueNumber=mapping.getSubmapResidues()[i];
Residue residue=residues[residueNumber];
residue.decodeResidue(vorbis, source, mode, ch, doNotDecodeFlags, pcm);
}
for(int i=mapping.getCouplingSteps()-1; i>=0; i--) {
double newA=0, newM=0;
final float[] magnitudeVector=pcm[magnitudes[i]];
final float[] angleVector=pcm[angles[i]];
for(int j=0; j<magnitudeVector.length; j++) {
float a=angleVector[j];
float m=magnitudeVector[j];
if(a>0) {
//magnitudeVector[j]=m;
angleVector[j]=m>0?m-a:m+a;
}
else {
magnitudeVector[j]=m>0?m+a:m-a;
angleVector[j]=m;
}
}
}
for(int i=0; i<channels; i++) {
if(channelFloors[i]!=null) {
channelFloors[i].computeFloor(pcm[i]);
}
}
// perform an inverse mdct to all channels
for(int i=0; i<channels; i++) {
MdctFloat mdct=blockFlag?iHeader.getMdct1():iHeader.getMdct0();
mdct.imdct(pcm[i], window, pcmInt[i]);
}
}
private float[] getComputedWindow() {
int ix=(blockFlag?4:0)+(previousWindowFlag?2:0)+(nextWindowFlag?1:0);
float[] w=windows[ix];
if(w==null) {
w=new float[n];
for(int i=0;i<leftN;i++){
float x=(float)((i+.5)/leftN*Math.PI/2.);
x=(float)Math.sin(x);
x*=x;
x*=(float)Math.PI/2.;
x=(float)Math.sin(x);
w[i+leftWindowStart]=x;
}
for(int i=leftWindowEnd; i<rightWindowStart; w[i++]=1.0f);
for(int i=0;i<rightN;i++){
float x=(float)((rightN-i-.5)/rightN*Math.PI/2.);
x=(float)Math.sin(x);
x*=x;
x*=(float)Math.PI/2.;
x=(float)Math.sin(x);
w[i+rightWindowStart]=x;
}
windows[ix]=w;
}
return w;
}
protected int getNumberOfSamples() {
return rightWindowStart-leftWindowStart;
}
protected int getPcm(final AudioPacket previousPacket, final int[][] buffer) {
int channels=pcm.length;
int val;
// copy left window flank and mix with right window flank from
// the previous audio packet
for(int i=0; i<channels; i++) {
int j1=0, j2=previousPacket.rightWindowStart;
final int[] ppcm=previousPacket.pcmInt[i];
final int[] tpcm=pcmInt[i];
final int[] target=buffer[i];
for(int j=leftWindowStart; j<leftWindowEnd; j++) {
val=ppcm[j2++]+tpcm[j];
if(val>32767) val=32767;
if(val<-32768) val=-32768;
target[j1++]=val;
}
}
// use System.arraycopy to copy the middle part (if any)
// of the window
if(leftWindowEnd+1<rightWindowStart) {
for(int i=0; i<channels; i++) {
System.arraycopy(pcmInt[i], leftWindowEnd, buffer[i], leftWindowEnd-leftWindowStart, rightWindowStart-leftWindowEnd);
}
}
return rightWindowStart-leftWindowStart;
}
protected void getPcm(final AudioPacket previousPacket, final byte[] buffer) {
int channels=pcm.length;
int val;
// copy left window flank and mix with right window flank from
// the previous audio packet
for(int i=0; i<channels; i++) {
int ix=0, j2=previousPacket.rightWindowStart;
final int[] ppcm=previousPacket.pcmInt[i];
final int[] tpcm=pcmInt[i];
for(int j=leftWindowStart; j<leftWindowEnd; j++) {
val=ppcm[j2++]+tpcm[j];
if(val>32767) val=32767;
if(val<-32768) val=-32768;
buffer[ix+(i*2)+1]=(byte)(val&0xff);
buffer[ix+(i*2)]=(byte)((val>>8)&0xff);
ix+=channels*2;
}
ix=(leftWindowEnd-leftWindowStart)*channels*2;
for(int j=leftWindowEnd; j<rightWindowStart; j++) {
val=tpcm[j];
if(val>32767) val=32767;
if(val<-32768) val=-32768;
buffer[ix+(i*2)+1]=(byte)(val&0xff);
buffer[ix+(i*2)]=(byte)((val>>8)&0xff);
ix+=channels*2;
}
}
}
protected float[] getWindow() {
return window;
}
protected int getLeftWindowStart() {
return leftWindowStart;
}
protected int getLeftWindowEnd() {
return leftWindowEnd;
}
protected int getRightWindowStart() {
return rightWindowStart;
}
protected int getRightWindowEnd() {
return rightWindowEnd;
}
public int[][] getPcm() {
return pcmInt;
}
public float[][] getFreqencyDomain() {
return pcm;
}
}
|