#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
FILE *f = fopen("Q700RawSoundData2", "rb");
FILE *w = fopen("Q700DataOut", "wb");
int8_t packet[15];
int16_t sinc_wave[28] = {0, 17162, 29319, 34628, 32767, 24841, 13019, 0, -11581, -19657, -23071, -21701, -16360, -8532, 0, 7530, 12751, 14951, 14072, 10636, 5573, 0, -5009, -8602, -10268, -9880, -7668, -4145};
int16_t dull_wave[28] = {32767, 31343, 29962, 28623, 27326, 26072, 24860, 23691, 22564, 21479, 20437, 19437, 18480, 17565, 16692, 15862, 15074, 14329, 13626, 12965, 12347, 11771, 11237, 10746, 10298, 9891, 9527, 9206};
int16_t s_wave[28] = {0, 19660, 31311, 32403, 32767, 32767, 32039, 30947, 30218, 29126, 27670, 26214, 25121, 23665, 22209, 20752, 19296, 17840, 16748, 15291, 14199, 13107, 12015, 10922, 9830, 8738, 8010, 6917};
int16_t output_data[28];
int8_t nibble;
while (!feof(f))
{
// Read in entire packet.
for(int i = 0; i < 15; i++)
{
fread(&packet[i], 1, 1, f);
}
// Switch for first nibble (sound characteristic)
switch(packet[0] & 0x30)
{
case 0x00: // Direct samples
for(int i = 1; i < 15; i++) // for each packet data byte
{
// FIRST NIBBLE
nibble = (packet[i] & 0xF0) >> 4;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
output_data[2 * (i-1)] = (nibble * 512) >> ((packet[0] & 0x0F) - 3);
// SECOND NIBBLE
nibble = (packet[i] & 0xF0) >> 4;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
output_data[(2 * (i-1)) + 1] = (nibble * 512) >> ((packet[0] & 0x0F) - 3);
}
break;
case 0x10: // Dull slope samples
for (int i = 0; i < 28; i++)
{
output_data[i] = 0;//output_data[27]; // Clear output data.
// Note: PROBABLY needs to start at the previous raw from the previous packet, not 0.
}
for(int i = 1; i < 15; i++) // for each packet data byte
{
// FIRST NIBBLE
nibble = (packet[i] & 0xF0) >> 4;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 1; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((dull_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
// SECOND NIBBLE
nibble = (packet[i] & 0x0F) >> 0;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 0; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((dull_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
}
break;
case 0x20: // S-Curve samples
for (int i = 0; i < 28; i++)
{
output_data[i] = 0;//output_data[27]; // Clear output data.
// Note: PROBABLY needs to start at the previous raw from the previous packet, not 0.
}
for(int i = 1; i < 15; i++) // for each packet data byte
{
// FIRST NIBBLE
nibble = (packet[i] & 0xF0) >> 4;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 1; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((s_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
// SECOND NIBBLE
nibble = (packet[i] & 0x0F) >> 0;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 0; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((s_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
}
break;
case 0x30: // Sinc pulse samples
for (int i = 0; i < 28; i++)
{
output_data[i] = 0;//output_data[27]; // Clear output data.
// Note: PROBABLY needs to start at the previous raw from the previous packet, not 0.
}
for(int i = 1; i < 15; i++) // for each packet data byte
{
// FIRST NIBBLE
nibble = (packet[i] & 0xF0) >> 4;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 1; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((sinc_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
// SECOND NIBBLE
nibble = (packet[i] & 0x0F) >> 0;
if(nibble & 0x08)
{
nibble |= 0xF0; // Sign extend.
}
if(nibble > 5) nibble = 5; // nibble max is 5 with sinc.
if(nibble < -5) nibble = -5; // This line is not verified.
for(int j = i - 0; j < 28; j++) // follow through to each remaining sample.
{
output_data[j] = output_data[j] + ((sinc_wave[j] * nibble) / 5) >> ((packet[0] & 0x0F) - 3);
}
}
break;
default:
cerr << "Error when parsing sound characteristic nibble.\n";
break;
}
for(int i = 0; i < 28; i++)
{
fwrite(&output_data[i], 2, 1, w);
}
}
fclose(f);
fclose(w);
}