#include
#include
#include
using namespace std;
#define OUTPUT_DATA_SIZE 100000
int16_t output_data[OUTPUT_DATA_SIZE];
unsigned long int output_data_count = 0;
int main(int argc, char *argv[])
{
FILE *f = fopen("Q700RawSoundData2", "rb");
FILE *w = fopen("Q700DataOut", "wb");
int8_t packet[15];
int16_t sinc_wave[56] = {32767, 32070, 21612, 4880, -11155, -21612, -23355, -17429, -6972, 4532, 12549, 15338, 12549, 5926, -2092, -8017, -10806, -9760, -5926, -697, 4183, 6623, 6623, 4183, 697, -2789, -4880, -5229, -3834, -1743, 1046, 2440, 3137, 2440, 1046, -697, -2092, -2440, -2440, -1394, -349, 697, 1394, 1394, 697, -349, -1046, -1394, -1394, -1046, -697, 0, 349, 697, 349, 0};
int16_t saw_wave[80] = {32767, 30327, 27887, 25795, 24052, 22658, 21264, 20218, 18475, 17778, 16384, 15338, 14292, 13595, 12549, 11852, 11155, 10458, 9760, 9063, 8715, 8017, 7320, 6972, 6275, 5926, 5577, 5229, 4880, 4880, 4532, 4183, 3834, 3834, 3486, 3137, 3137, 2789, 2789, 2440, 2440, 2092, 2092, 2092, 1743, 1743, 1743, 1394, 1394, 1394, 1394, 1046, 1046, 1046, 697, 697, 697, 697, 697, 697, 697, 697, 349, 349, 349, 349, 349, 349, 349, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int16_t hump_wave[64] = {30496, 32118, 32767, 31794, 31145, 29847, 28225, 26603, 24656, 22710, 21088, 19141, 17519, 15572, 13950, 12653, 11355, 10057, 8759, 7462, 6489, 5515, 4542, 3893, 3244, 2595, 2271, 1622, 1298, 973, 649, 649, 324, 0, 0, 0, -324, -324, -324, -324, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int8_t nibble_1, nibble_2;
signed int new_sample;
// Clear output data.
for(int i = 0; i < OUTPUT_DATA_SIZE; i++)
{
output_data[i] = 0;
}
output_data_count = 0;
while (fread(packet, 1, 15, f)) // Read in entire packets until EOF.
{
if(0x00 == packet[0] & 0x30) // Previous samples that extend into a direct sample are overridden.
{
for(int i = output_data_count; i < output_data_count + 80; i++)
{
output_data[i] = 0; // Clear remaining samples.
}
}
for(int i = 1; i < 15; i++) // for each packet data byte
{
// Separate and sign extend each nibble.
nibble_1 = packet[i] & 0x0F; // Least significant nibble plays first.
nibble_2 = (packet[i] & 0xF0) >> 4;
if(nibble_1 & 0x08) nibble_1 |= 0xF0; // Sign extend
if(nibble_2 & 0x08) nibble_2 |= 0xF0;
switch(packet[0] & 0x30)
{
case 0x00: // Direct samples
new_sample = (nibble_1 * 512);
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
output_data[output_data_count] = new_sample;
output_data_count++;
new_sample = (nibble_2 * 512);
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
output_data[output_data_count] = new_sample;
output_data_count++;
break;
case 0x10: // Saw wave pulses
for(int j = 0; j < 80; j++) // follow through to each remaining sample.
{
new_sample = saw_wave[i] * nibble_1;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
for(int j = 0; j < 80; j++) // follow through to each remaining sample.
{
new_sample = saw_wave[i] * nibble_2;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
break;
case 0x20: // Hump wave pulses
for(int j = 0; j < 64; j++) // follow through to each remaining sample.
{
new_sample = hump_wave[i] * 141 / 106 * nibble_1 / 3;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
for(int j = 0; j < 64; j++) // follow through to each remaining sample.
{
new_sample = hump_wave[i] * 141 / 106 * nibble_2 / 3;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
break;
case 0x30: // Sinc wave pulses
for(int j = 0; j < 56; j++) // follow through to each remaining sample.
{
new_sample = sinc_wave[i] * nibble_1 / 5;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
for(int j = 0; j < 56; j++) // follow through to each remaining sample.
{
new_sample = sinc_wave[i] * nibble_2 / 5;
for(int k = (packet[0] & 0x0F); k != 0; k--) new_sample /= 2; // Can't shift because of signed numbers.
new_sample += output_data[output_data_count + j];
if(new_sample > 32767) output_data[output_data_count + j] = 32767;
else if(new_sample < -32768) output_data[output_data_count + j] = -32768;
else output_data[output_data_count + j] = new_sample;
}
output_data_count++;
break;
default:
cerr << "Error when parsing sound characteristic nibble.\n";
break;
}
}
}
//cout << output_data_count;
fwrite(output_data, 2, output_data_count, w);
fclose(f);
fclose(w);
}