jbshelton

GBAudioPlayerV2.5 encoder

Sep 1st, 2021
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.28 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2021 Jackson Shelton.
  3.     This code cannot be distributed without the creator's explicit permission.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <math.h>
  11. #include <unistd.h>
  12.  
  13. #define variant_pmnw 0x18
  14. #define variant_pnwm 0x25
  15. #define variant_mpnw 0x32
  16. #define variant_mnwp 0x3F
  17. #define variant_nwpm 0x4C
  18. #define variant_nwmp 0x59
  19.  
  20. /*
  21.     Dynamic amplitude combination choosing algorithm ideas:
  22.     (((curr_pulse + prev_noisewave)*(prev_mv+1)) +
  23.     ((curr_pulse + prev_noisewave)*(curr_mv+1)) +
  24.     ((curr_pulse + curr_noisewave)*(curr_mv+1)))
  25.     / 3 = avg_amp
  26.     (Average the amplitudes of the 3 stages of register writes)
  27.     Whichever amplitude combination is closest to the intended
  28.     amplitude will be chosen.
  29. */
  30.  
  31. /*
  32.     Dynamic amplitude combination choosing improvement:
  33.     The difference is positive when choosing the best variant,
  34.     but should be negative for this algorithm(?)
  35.     Search all amplitudes within plus or minus the best difference
  36.     from the intended amplitude
  37.     That target amplitude whose difference added to it that is closest
  38.     to the intended amplitude gets chosen
  39.     (Or, alternatively, an amplitude whose signed difference is within
  40.     the range, whose difference between itself and the combination
  41.     switch is less than the current amplitude's difference)
  42.  
  43.     Another threshold could be implemented where the difference
  44.     has to lie in between the two amplitudes.
  45.     If that limit can't be met, then move on to the next part of the
  46.     algorithm.
  47.  
  48.     That is:
  49.     Any amplitude with its (signed) difference added to it that
  50.     exceeds the difference range of the intended amplitude gets axed
  51.  
  52.     try variants:
  53.     1. previous amplitude remains the amplitude read from ROM
  54.     2. previous amplitude gets set to the chosen best amplitude
  55. */
  56.  
  57. struct hq_lut_t
  58. {
  59.     uint8_t pulse_lut[256];
  60.     uint8_t mv_lut[256];
  61.     int amp_lut[256];
  62. };
  63.  
  64. struct shq_lut_t
  65. {
  66.     uint8_t pulse_lut[1024][9];
  67.     uint8_t mv_lut[1024][9];
  68.     uint8_t nw_lut[1024][9];
  69.     int amp_lut[1024];
  70.     int repeats[1024];
  71.     int prev_index;
  72.     int prev_amp;
  73.     int max_repeats;
  74.     int max_index;
  75. };
  76.  
  77. void init_hq_lut(struct hq_lut_t *hq_lut)
  78. {
  79.     for(int i=0; i<256; i++)
  80.     {
  81.         hq_lut->pulse_lut[i] = 0;
  82.         hq_lut->mv_lut[i] = 0;
  83.         hq_lut->amp_lut[i] = -1;
  84.     }
  85. }
  86.  
  87. void init_shq_lut(struct shq_lut_t *shq_lut)
  88. {
  89.     for(int j=0; j<2; j++)
  90.     {
  91.     for(int i=0; i<1024; i++)
  92.     {
  93.         shq_lut->pulse_lut[i][j] = 0;
  94.         shq_lut->mv_lut[i][j] = 0;
  95.         shq_lut->nw_lut[i][j] = 0;
  96.         shq_lut->amp_lut[i] = -1;
  97.         shq_lut->repeats[i] = 0;
  98.         shq_lut->prev_index = 0;
  99.         shq_lut->prev_amp = 0;
  100.     }
  101.     }
  102. }
  103.  
  104. void generate_hq_lut(struct hq_lut_t *hq_lut)
  105. {
  106.     init_hq_lut(hq_lut);
  107.    
  108.     int pulse = 0;
  109.     int uniqueamps = 0;
  110.     int outamp = 0;
  111.  
  112.     for(int m=0; m<8; m++)
  113.     {
  114.         for(int p=0; p<16; p++)
  115.         {
  116.             if(p<8)
  117.             {
  118.                 pulse = p-8;
  119.                 pulse = (pulse*2)+1;
  120.                 outamp = (int)(128.0+((128.0/120.0)*((double)(pulse*(m+1)))));
  121.             }
  122.             else
  123.             {
  124.                 pulse = p-7;
  125.                 pulse = (pulse*2)-1;
  126.                 outamp = (int)(127.0+((128.0/120.0)*((double)(pulse*(m+1)))));
  127.             }
  128.  
  129.             if(hq_lut->amp_lut[outamp]==-1)
  130.             {  
  131.                 uniqueamps++;
  132.                 hq_lut->pulse_lut[outamp] = (uint8_t)p;
  133.                 hq_lut->mv_lut[outamp] = (uint8_t)m;
  134.                 hq_lut->amp_lut[outamp] = outamp;
  135.             }
  136.         }
  137.     }
  138.  
  139.     printf("no. of unique amplitudes (hq) = %d\n", uniqueamps);
  140.     uint8_t temp_mv, temp_pulse;
  141.     temp_pulse = 0;
  142.     temp_mv = 0;
  143.     int temp_amp;
  144.     temp_amp = 0;
  145.  
  146.     for(int i=0; i<128; i++)
  147.     {
  148.         if(hq_lut->amp_lut[i]==i)
  149.         {
  150.             temp_pulse = hq_lut->pulse_lut[i];
  151.             temp_mv = hq_lut->mv_lut[i];
  152.             temp_amp = hq_lut->amp_lut[i];
  153.         }
  154.         else
  155.         {
  156.             hq_lut->pulse_lut[i] = temp_pulse;
  157.             hq_lut->mv_lut[i] = temp_mv;
  158.             hq_lut->amp_lut[i] = temp_amp;
  159.         }
  160.     }
  161.  
  162.     for(int i=255; i>=128; i--)
  163.     {
  164.         if(hq_lut->amp_lut[i]==i)
  165.         {
  166.             temp_pulse = hq_lut->pulse_lut[i];
  167.             temp_mv = hq_lut->mv_lut[i];
  168.             temp_amp = hq_lut->amp_lut[i];
  169.         }
  170.         else
  171.         {
  172.             hq_lut->pulse_lut[i] = temp_pulse;
  173.             hq_lut->mv_lut[i] = temp_mv;
  174.             hq_lut->amp_lut[i] = temp_amp;
  175.         }
  176.     }
  177. }
  178.  
  179. int shq_calculate_outamp(int p, uint8_t nw, uint8_t m)
  180. {
  181.     int pulse = 0;
  182.     int outamp = 0;
  183.     int noisewave = 0;
  184.     int pu = p;
  185.     uint8_t nwi = nw;
  186.    
  187.     if(nwi==0)
  188.     {
  189.         noisewave = 0;
  190.     }
  191.     else if(nwi==1)
  192.     {
  193.         noisewave = 15;
  194.     }
  195.     else
  196.     {
  197.         noisewave = -15;
  198.     }
  199.  
  200.     if(pu<8)
  201.     {
  202.         pulse = pu-8;
  203.         pulse = (pulse*2)+1;
  204.         outamp = (int)(512.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
  205.     }
  206.     else
  207.     {
  208.         pulse = pu-7;
  209.         pulse = (pulse*2)-1;
  210.         outamp = (int)(511.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
  211.     }
  212.     return outamp;
  213. }
  214.  
  215. //returns the difference between avg amplitude and intended
  216. //and sets the previous amplitude to the current amplitude
  217. //edit? make it return the address to the intended routine?
  218. uint8_t shq_get_best_amplitude(struct shq_lut_t *shq_lut, int curr_amp)
  219. {
  220.     uint8_t variants[6] = {variant_pmnw, variant_pnwm,
  221.     variant_mpnw, variant_mnwp, variant_nwpm, variant_nwmp};
  222.  
  223.     int prev_amp = shq_lut->prev_amp;
  224.     int prev_index = shq_lut->prev_index;
  225.     int outamps[6][shq_lut->repeats[curr_amp]][2]; //[trigger order][combination index][combination stage]
  226.     int avg_outamp[6][shq_lut->repeats[curr_amp]];
  227.     int p = 0;
  228.     uint8_t nw, m;
  229.     nw = m = 0;
  230.     int outamp_diff[6][shq_lut->repeats[curr_amp]];
  231.     int final_avg = 0;
  232.  
  233.     for(int j=0; j<6; j++)
  234.     {
  235.         for(int i=0; i<shq_lut->repeats[curr_amp]; i++)
  236.         {
  237.             if(j<2)
  238.             {    
  239.                 //pmnw and pnwm
  240.                 p = (int)shq_lut->pulse_lut[curr_amp][i];
  241.                 m = shq_lut->mv_lut[prev_amp][prev_index];
  242.                 nw = shq_lut->nw_lut[prev_amp][prev_index];
  243.             }
  244.             if(j>=2 && j<4)
  245.             {
  246.                 //mpnw and mnwp
  247.                 p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
  248.                 m = shq_lut->mv_lut[curr_amp][i];
  249.                 nw = shq_lut->nw_lut[prev_amp][prev_index];
  250.             }
  251.             if(j>=4)
  252.             {
  253.                 //nwpm and nwmp
  254.                 p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
  255.                 m = shq_lut->mv_lut[prev_amp][prev_index];
  256.                 nw = shq_lut->nw_lut[curr_amp][i];
  257.             }
  258.            
  259.             outamps[j][i][0] = shq_calculate_outamp(p, nw, m);
  260.        
  261.             if(j==0 || j==5)
  262.             {
  263.                 //pmnw and nwmp (master volume)
  264.                 m = shq_lut->mv_lut[curr_amp][i];
  265.                 //m = shq_lut->mv_lut[prev_amp][prev_index];
  266.             }
  267.             if(j==2 || j==4)
  268.             {
  269.                 //mpnw and nwpm (pulse)
  270.                 p = (int)shq_lut->pulse_lut[curr_amp][i];
  271.                 //p = (int)shq_lut->pulse_lut[prev_amp][prev_index];
  272.             }
  273.             if(j==1 || j==3)
  274.             {
  275.                 //pnwm and mnwp (noisewave)
  276.                 nw = shq_lut->nw_lut[curr_amp][i];
  277.                 //nw = shq_lut->nw_lut[prev_amp][prev_index];
  278.             }
  279.  
  280.             outamps[j][i][1] = shq_calculate_outamp(p, nw, m);
  281.  
  282.             avg_outamp[j][i] = outamps[j][i][0] + outamps[j][i][1];
  283.             //printf("FUCK\n");
  284.             final_avg = avg_outamp[j][i];
  285.            
  286.             if((final_avg>=(curr_amp*2) && final_avg<=(prev_amp*2)) || (final_avg<=(curr_amp*2) && final_avg>=(prev_amp*2)))
  287.             {
  288.                 shq_lut->prev_index = i;
  289.                 return variants[j];
  290.             }
  291.             if(final_avg>=(curr_amp*2))
  292.             {  
  293.                 outamp_diff[j][i] = (final_avg - (curr_amp*2));
  294.             }
  295.             else
  296.             {
  297.                 outamp_diff[j][i] = ((curr_amp*2) - final_avg);
  298.             }
  299.         }
  300.     }
  301.    
  302.     //printf("getting best amplitude\n");
  303.     shq_lut->prev_amp = curr_amp;
  304.     int best_diff = 1024;
  305.     int best_index = 0;
  306.     int best_order = 0;
  307.     if(shq_lut->repeats[curr_amp]>0)
  308.     {
  309.         for(int k=0; k<6; k++)
  310.         {  
  311.             for(int i=0; i<shq_lut->repeats[curr_amp]; i++)
  312.             {    
  313.                 for(int j=i; j<shq_lut->repeats[curr_amp]; j++)
  314.                 {    
  315.                     if((outamp_diff[k][i]<=outamp_diff[k][j]) && outamp_diff[k][i]<best_diff)
  316.                     {
  317.                         best_index = i;
  318.                         best_order = k;
  319.                         best_diff = outamp_diff[k][i];
  320.                     }
  321.                     else if((outamp_diff[k][j]<outamp_diff[k][i]) && outamp_diff[k][j]<best_diff)
  322.                     {  
  323.                         best_index = j;
  324.                         best_order = k;
  325.                         best_diff = outamp_diff[k][j];
  326.                     }
  327.                 }
  328.             }
  329.         }
  330.         shq_lut->prev_index = best_index;
  331.         printf("\rbest difference = %4d, best index = %d, best variant = %d", best_diff, best_index, best_order);
  332.         return variants[best_order]; //best_diff;
  333.     }
  334.     else
  335.     {
  336.         for(int k=0; k<6; k++)
  337.         {  
  338.             best_order = k;
  339.             if(outamp_diff[k][0]<best_diff)
  340.             {
  341.                 best_diff = outamp_diff[k][0];
  342.             }
  343.         }
  344.         shq_lut->prev_index = best_index;
  345.         printf("\rbest difference = %4d, best index = %d, best variant = %d", best_diff, best_index, best_order);
  346.         return variants[best_order]; //best_diff;
  347.     }
  348. }
  349.  
  350. void generate_shq_lut(struct shq_lut_t *shq_lut)
  351. {
  352.     init_shq_lut(shq_lut);
  353.    
  354.     int pulse = 0;
  355.     int noisewave = 0;
  356.     int uniqueamps = 0;
  357.     int outamp = 0;
  358.  
  359.     for(int nw=0; nw<3; nw++)
  360.     {
  361.         for(int m=0; m<8; m++)
  362.         {
  363.             for(int p=0; p<16; p++)
  364.             {
  365.                 if(nw==0)
  366.                 {
  367.                     noisewave = 0;
  368.                 }
  369.                 else if(nw==1)
  370.                 {
  371.                     noisewave = 15;
  372.                 }
  373.                 else
  374.                 {
  375.                     noisewave = -15;
  376.                 }
  377.                 if(p<8)
  378.                 {
  379.                     pulse = p-8;
  380.                     pulse = (pulse*2)+1;
  381.                     outamp = (int)(512.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
  382.                 }
  383.                 else
  384.                 {
  385.                     pulse = p-7;
  386.                     pulse = (pulse*2)-1;
  387.                     outamp = (int)(511.0+((512.0/240.0)*((double)((pulse+noisewave)*(m+1)))));
  388.                 }
  389.  
  390.                 if(shq_lut->amp_lut[outamp]==-1)
  391.                 {  
  392.                     uniqueamps++;
  393.                     shq_lut->pulse_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)p;
  394.                     shq_lut->mv_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)m;
  395.                     shq_lut->nw_lut[outamp][shq_lut->repeats[outamp]] = (((uint8_t)nw)<<2);
  396.                     shq_lut->amp_lut[outamp] = outamp;
  397.                 }
  398.                 else
  399.                 {
  400.                     shq_lut->repeats[outamp]++;
  401.                     shq_lut->pulse_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)p;
  402.                     shq_lut->mv_lut[outamp][shq_lut->repeats[outamp]] = (uint8_t)m;
  403.                     shq_lut->nw_lut[outamp][shq_lut->repeats[outamp]] = (((uint8_t)nw)<<2);
  404.                 }
  405.             }
  406.         }
  407.     }
  408.  
  409.     printf("no. of unique amplitudes (shq) = %d\n", uniqueamps);
  410.     int max_repeats = 0;
  411.     for(int i=0; i<1024; i++)
  412.     {
  413.         if(shq_lut->repeats[i]>max_repeats)
  414.         {
  415.             max_repeats = shq_lut->repeats[i];
  416.             shq_lut->max_index = i;
  417.         }
  418.     }
  419.     printf("no. of max combination repeats = %d\n", max_repeats);
  420.     shq_lut->max_repeats = max_repeats;
  421.  
  422.     uint8_t temp_mv, temp_pulse, temp_nw;
  423.     temp_pulse = 0;
  424.     temp_mv = 0;
  425.     temp_nw = 0;
  426.     int temp_amp = 0;
  427.  
  428.     for(int j=0; j<max_repeats; j++)
  429.     {
  430.     for(size_t i=0; i<512; i++)
  431.     {
  432.         if(shq_lut->amp_lut[i]!=-1)
  433.         {
  434.             temp_pulse = shq_lut->pulse_lut[i][j];
  435.             temp_mv = shq_lut->mv_lut[i][j];
  436.             temp_nw = shq_lut->nw_lut[i][j];
  437.             temp_amp = shq_lut->amp_lut[i];
  438.         }
  439.         else
  440.         {
  441.             shq_lut->pulse_lut[i][j] = temp_pulse;
  442.             shq_lut->mv_lut[i][j] = temp_mv;
  443.             shq_lut->nw_lut[i][j] = temp_nw;
  444.             shq_lut->amp_lut[i] = temp_amp;
  445.         }
  446.     }
  447.  
  448.     for(size_t i=511; i>0; i--)
  449.     {
  450.         if(shq_lut->amp_lut[i]!=-1)
  451.         {
  452.             temp_pulse = shq_lut->pulse_lut[i][j];
  453.             temp_mv = shq_lut->mv_lut[i][j];
  454.             temp_nw = shq_lut->nw_lut[i][j];
  455.             temp_amp = shq_lut->amp_lut[i];
  456.         }
  457.         else
  458.         {
  459.             shq_lut->pulse_lut[i][j] = temp_pulse;
  460.             shq_lut->mv_lut[i][j] = temp_mv;
  461.             shq_lut->nw_lut[i][j] = temp_nw;
  462.             shq_lut->amp_lut[i] = temp_amp;
  463.         }
  464.     }
  465.  
  466.     for(size_t i=1023; i>=512; i--)
  467.     {
  468.         if(shq_lut->amp_lut[i]!=-1)
  469.         {
  470.             temp_pulse = shq_lut->pulse_lut[i][j];
  471.             temp_mv = shq_lut->mv_lut[i][j];
  472.             temp_nw = shq_lut->nw_lut[i][j];
  473.             temp_amp = shq_lut->amp_lut[i];
  474.         }
  475.         else
  476.         {
  477.             shq_lut->pulse_lut[i][j] = temp_pulse;
  478.             shq_lut->mv_lut[i][j] = temp_mv;
  479.             shq_lut->nw_lut[i][j] = temp_nw;
  480.             shq_lut->amp_lut[i] = temp_amp;
  481.         }
  482.     }
  483.  
  484.     for(size_t i=512; i<1024; i++)
  485.     {
  486.         if(shq_lut->amp_lut[i]!=-1)
  487.         {
  488.             temp_pulse = shq_lut->pulse_lut[i][j];
  489.             temp_mv = shq_lut->mv_lut[i][j];
  490.             temp_nw = shq_lut->nw_lut[i][j];
  491.             temp_amp = shq_lut->amp_lut[i];
  492.         }
  493.         else
  494.         {
  495.             shq_lut->pulse_lut[i][j] = temp_pulse;
  496.             shq_lut->mv_lut[i][j] = temp_mv;
  497.             shq_lut->nw_lut[i][j] = temp_nw;
  498.             shq_lut->amp_lut[i] = temp_amp;
  499.         }
  500.     }
  501.     }
  502. }
  503.  
  504. int main(int argc, const char * argv[])
  505. {
  506.     if (argc != 5) {
  507.         printf("Usage: %s output_dir encoding_type aud_channels timer_div\n", argv[0]);
  508.         return -1;
  509.     }
  510.  
  511.     char output_dir[128];
  512.     printf("No. of channels: %d\n", atoi(argv[3]));
  513.     strcat(output_dir, "output/build");
  514.  
  515.     printf("%s\n", output_dir);
  516.     int is_ok = chdir(output_dir);
  517.     printf("%d\n", is_ok);
  518.  
  519.     int timer_div = (256 - atoi(argv[4]));
  520.     char tim_div[64];
  521.     sprintf(tim_div, "%d", timer_div);
  522.     strcat(tim_div, "\n");
  523.     char div_line[64];
  524.     strcpy(div_line, "ld a, ");
  525.     strcat(div_line, tim_div);
  526.  
  527.     FILE *div_val;
  528.     if((div_val = fopen("div.asm", "w"))<0)
  529.     {
  530.          printf("Error: couldn't create div.asm\n");
  531.          return 1;  
  532.     }
  533.     fputs(div_line, div_val);
  534.     fclose(div_val);
  535.  
  536.     is_ok = chdir("..");
  537.     is_ok = chdir(argv[1]);
  538.     printf("%d\n", is_ok);
  539.  
  540.     FILE *audiof = fopen("audio.raw", "rb"); //binary read mode (bytes)
  541.     if (!audiof) {
  542.         perror("Failed to load audio source file");
  543.         return 1;
  544.     }
  545.  
  546.     fseek(audiof, 0, SEEK_END);
  547.     size_t audio_size, debug_size;
  548.     int audio_mode = 0;
  549.     uint8_t aud_channels = (uint8_t)atoi(argv[3]);
  550.  
  551.     char *en_mode;
  552.  
  553.     if((en_mode = strstr(argv[2], "leg"))!=NULL)
  554.     {
  555.         audio_size = ftell(audiof)/2;
  556.         debug_size = ftell(audiof);
  557.         audio_mode = 0;
  558.     }
  559.     else if((en_mode = strstr(argv[2], "shq"))!=NULL)
  560.     {
  561.         if(aud_channels==2)
  562.         {    
  563.             audio_size = (size_t)(((long double)(ftell(audiof)*2))/3.0);
  564.         }
  565.         else
  566.         {
  567.             //audio_size = ftell(audiof)+1;
  568.             audio_size = (size_t)(((long double)(ftell(audiof)*3))/2.0);
  569.             printf("audio size = %lx\n", audio_size);
  570.         }
  571.         debug_size = ftell(audiof)+1;
  572.         audio_mode = 2;
  573.     }
  574.     else //if((en_mode = strstr(argv[2], "hq"))!=NULL)
  575.     {
  576.         audio_size = ftell(audiof);
  577.         debug_size = audio_size;
  578.         audio_mode = 1;
  579.     }
  580.    
  581.  
  582.     struct hq_lut_t *hq_lut = (struct hq_lut_t *)(malloc(sizeof(struct hq_lut_t)));
  583.     generate_hq_lut(hq_lut);
  584.  
  585.     struct shq_lut_t *shq_lut = (struct shq_lut_t *)(malloc(sizeof(struct shq_lut_t)));
  586.     generate_shq_lut(shq_lut);
  587.  
  588.     fseek(audiof, 0 , SEEK_SET);
  589.  
  590.     size_t main_pos = 0;
  591.     size_t debug_pos = 0;
  592.  
  593.     /*
  594.     static uint8_t *sawtooth;
  595.     sawtooth = (uint8_t *)malloc(65536*2);
  596.  
  597.     for(size_t i=0; i<65536*2; i+=2)
  598.     {
  599.         sawtooth[i] = (uint8_t)(((uint16_t)shq_lut.amp_lut[i>>7]<<6)&0xff);
  600.         sawtooth[i+1] = (uint8_t)(((uint16_t)shq_lut.amp_lut[i>>7]<<6)>>8)^0x80;
  601.     }
  602.  
  603.     FILE *sawtooth_test = fopen("sawtooth_test.raw", "wb");
  604.     fwrite(sawtooth, 1, (65536*2), sawtooth_test);
  605.     fclose(sawtooth_test);
  606.     free(sawtooth);
  607.     */
  608.    
  609.     static uint8_t *output;
  610.     output = (uint8_t *)malloc(audio_size);
  611.     memset(output, 0, audio_size);
  612.  
  613.     static uint8_t *debug;
  614.     debug = (uint8_t *)malloc(debug_size);
  615.     memset(debug, 0, debug_size);
  616.  
  617.     bool done = false;
  618.  
  619.     if(audio_mode==0)
  620.     {
  621.         while(!done)
  622.         {
  623.             uint8_t left, right;
  624.             if(fread(&left, 1, 1, audiof) != 1 || fread(&right, 1, 1, audiof) != 1)
  625.             {
  626.                 left = right = 0x80;
  627.                 done = true;
  628.             }
  629.             output[main_pos++] = (left&0xf0)|((right&0xf0)>>4);
  630.             debug[debug_pos++] = (left&0xf0);
  631.             debug[debug_pos++] = (right&0xf0);
  632.         }
  633.     }
  634.     if(audio_mode==1)
  635.     {
  636.         if(aud_channels==2)
  637.         {  
  638.             while(!done)
  639.             {
  640.                 uint8_t left, right;
  641.                 if(fread(&left, 1, 1, audiof) != 1 || fread(&right, 1, 1, audiof) != 1)
  642.                 {
  643.                     left = right = 0x80;
  644.                     done = true;
  645.                 }
  646.  
  647.                 output[main_pos++] = (((hq_lut->pulse_lut[left]&0x0f)<<4)|(hq_lut->pulse_lut[right]&0x0f));
  648.                 output[main_pos++] = (((hq_lut->mv_lut[left]&0x07)<<4)|(hq_lut->mv_lut[right]&0x07));
  649.                
  650.                 debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[left];
  651.                 debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[right];
  652.             }
  653.         }
  654.         if(aud_channels==1)
  655.         {
  656.             while(!done)
  657.             {  
  658.                 uint8_t samp;
  659.                 if(fread(&samp, 1, 1, audiof) != 1)
  660.                 {
  661.                     samp = 0x80;
  662.                     done = true;
  663.                 }
  664.                 output[main_pos++] = (((hq_lut->pulse_lut[samp]&0x0f)<<4)|(hq_lut->mv_lut[samp]&0x07));
  665.                 debug[debug_pos++] = (uint8_t)hq_lut->amp_lut[samp];
  666.             }
  667.         }
  668.     }
  669.     if(audio_mode==2)
  670.     {
  671.         if(aud_channels==2)
  672.         {  
  673.             /*
  674.             while(!done)
  675.             {
  676.                 uint8_t left[2], right[2];
  677.                 if(fread(left, 1, 2, audiof) != 1 || fread(right, 1, 2, audiof) != 1)
  678.                 {
  679.                     left[0] = right[0] = 0;
  680.                     left[1] = right[1] = 0;
  681.                     done = true;
  682.                 }
  683.  
  684.                 // TODO: amp lut is signed long, convert it to uint16_t before proceeding
  685.                 uint16_t left_16 = (((uint16_t)left[1]^0x80)<<8)|(uint16_t)left[0];
  686.                 uint16_t right_16 = (((uint16_t)right[1]^0x80)<<8)|(uint16_t)right[0];
  687.                
  688.                 output[main_pos++] = ((shq_lut.pulse_lut[left_16]<<4)|shq_lut.pulse_lut[right_16]);
  689.                 output[main_pos++] = ((shq_lut.mv_lut[left_16]<<4)|shq_lut.mv_lut[right_16]);
  690.                 output[main_pos++] = ((shq_lut.nw_lut[left_16]<<4)|shq_lut.nw_lut[right_16]);
  691.  
  692.                 uint8_t left_out[2], right_out[2];
  693.                 left_out[0] = (uint8_t)(shq_lut.amp_lut[left_16]&0xff);
  694.                 left_out[1] = (uint8_t)((shq_lut.amp_lut[left_16]>>8)^0x80);
  695.                 right_out[0] = (uint8_t)(shq_lut.amp_lut[right_16]&0xff);
  696.                 right_out[1] = (uint8_t)((shq_lut.amp_lut[right_16]>>8)^0x80);
  697.  
  698.                 debug[debug_pos++] = left_out[0];
  699.                 debug[debug_pos++] = left_out[1];
  700.                 debug[debug_pos++] = right_out[0];
  701.                 debug[debug_pos++] = right_out[1];
  702.             }
  703.             */
  704.         }
  705.         if(aud_channels==1)
  706.         {
  707.             //printf("shq encoder\n");
  708.             /*
  709.             uint16_t samp;
  710.             uint8_t *samp_read = (uint8_t *)&samp;
  711.             int ok = fread(samp_read, 1, 2, audiof);
  712.             printf("%d\n", ok);
  713.  
  714.             samp = (samp^0x8000)>>6;
  715.             */
  716.  
  717.             shq_lut->prev_amp = shq_lut->max_index;
  718.             shq_lut->prev_index = shq_lut->max_repeats;
  719.  
  720.             /*
  721.             output[main_pos++] = ((shq_lut.pulse_lut[samp][index]<<4)|(shq_lut.mv_lut[samp][index]&0x07));
  722.             output[main_pos++] = ((shq_lut.nw_lut[samp][index]<<4)|shq_lut.nw_lut[samp][index]);
  723.  
  724.             short int conv_samp = shq_lut.amp_lut[samp];
  725.             uint16_t final_conv = * ((uint16_t *) &conv_samp);
  726.                
  727.             uint8_t samp_hi, samp_lo;
  728.             samp_hi = (uint8_t)((final_conv>>2)^0x80);
  729.             samp_lo = (uint8_t)((final_conv<<6)&0xc0);
  730.  
  731.             debug[debug_pos++] = samp_lo;
  732.             debug[debug_pos++] = samp_hi;
  733.             */
  734.  
  735.             while(!done && debug_pos<debug_size)
  736.             {  
  737.                 uint16_t samp;
  738.                 uint8_t *samp_read = (uint8_t *)&samp;
  739.                 if(fread(samp_read, 1, 2, audiof) != 1)
  740.                 {
  741.                     //samp = 0;
  742.                     //done = true;
  743.                 }
  744.  
  745.                 samp = (samp^0x8000)>>6;
  746.  
  747.                 int index;
  748.                 //int diff;
  749.                 //diff = shq_get_best_amplitude(&shq_lut, samp);
  750.                 uint8_t jump_addr;
  751.                 jump_addr = shq_get_best_amplitude(shq_lut, samp);
  752.                 index = shq_lut->prev_index;
  753.                 //printf(", encoding byte %lx\n", main_pos);
  754.  
  755.                 output[main_pos++] = ((shq_lut->pulse_lut[samp][index]<<4)|(shq_lut->mv_lut[samp][index]&0x07));
  756.                 output[main_pos++] = ((shq_lut->nw_lut[samp][index]<<4)|shq_lut->nw_lut[samp][index]);
  757.                 output[main_pos++] = jump_addr;
  758.  
  759.                 short int conv_samp = shq_lut->amp_lut[samp];
  760.                 uint16_t final_conv = * ((uint16_t *) &conv_samp);
  761.                
  762.                 uint8_t samp_hi, samp_lo;
  763.                 samp_hi = (uint8_t)((final_conv>>2)^0x80);
  764.                 samp_lo = (uint8_t)((final_conv<<6)&0xc0);
  765.  
  766.                 debug[debug_pos++] = samp_lo;
  767.                 debug[debug_pos++] = samp_hi;
  768.                
  769.                 if((main_pos&0x3fff)==16383)
  770.                 {
  771.                     printf("changing banks, address = %16lu\n", main_pos);
  772.                     main_pos++;
  773.                 }
  774.             }
  775.         }
  776.     }
  777.     free(hq_lut);
  778.     free(shq_lut);
  779.     fclose(audiof);
  780.     FILE *out_aud = fopen("audio_encoded.raw", "wb");
  781.     if(!out_aud)
  782.     {
  783.         perror("Error: cannot write to output audio file\n");
  784.         return 1;
  785.     }
  786.     fwrite(output, 1, audio_size, out_aud);
  787.     fclose(out_aud);
  788.     free(output);
  789.  
  790.     FILE *debug_aud = fopen("debug_audio.raw", "wb");
  791.     fwrite(debug, 1, debug_size, debug_aud);
  792.     fclose(debug_aud);
  793.  
  794.     free(debug);
  795.     printf("Audio has been encoded!\n");
  796. }
Add Comment
Please, Sign In to add comment