How can I improve a wav file using AS3?

I am trying to increase the size of 8000hz, 16-bit wav file to 11025hz in AS3. At the moment, I'm not worried about applying low-pass filters, which, as I know, I will eventually need.

I was linking to this wiki page .

Here is what I have done so far:

  • The smallest total number equal to 3,528,000
  • The calculated L should be 441
  • Calculated M will be 320
  • Added 440 zeros between swatches
  • Every 320th sample is written on a new byte array

However, when I go to play the new wav, it is an indistinguishable noise. Here is my code:

const sourceRate:uint = 8000;
const targetRate:uint = 11025;
var lcm:uint = lcm(targetRate, sourceRate); // = 3528000
var l:uint = lcm / sourceRate; // = 441
var m:uint = lcm / targetRate; // = 320

// upsample by factor of l
var upsampleData:ByteArray = new ByteArray();
upsampleData.endian = Endian.LITTLE_ENDIAN;

// originalWavData is a ByteArray of the source wav data
// fill is a ByteArray that contains 440 zeroes, written using writeShort(0x0)

while(originalWavData.bytesAvailable > 1) {
    upsampleData.writeBytes(fill);
    upsampleData.writeShort(originalWavData.readShort());
}

// downsample by factor of m
var downsampleData:ByteArray = new ByteArray();
downsampleData.endian = Endian.LITTLE_ENDIAN;

upsampleData.position = 0;

for(var k:uint=0; k<upsampleData.length; k++) {
    upsampleData.position = k * m;
    if(upsampleData.bytesAvailable < 2) break;
    downsampleData.writeShort(upsampleData.readShort());
}

Can someone show me what I'm doing wrong in my code? This is my first question, so if I forgot something or you need to provide more information, please let me know.

Thank!

Update:

Aric , :

/**
 * Generates a ByteArray containing numSamples of
 * data using linear interpolation between points
 * y0 and y1.
 */
function interpolate(y0:int, y1:int, numSamples:uint):ByteArray {
    var b:ByteArray = new ByteArray();
    b.endian = Endian.LITTLE_ENDIAN;
    var m:Number = Math.round((y1-y0)/numSamples);
    for(var i:uint=0; i<numSamples; i++) {
        var n:int = m * i + y0;
        b.writeShort(n);
    }
    b.position = 0;
    return 0;
}

// upsample by factor of l
var n1:int = 0;
while(originalWavData.bytesAvailable > 1) {
    var sample:int = originalWavData.readShort();
    upsampleData.writeBytes(interpolate(n1, sample, (l-1)));
    n1 = sample;
}

// downsample by factor of m
while(upsampleData.bytesAvailable > 1) {
    downsampleData.writeShort(upsampleData.readShort());
    upsampleData.position += ((m-1)*2);
}

, : , . , - , . n1 . , , , M- , .

, 1000 , , . .

+3
1

- , ? Upsampling , , CD "CD-Quality".

, , , . , , , 3528000 , 11025 .

- . , 8 : [15, 25, 33].

3528000 , (441 * 3 = 1323). 220- 15, 661- 25, 1102- 33. , .

, 1323 4 . 160-480, - 481-800, - 801-1160, - 1161-1480. , 1324-1480, # 4 .

. , , . - , , , .

+5

Source: https://habr.com/ru/post/1725903/


All Articles