home *** CD-ROM | disk | FTP | other *** search
/ Fujiology Archive / fujiology_archive_v1_0.iso / !FALCON / NOCREW / MP2_0997.ZIP / mp2_0997 / src / replay.c < prev    next >
C/C++ Source or Header  |  1999-09-05  |  5KB  |  238 lines

  1. #include <tos.h>
  2. #include <ext.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include "snddefs.h"
  8. #include "decoder.h"
  9. #include "stream.h"
  10. #include "matrix.h"
  11. #include "clock.h"
  12.  
  13. /* global variables from mp2audio.c */
  14. extern int replay;
  15.  
  16. #define MUTE         1
  17. #define MUTE_RESTORE 0
  18.  
  19. /* If tos.h defines the wrong names!! */
  20. #ifndef setsndmode
  21. #define Vsetmode setmode
  22. #define setmode setsndmode
  23. #endif
  24.  
  25. /* External clock values */
  26. long ext0, ext1;
  27.  
  28. static void mute(int mode)
  29. {
  30.     static int ltatten, rtatten, ltgain, rtgain;
  31.  
  32.     if(mode) { /* mode==MUTE */
  33.         /* Save old settings */
  34.         ltatten = (int) soundcmd(LTATTEN, -1);
  35.         rtatten = (int) soundcmd(RTATTEN, -1);
  36.         ltgain = (int) soundcmd(LTGAIN, -1);
  37.         rtgain = (int) soundcmd(RTGAIN, -1);
  38.         soundcmd(LTATTEN, 0x00f0);
  39.         soundcmd(RTATTEN, 0x00f0);
  40.         soundcmd(RTGAIN, 0x0000);
  41.         soundcmd(LTGAIN, 0x0000);
  42.     } else { /* mode==MUTE_RESTORE */
  43.         /* Restore old settings */
  44.         soundcmd(LTATTEN, ltatten);
  45.         soundcmd(RTATTEN, rtatten);
  46.         soundcmd(LTGAIN, ltgain);
  47.         soundcmd(RTGAIN, rtgain);
  48.     }
  49. }
  50.  
  51. int external_clock(void)
  52. {
  53.     static int external_clock_enabled = -1;
  54.     
  55.     if(external_clock_enabled >= 0)
  56.         return external_clock_enabled;
  57.  
  58. #if 1
  59.     bufs = (long)Mxalloc(TEST_BUFSIZE, 0);
  60.     if(!bufs) {
  61.         ext0 = ext1 = 0;
  62.         return 0;
  63.     }
  64.     bufe = bufs + TEST_BUFSIZE;
  65.         
  66.     Supexec(test_ext_clk);
  67.         
  68.     Mfree((void *)bufs);
  69. #else
  70. /*    clkprobe(); */
  71. #endif
  72.  
  73.     switch(((int *)&type)[0]) {
  74.     case 1:
  75.         ext0 = 44100L;
  76.         break;
  77.     case 2:
  78.         ext0 = 48000L;
  79.         break;
  80.     default:
  81.         ext0 = 0;
  82.     }
  83.     switch(((int *)&type)[1]) {
  84.     case 1:
  85.         ext1 = 44100L;
  86.         break;
  87.     case 2:
  88.         ext1 = 48000L;
  89.         break;
  90.     default:
  91.         ext1 = 0;
  92.     }
  93.  
  94.     external_clock_enabled = ext0 || ext1;
  95.  
  96.     return external_clock_enabled;
  97. }
  98.  
  99. static void set_matrix(long frequency)
  100. {
  101.     sndstatus(SND_RESET);
  102.     setmode(STEREO16);
  103.     soundcmd(ADDERIN,MATIN);
  104.     dsptristate(ENABLE,ENABLE);
  105.     setbuffer(SR_PLAY, stream_get_buffer(), stream_get_buffer_end());
  106.  
  107.     switch((int)(frequency/10)) {
  108.     case 1600:
  109.         if(ext0 == 48000L) {
  110.             gpio(2, 0); /* Use clock 0 */
  111.             Supexec(set_matrix_ext_16000);
  112.         } else if(ext1 == 48000L) {
  113.             gpio(2, 1); /* Use clock 1 */
  114.             Supexec(set_matrix_ext_16000);
  115.         } else
  116.             Supexec(set_matrix_int_16390);
  117.         break;
  118.     case 2205:
  119.         if(ext0 == 44100L) {
  120.             gpio(2, 0); /* Use clock 0 */
  121.             Supexec(set_matrix_ext_half);
  122.         } else if(ext1 == 44100L) {
  123.             gpio(2, 1); /* Use clock 1 */
  124.             Supexec(set_matrix_ext_half);
  125.         } else
  126.             Supexec(set_matrix_int_24585);
  127.         break;
  128.     case 2400:
  129.         if(ext0 == 48000L) {
  130.             gpio(2, 0); /* Use clock 0 */
  131.             Supexec(set_matrix_ext_half);
  132.         } else if(ext1 == 48000L) {
  133.             gpio(2, 1); /* Use clock 1 */
  134.             Supexec(set_matrix_ext_half);
  135.         } else
  136.             Supexec(set_matrix_int_24585);
  137.         break;
  138.     case 3200:
  139.         if(ext0 == 48000L) {
  140.             gpio(2, 0); /* Use clock 0 */
  141.             Supexec(set_matrix_ext_32000);
  142.         } else if(ext1 == 48000L) {
  143.             gpio(2, 1); /* Use clock 1 */
  144.             Supexec(set_matrix_ext_32000);
  145.         } else
  146.             Supexec(set_matrix_int_32780);
  147.         break;
  148.     case 4410:
  149.         if(ext0 == 44100L) {
  150.             gpio(2, 0); /* Use clock 0 */
  151.             Supexec(set_matrix_ext_full);
  152.         } else if(ext1 == 44100L) {
  153.             gpio(2, 1); /* Use clock 1 */
  154.             Supexec(set_matrix_ext_full);
  155.         } else
  156.             Supexec(set_matrix_int_49170);
  157.  
  158. /* SPDIF test */
  159. #if 0
  160.             {
  161.             int recmo;
  162.             recmo=0; /* Record/44.1KHz */
  163.             gpio(2,recmo+4); /* Reset */
  164.             delay(50);
  165.             gpio(2,recmo); /* Unreset */
  166.             }
  167. #endif
  168. /* end SPDIF test */
  169.  
  170.         break;
  171.     case 4800:
  172.         if(ext0 == 48000L) {
  173.             gpio(2, 0); /* Use clock 0 */
  174.             Supexec(set_matrix_ext_full);
  175.         } else if(ext1 == 48000L) {
  176.             gpio(2, 1); /* Use clock 1 */
  177.             Supexec(set_matrix_ext_full);
  178.         } else
  179.             Supexec(set_matrix_int_49170);
  180.                 
  181.         break;
  182.     }
  183. }
  184.  
  185. void replay_continue(void)
  186. {
  187.     Dsp_Hf0(0);        /* Tell DSP to play again. */
  188.     replay = 1;
  189. }
  190.  
  191. void replay_init(long frequency)
  192. {
  193.     /* Save all old audio settings */
  194.     Supexec(saveaudio);
  195.  
  196.     mute(MUTE);
  197.     set_matrix(frequency);
  198.     mute(MUTE_RESTORE);
  199.     
  200. /*    do decoder_load(); while(!decoder_dma_acknowledge()); */
  201.     
  202.     decoder_load();
  203.     stream_load(1);
  204.  
  205.     if(frequency == 44100L && !external_clock()) {
  206.         /* Enable frequency conversion. */
  207.         /* Still not reimplemented.. sigh.. */
  208.     }
  209.  
  210.     buffoper(SB_PLA_ENA | SB_PLA_RPT);
  211.     replay = 1;
  212. }
  213.  
  214. void replay_pause(void)
  215. {
  216.     Dsp_Hf0(1);        /* Tell DSP to pause. */
  217.     replay = 0;
  218. }
  219.  
  220. int replay_pausep(void)
  221. {
  222.     return Dsp_Hf0(-1);
  223. }
  224.  
  225. int replayp(void)
  226. {
  227.     return replay;
  228. }
  229.  
  230. void replay_stop(void)
  231. {
  232.     /* Restore all old audio settings */
  233.     Supexec(restoreaudio);
  234.     
  235.     stream_reset();
  236.     replay = 0;
  237. }
  238.