Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2021 Jackson Shelton.
- This code cannot be distributed without the creator's explicit permission.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdint.h>
- #include <math.h>
- #include <unistd.h>
- #define variant_pmnw 0x18
- #define variant_pnwm 0x25
- #define variant_mpnw 0x32
- #define variant_mnwp 0x3F
- #define variant_nwpm 0x4C
- #define variant_nwmp 0x59
- /*
- Dynamic amplitude combination choosing algorithm ideas:
- (((curr_pulse + prev_noisewave)*(prev_mv+1)) +
- ((curr_pulse + prev_noisewave)*(curr_mv+1)) +
- ((curr_pulse + curr_noisewave)*(curr_mv+1)))
- / 3 = avg_amp
- (Average the amplitudes of the 3 stages of register writes)
- Whichever amplitude combination is closest to the intended
- amplitude will be chosen.
- */
- /*
- Dynamic amplitude combination choosing improvement:
- The difference is positive when choosing the best variant,
- but should be negative for this algorithm(?)
- Search all amplitudes within plus or minus the best difference
- from the intended amplitude
- That target amplitude whose difference added to it that is closest
- to the intended amplitude gets chosen
- (Or, alternatively, an amplitude whose signed difference is within
- the range, whose difference between itself and the combination
- switch is less than the current amplitude's difference)
- Another threshold could be implemented where the difference
- has to lie in between the two amplitudes.
- If that limit can't be met, then move on to the next part of the
- algorithm.
- That is:
- Any amplitude with its (signed) difference added to it that
- exceeds the difference range of the intended amplitude gets axed
- try variants:
- 1. previous amplitude remains the amplitude read from ROM
- 2. previous amplitude gets set to the chosen best amplitude
- */
- struct hq_lut_t
- {
- uint8_t pulse_lut[256];
- uint8_t mv_lut[256];
- int amp_lut[256];
- };
- struct shq_lut_t
- {
- uint8_t pulse_lut[1024][9];
- uint8_t mv_lut[1024][9];
- uint8_t nw_lut[1024][9];
- int amp_lut[1024];
- int repeats[1024];
- int prev_index;
- int prev_amp;
- int max_repeats;
- int max_index;
- };
- void init_hq_lut(struct hq_lut_t *hq_lut)
- {
- for(int i=0; i<256; i++)
- {
- hq_lut->pulse_lut[i] = 0;
- hq_lut->mv_lut[i] = 0;
- hq_lut->amp_lut[i] = -1;
- }
- }
- void init_shq_lut(struct shq_lut_t *shq_lut)
- {
- for(int j=0; j<2; j++)
- {
- for(int i=0; i<1024; i++)
- {
- shq_lut->pulse_lut[i][j] = 0;
- shq_lut->mv_lut[i][j] = 0;
- shq_lut->nw_lut[i][j] = 0;
- shq_lut->amp_lut[i] = -1;
- shq_lut->repeats[i] = 0;
- shq_lut->prev_index = 0;
- shq_lut->prev_amp = 0;
- }
- }
- }
- void generate_hq_lut(struct hq_lut_t *hq_lut)
- {
- init_hq_lut(hq_lut);
- int pulse = 0;
- int uniqueamps = 0;
- int outamp = 0;
- for(int m=0; m<8; m++)
- {
- for(int p=0; p<16; p++)
- {
- if(p<8)
- {
- pulse = p-8;
- pulse = (pulse*2)+1;
- outamp = (int)(128.0+((128.0/120.0)*((double)(pulse*(m+1)))));
- }
- else
- {
- pulse = p-7;
- pulse = (pulse*2)-1;
- outamp = (int)(127.0+((128.0/120.0)*((double)(pulse*(m+1)))));
- }
- if(hq_lut->amp_lut[outamp]==-1)
- {
- uniqueamps++;
- hq_lut->pulse_lut[outamp] = (uint8_t)p;
- hq_lut->mv_lut[outamp] = (uint8_t)m;
- hq_lut->amp_lut[outamp] = outamp;
- }
- }
- }
- printf("no. of unique amplitudes (hq) = %d\n", uniqueamps);
- uint8_t temp_mv, temp_pulse;
- temp_pulse = 0;
- temp_mv = 0;
- int temp_amp;
- temp_amp = 0;
- for(int i=0; i<128; i++)
- {
- if(hq_lut->amp_lut[i]==i)
- {
- temp_pulse = hq_lut->pulse_lut[i];
- temp_mv = hq_lut->mv_lut[i];
- temp_amp = hq_lut->amp_lut[i];
- }
- else
- {
- hq_lut->pulse_lut[i] = temp_pulse;
- hq_lut->mv_lut[i] = temp_mv;
- hq_lut->amp_lut[i] = temp_amp;
- }
- }
- for(int i=255; i>=128; i--)
- {
- if(hq_lut->amp_lut[i]==i)
- {
- temp_pulse = hq_lut->pulse_lut[i];
- temp_mv = hq_lut->mv_lut[i];
- temp_amp = hq_lut->amp_lut[i];
- }
- else
- {
- hq_lut->pulse_lut[i] = temp_pulse;
- hq_lut->mv_lut[i] = temp_mv;
- hq_lut->amp_lut[i] = temp_amp;
- }
- }
- }
- int shq_calculate_outamp(int p, uint8_t nw, uint8_t m)
- {
- int pulse = 0;
- int outamp = 0;
- int noisewave = 0;
- int pu = p;
- uint8_t nwi = nw;
- if(nwi==0)
- {
- noisewave = 0;
- }
- else if(nwi==1)
- {
- noisewave = 15;
- }
- else
- {
- noisewave = -15;
- }
- if(pu<8)
- {
- pulse = pu-8;
- pulse = (pulse*2)+1;
- outamp = (int)(512.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
- }
- else
- {
- pulse = pu-7;
- pulse = (pulse*2)-1;
- outamp = (int)(511.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
- }
- return outamp;
- }
- //returns the difference between avg amplitude and intended
- //and sets the previous amplitude to the current amplitude
- //edit? make it return the address to the intended routine?
- uint8_t shq_get_best_amplitude(struct shq_lut_t *shq_lut, int curr_amp)
- {
- uint8_t variants[6] = {variant_pmnw, variant_pnwm,
- variant_mpnw, variant_mnwp, variant_nwpm, variant_nwmp};
- int prev_amp = shq_lut->prev_amp;
- int prev_index = shq_lut->prev_index;
- int outamps[6][shq_lut->repeats[curr_amp]][2]; //[trigger order][combination index][combination stage]
- int avg_outamp[6][shq_lut->repeats[curr_amp]];
- int p = 0;
- uint8_t nw, m;
- nw = m = 0;
- int outamp_diff[6][shq_lut->repeats[curr_amp]];
- int final_avg = 0;
- for(int j=0; j<6; j++)
- {
- for(int i=0; i<shq_lut->repeats[curr_amp]; i++)
- {
- if(j<2)
- {
- //pmnw and pnwm
- p = (int)shq_lut->pulse_lut[curr_amp][i];
- m = shq_lut->mv_lut[prev_amp][prev_index];
- nw = shq_lut->nw_lut[prev_amp][prev_index];
- }
- if(j>=2 && j<4)
- {
- //mpnw and mnwp
- p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
- m = shq_lut->mv_lut[curr_amp][i];
- nw = shq_lut->nw_lut[prev_amp][prev_index];
- }
- if(j>=4)
- {
- //nwpm and nwmp
- p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
- m = shq_lut->mv_lut[prev_amp][prev_index];
- nw = shq_lut->nw_lut[curr_amp][i];
- }
- outamps[j][i][0] = shq_calculate_outamp(p, nw, m);
- if(j==0 || j==5)
- {
- //pmnw and nwmp (master volume)
- m = shq_lut->mv_lut[curr_amp][i];
- //m = shq_lut->mv_lut[prev_amp][prev_index];
- }
- if(j==2 || j==4)
- {
- //mpnw and nwpm (pulse)
- p = (int)shq_lut->pulse_lut[curr_amp][i];
- //p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
- }
- if(j==1 || j==3)
- {
- //pnwm and mnwp (noisewave)
- nw = shq_lut->nw_lut[curr_amp][i];
- //nw = shq_lut->nw_lut[prev_amp][prev_index];
- }
- outamps[j][i][1] = shq_calculate_outamp(p, nw, m);
- avg_outamp[j][i] = outamps[j][i][0] + outamps[j][i][1];
- //printf("FUCK\n");
- final_avg = avg_outamp[j][i];
- if((final_avg>=(curr_amp*2) && final_avg<=(prev_amp*2)) || (final_avg<=(curr_amp*2) && final_avg>=(prev_amp*2)))
- {
- shq_lut->prev_index = i;
- return variants[j];
- }
- if(final_avg>=(curr_amp*2))
- {
- outamp_diff[j][i] = (final_avg - (curr_amp*2));
- }
- else
- {
- outamp_diff[j][i] = ((curr_amp*2) - final_avg);
- }
- }
- }
- //printf("getting best amplitude\n");
- shq_lut->prev_amp = curr_amp;
- int best_diff = 1024;
- int best_index = 0;
- int best_order = 0;
- if(shq_lut->repeats[curr_amp]>0)
- {
- for(int k=0; k<6; k++)
- {
- for(int i=0; i<shq_lut->repeats[curr_amp]; i++)
- {
- for(int j=i; j<shq_lut->repeats[curr_amp]; j++)
- {
- if((outamp_diff[k][i]<=outamp_diff[k][j]) && outamp_diff[k][i]<best_diff)
- {
- best_index = i;
- best_order = k;
- best_diff = outamp_diff[k][i];
- }
- else if((outamp_diff[k][j]<outamp_diff[k][i]) && outamp_diff[k][j]<best_diff)
- {
- best_index = j;
- best_order = k;
- best_diff = outamp_diff[k][j];
- }
- }
- }
- }
- shq_lut->prev_index = best_index;
- printf("\rbest difference = %4d, best index = %d, best variant = %d", best_diff, best_index, best_order);
- return variants[best_order]; //best_diff;
- }
- else
- {
- for(int k=0; k<6; k++)
- {
- best_order = k;
- if(outamp_diff[k][0]<best_diff)
- {
- best_diff = outamp_diff[k][0];
- }
- }
- shq_lut->prev_index = best_index;
- printf("\rbest difference = %4d, best index = %d, best variant = %d", best_diff, best_index, best_order);
- return variants[best_order]; //best_diff;
- }
- }
- void generate_shq_lut(struct shq_lut_t *shq_lut)
- {
- init_shq_lut(shq_lut);
- int pulse = 0;
- int noisewave = 0;
- int uniqueamps = 0;
- int outamp = 0;
- for(int nw=0; nw<3; nw++)
- {
- for(int m=0; m<8; m++)
- {
- for(int p=0; p<16; p++)
- {
- if(nw==0)
- {
- noisewave = 0;
- }
- else if(nw==1)
- {
- noisewave = 15;
- }
- else
- {
- noisewave = -15;
- }
- if(p<8)
- {
- pulse = p-8;
- pulse = (pulse*2)+1;
- outamp = (int)(512.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
- }
- else
- {
- pulse = p-7;
- pulse = (pulse*2)-1;
- outamp = (int)(511.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
- }
- if(shq_lut->amp_lut[outamp]==-1)
- {
- uniqueamps++;
- shq_lut->pulse_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)p;
- shq_lut->mv_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)m;
- shq_lut->nw_lut[outamp][shq_lut->repeats[outamp]] = (((uint8_t)nw)<<2);
- shq_lut->amp_lut[outamp] = outamp;
- }
- else
- {
- shq_lut->repeats[outamp]++;
- shq_lut->pulse_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)p;
- shq_lut->mv_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)m;
- shq_lut->nw_lut[outamp][shq_lut->repeats[outamp]] = (((uint8_t)nw)<<2);
- }
- }
- }
- }
- printf("no. of unique amplitudes (shq) = %d\n", uniqueamps);
- int max_repeats = 0;
- for(int i=0; i<1024; i++)
- {
- if(shq_lut->repeats[i]>max_repeats)
- {
- max_repeats = shq_lut->repeats[i];
- shq_lut->max_index = i;
- }
- }
- printf("no. of max combination repeats = %d\n", max_repeats);
- shq_lut->max_repeats = max_repeats;
- uint8_t temp_mv, temp_pulse, temp_nw;
- temp_pulse = 0;
- temp_mv = 0;
- temp_nw = 0;
- int temp_amp = 0;
- for(int j=0; j<max_repeats; j++)
- {
- for(size_t i=0; i<512; i++)
- {
- if(shq_lut->amp_lut[i]!=-1)
- {
- temp_pulse = shq_lut->pulse_lut[i][j];
- temp_mv = shq_lut->mv_lut[i][j];
- temp_nw = shq_lut->nw_lut[i][j];
- temp_amp = shq_lut->amp_lut[i];
- }
- else
- {
- shq_lut->pulse_lut[i][j] = temp_pulse;
- shq_lut->mv_lut[i][j] = temp_mv;
- shq_lut->nw_lut[i][j] = temp_nw;
- shq_lut->amp_lut[i] = temp_amp;
- }
- }
- for(size_t i=511; i>0; i--)
- {
- if(shq_lut->amp_lut[i]!=-1)
- {
- temp_pulse = shq_lut->pulse_lut[i][j];
- temp_mv = shq_lut->mv_lut[i][j];
- temp_nw = shq_lut->nw_lut[i][j];
- temp_amp = shq_lut->amp_lut[i];
- }
- else
- {
- shq_lut->pulse_lut[i][j] = temp_pulse;
- shq_lut->mv_lut[i][j] = temp_mv;
- shq_lut->nw_lut[i][j] = temp_nw;
- shq_lut->amp_lut[i] = temp_amp;
- }
- }
- for(size_t i=1023; i>=512; i--)
- {
- if(shq_lut->amp_lut[i]!=-1)
- {
- temp_pulse = shq_lut->pulse_lut[i][j];
- temp_mv = shq_lut->mv_lut[i][j];
- temp_nw = shq_lut->nw_lut[i][j];
- temp_amp = shq_lut->amp_lut[i];
- }
- else
- {
- shq_lut->pulse_lut[i][j] = temp_pulse;
- shq_lut->mv_lut[i][j] = temp_mv;
- shq_lut->nw_lut[i][j] = temp_nw;
- shq_lut->amp_lut[i] = temp_amp;
- }
- }
- for(size_t i=512; i<1024; i++)
- {
- if(shq_lut->amp_lut[i]!=-1)
- {
- temp_pulse = shq_lut->pulse_lut[i][j];
- temp_mv = shq_lut->mv_lut[i][j];
- temp_nw = shq_lut->nw_lut[i][j];
- temp_amp = shq_lut->amp_lut[i];
- }
- else
- {
- shq_lut->pulse_lut[i][j] = temp_pulse;
- shq_lut->mv_lut[i][j] = temp_mv;
- shq_lut->nw_lut[i][j] = temp_nw;
- shq_lut->amp_lut[i] = temp_amp;
- }
- }
- }
- }
- int main(int argc, const char * argv[])
- {
- if (argc != 5) {
- printf("Usage: %s output_dir encoding_type aud_channels timer_div\n", argv[0]);
- return -1;
- }
- char output_dir[128];
- printf("No. of channels: %d\n", atoi(argv[3]));
- strcat(output_dir, "output/build");
- printf("%s\n", output_dir);
- int is_ok = chdir(output_dir);
- printf("%d\n", is_ok);
- int timer_div = (256 - atoi(argv[4]));
- char tim_div[64];
- sprintf(tim_div, "%d", timer_div);
- strcat(tim_div, "\n");
- char div_line[64];
- strcpy(div_line, "ld a, ");
- strcat(div_line, tim_div);
- FILE *div_val;
- if((div_val = fopen("div.asm", "w"))<0)
- {
- printf("Error: couldn't create div.asm\n");
- return 1;
- }
- fputs(div_line, div_val);
- fclose(div_val);
- is_ok = chdir("..");
- is_ok = chdir(argv[1]);
- printf("%d\n", is_ok);
- FILE *audiof = fopen("audio.raw", "rb"); //binary read mode (bytes)
- if (!audiof) {
- perror("Failed to load audio source file");
- return 1;
- }
- fseek(audiof, 0, SEEK_END);
- size_t audio_size, debug_size;
- int audio_mode = 0;
- uint8_t aud_channels = (uint8_t)atoi(argv[3]);
- char *en_mode;
- if((en_mode = strstr(argv[2], "leg"))!=NULL)
- {
- audio_size = ftell(audiof)/2;
- debug_size = ftell(audiof);
- audio_mode = 0;
- }
- else if((en_mode = strstr(argv[2], "shq"))!=NULL)
- {
- if(aud_channels==2)
- {
- audio_size = (size_t)(((long double)(ftell(audiof)*2))/3.0);
- }
- else
- {
- //audio_size = ftell(audiof)+1;
- audio_size = (size_t)(((long double)(ftell(audiof)*3))/2.0);
- printf("audio size = %lx\n", audio_size);
- }
- debug_size = ftell(audiof)+1;
- audio_mode = 2;
- }
- else //if((en_mode = strstr(argv[2], "hq"))!=NULL)
- {
- audio_size = ftell(audiof);
- debug_size = audio_size;
- audio_mode = 1;
- }
- struct hq_lut_t *hq_lut = (struct hq_lut_t *)(malloc(sizeof(struct hq_lut_t)));
- generate_hq_lut(hq_lut);
- struct shq_lut_t *shq_lut = (struct shq_lut_t *)(malloc(sizeof(struct shq_lut_t)));
- generate_shq_lut(shq_lut);
- fseek(audiof, 0 , SEEK_SET);
- size_t main_pos = 0;
- size_t debug_pos = 0;
- /*
- static uint8_t *sawtooth;
- sawtooth = (uint8_t *)malloc(65536*2);
- for(size_t i=0; i<65536*2; i+=2)
- {
- sawtooth[i] = (uint8_t)(((uint16_t)shq_lut.amp_lut[i>>7]<<6)&0xff);
- sawtooth[i+1] = (uint8_t)(((uint16_t)shq_lut.amp_lut[i>>7]<<6)>>8)^0x80;
- }
- FILE *sawtooth_test = fopen("sawtooth_test.raw", "wb");
- fwrite(sawtooth, 1, (65536*2), sawtooth_test);
- fclose(sawtooth_test);
- free(sawtooth);
- */
- static uint8_t *output;
- output = (uint8_t *)malloc(audio_size);
- memset(output, 0, audio_size);
- static uint8_t *debug;
- debug = (uint8_t *)malloc(debug_size);
- memset(debug, 0, debug_size);
- bool done = false;
- if(audio_mode==0)
- {
- while(!done)
- {
- uint8_t left, right;
- if(fread(&left, 1, 1, audiof) != 1 || fread(&right, 1, 1, audiof) != 1)
- {
- left = right = 0x80;
- done = true;
- }
- output[main_pos++] = (left&0xf0)|((right&0xf0)>>4);
- debug[debug_pos++] = (left&0xf0);
- debug[debug_pos++] = (right&0xf0);
- }
- }
- if(audio_mode==1)
- {
- if(aud_channels==2)
- {
- while(!done)
- {
- uint8_t left, right;
- if(fread(&left, 1, 1, audiof) != 1 || fread(&right, 1, 1, audiof) != 1)
- {
- left = right = 0x80;
- done = true;
- }
- output[main_pos++] = (((hq_lut->pulse_lut[left]&0x0f)<<4)|(hq_lut->pulse_lut[right]&0x0f));
- output[main_pos++] = (((hq_lut->mv_lut[left]&0x07)<<4)|(hq_lut->mv_lut[right]&0x07));
- debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[left];
- debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[right];
- }
- }
- if(aud_channels==1)
- {
- while(!done)
- {
- uint8_t samp;
- if(fread(&samp, 1, 1, audiof) != 1)
- {
- samp = 0x80;
- done = true;
- }
- output[main_pos++] = (((hq_lut->pulse_lut[samp]&0x0f)<<4)|(hq_lut->mv_lut[samp]&0x07));
- debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[samp];
- }
- }
- }
- if(audio_mode==2)
- {
- if(aud_channels==2)
- {
- /*
- while(!done)
- {
- uint8_t left[2], right[2];
- if(fread(left, 1, 2, audiof) != 1 || fread(right, 1, 2, audiof) != 1)
- {
- left[0] = right[0] = 0;
- left[1] = right[1] = 0;
- done = true;
- }
- // TODO: amp lut is signed long, convert it to uint16_t before proceeding
- uint16_t left_16 = (((uint16_t)left[1]^0x80)<<8)|(uint16_t)left[0];
- uint16_t right_16 = (((uint16_t)right[1]^0x80)<<8)|(uint16_t)right[0];
- output[main_pos++] = ((shq_lut.pulse_lut[left_16]<<4)|shq_lut.pulse_lut[right_16]);
- output[main_pos++] = ((shq_lut.mv_lut[left_16]<<4)|shq_lut.mv_lut[right_16]);
- output[main_pos++] = ((shq_lut.nw_lut[left_16]<<4)|shq_lut.nw_lut[right_16]);
- uint8_t left_out[2], right_out[2];
- left_out[0] = (uint8_t)(shq_lut.amp_lut[left_16]&0xff);
- left_out[1] = (uint8_t)((shq_lut.amp_lut[left_16]>>8)^0x80);
- right_out[0] = (uint8_t)(shq_lut.amp_lut[right_16]&0xff);
- right_out[1] = (uint8_t)((shq_lut.amp_lut[right_16]>>8)^0x80);
- debug[debug_pos++] = left_out[0];
- debug[debug_pos++] = left_out[1];
- debug[debug_pos++] = right_out[0];
- debug[debug_pos++] = right_out[1];
- }
- */
- }
- if(aud_channels==1)
- {
- //printf("shq encoder\n");
- /*
- uint16_t samp;
- uint8_t *samp_read = (uint8_t *)&samp;
- int ok = fread(samp_read, 1, 2, audiof);
- printf("%d\n", ok);
- samp = (samp^0x8000)>>6;
- */
- shq_lut->prev_amp = shq_lut->max_index;
- shq_lut->prev_index = shq_lut->max_repeats;
- /*
- output[main_pos++] = ((shq_lut.pulse_lut[samp][index]<<4)|(shq_lut.mv_lut[samp][index]&0x07));
- output[main_pos++] = ((shq_lut.nw_lut[samp][index]<<4)|shq_lut.nw_lut[samp][index]);
- short int conv_samp = shq_lut.amp_lut[samp];
- uint16_t final_conv = * ((uint16_t *) &conv_samp);
- uint8_t samp_hi, samp_lo;
- samp_hi = (uint8_t)((final_conv>>2)^0x80);
- samp_lo = (uint8_t)((final_conv<<6)&0xc0);
- debug[debug_pos++] = samp_lo;
- debug[debug_pos++] = samp_hi;
- */
- while(!done && debug_pos<debug_size)
- {
- uint16_t samp;
- uint8_t *samp_read = (uint8_t *)&samp;
- if(fread(samp_read, 1, 2, audiof) != 1)
- {
- //samp = 0;
- //done = true;
- }
- samp = (samp^0x8000)>>6;
- int index;
- //int diff;
- //diff = shq_get_best_amplitude(&shq_lut, samp);
- uint8_t jump_addr;
- jump_addr = shq_get_best_amplitude(shq_lut, samp);
- index = shq_lut->prev_index;
- //printf(", encoding byte %lx\n", main_pos);
- output[main_pos++] = ((shq_lut->pulse_lut[samp][index]<<4)|(shq_lut->mv_lut[samp][index]&0x07));
- output[main_pos++] = ((shq_lut->nw_lut[samp][index]<<4)|shq_lut->nw_lut[samp][index]);
- output[main_pos++] = jump_addr;
- short int conv_samp = shq_lut->amp_lut[samp];
- uint16_t final_conv = * ((uint16_t *) &conv_samp);
- uint8_t samp_hi, samp_lo;
- samp_hi = (uint8_t)((final_conv>>2)^0x80);
- samp_lo = (uint8_t)((final_conv<<6)&0xc0);
- debug[debug_pos++] = samp_lo;
- debug[debug_pos++] = samp_hi;
- if((main_pos&0x3fff)==16383)
- {
- printf("changing banks, address = %16lu\n", main_pos);
- main_pos++;
- }
- }
- }
- }
- free(hq_lut);
- free(shq_lut);
- fclose(audiof);
- FILE *out_aud = fopen("audio_encoded.raw", "wb");
- if(!out_aud)
- {
- perror("Error: cannot write to output audio file\n");
- return 1;
- }
- fwrite(output, 1, audio_size, out_aud);
- fclose(out_aud);
- free(output);
- FILE *debug_aud = fopen("debug_audio.raw", "wb");
- fwrite(debug, 1, debug_size, debug_aud);
- fclose(debug_aud);
- free(debug);
- printf("Audio has been encoded!\n");
- }
Add Comment
Please, Sign In to add comment