home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 3 / CD_Magazyn_EXEC_nr_3.iso / Recent / mus / play / DelfSF.lha / DelfSF / src / delf.c < prev    next >
C/C++ Source or Header  |  2000-07-19  |  5KB  |  160 lines

  1. /*****************************************************************************
  2.  
  3.     DelfSF - sndfile player for Delfina DSP
  4.     Copyright (C) 2000  Michael Henke
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. *****************************************************************************/
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <proto/exec.h>
  26. #include <proto/dos.h>
  27. #include <exec/interrupts.h>
  28. #include <libraries/delfina.h>
  29.  
  30.  
  31.  
  32. /* we need these variables from main.c */
  33. struct loadbuf {
  34.     struct loadbuf *next;
  35.     short *data;
  36. };
  37. extern struct loadbuf *buf_play;
  38. extern long buffers_filled, buffers_played, pause;
  39.  
  40. /* number of samples per buffer */
  41. /* note: must be equal to BUFFER_SAMPLES in main.c !!! */
  42. #define DELFINA_SAMPLES     2048
  43.  
  44.  
  45.  
  46.  
  47. extern struct DelfObj DSP56K_PCM;
  48. /** offsets in PROGRAM section **/
  49. #define PROG_PCM_INIT       0
  50. #define PROG_PCM_INTKEY     0
  51. #define PROG_PCM_MUTE       2
  52. #define PROG_PCM_MODULE     4
  53. /** offsets in DATA sections (X/Y/L memory) **/
  54. #define DATY_PCM_BUFPTR     0
  55. /** additional DSP-memory allocations (internal/external, P/X/Y/L memory) **/
  56. /* none */
  57.  
  58.  
  59. struct Library *DelfinaBase=NULL;
  60. static struct Interrupt delfint={0};
  61. static struct DelfPrg *prg_pcm=NULL;
  62. static struct DelfModule *mod_pcm=NULL;
  63. static ULONG key=0, delfmemtype, delfcopysize;
  64. static LONG trigger_exit;
  65.  
  66.  
  67.  
  68.  
  69. static void __saveds IntServer(void)
  70. {
  71.     if(pause)
  72.     {
  73.         trigger_exit--;
  74.         Delf_Run(prg_pcm->prog+PROG_PCM_MUTE, 0, DRUNF_ASYNCH, 0,0,0,0);
  75.     }
  76.     else
  77.     {
  78.         if(buffers_filled>0)
  79.         {
  80.             Delf_CopyMem( buf_play->data,
  81.                           (void*)Delf_Peek(prg_pcm->ydata+DATY_PCM_BUFPTR,DMEMF_YDATA),
  82.                           delfcopysize,
  83.                           DCPF_FROM_AMY|DCPF_16BITH|delfmemtype );
  84.             buffers_filled--;
  85.             buffers_played++;
  86.             buf_play=buf_play->next;
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92.  
  93.  
  94. int StartDelfina(unsigned int rate, unsigned int channels, LONG volume)
  95. {
  96.     LONG j, p;
  97.     if(!(DelfinaBase=OpenLibrary("delfina.library",4)))
  98.     {
  99.         printf("**unable to open delfina.library v4\n");
  100.         return(0);
  101.     }
  102.     if(!(prg_pcm=Delf_AddPrg(&DSP56K_PCM)))
  103.     {
  104.         printf("**not enough Delfina memory\n");
  105.         return(0);
  106.     }
  107.     Delf_Run(prg_pcm->prog+PROG_PCM_INIT, 0, 0,
  108.             (ULONG)((channels==1)?1:0),
  109.             (volume>100) ? (volume*0x7fffff)/200 : (volume*0x400000)/100,
  110.             (ULONG)rate, 0 );
  111.     delfmemtype=(channels==1)?DMEMF_XDATA:DMEMF_LDATA;
  112.     delfcopysize=(channels==1)?DELFINA_SAMPLES*2:DELFINA_SAMPLES*4;
  113.     p=Delf_Peek(prg_pcm->ydata+DATY_PCM_BUFPTR,DMEMF_YDATA);
  114.     j=2;
  115.     while((buffers_filled>0) && (j-- >0))
  116.     {
  117.         Delf_CopyMem( buf_play->data,
  118.                       (void*)p,
  119.                       delfcopysize,
  120.                       DCPF_FROM_AMY|DCPF_16BITH|delfmemtype );
  121.         buffers_filled--;
  122.         p+=DELFINA_SAMPLES;
  123.         buf_play=buf_play->next;
  124.     }
  125.  
  126.     delfint.is_Code=(void(*)(void))IntServer;
  127.     if(!(key=Delf_AddIntServer(prg_pcm->prog+PROG_PCM_INTKEY,&delfint)))
  128.     {
  129.         printf("**couldn't create interrupt server\n");
  130.         return(0);
  131.     }
  132.     if(!(mod_pcm=Delf_AddModule(DM_Inputs, 0,
  133.                                 DM_Outputs, 1,
  134.                                 DM_Code, prg_pcm->prog+PROG_PCM_MODULE,
  135.                                 DM_Freq, 48000, /* always play at max. rate */
  136.                                 DM_Name, "DelfSF", 0)))
  137.     {
  138.         printf("**couldn't create DelfModule\n");
  139.         return(0);
  140.     }
  141.     return(1);  /* ok */
  142. }
  143.  
  144.  
  145.  
  146. void StopDelfina(void)
  147. {
  148.     if(mod_pcm)
  149.     {
  150.         pause++; trigger_exit=1;
  151.         while(trigger_exit>0) Delay(1); /* wait until playback has finished */
  152.         pause--;
  153.         Delf_RemModule(mod_pcm); mod_pcm=NULL;
  154.     }
  155.     if(key) {Delf_RemIntServer(key); key=0;}
  156.     if(prg_pcm) {Delf_RemPrg(prg_pcm); prg_pcm=NULL;}
  157.     if(DelfinaBase) {CloseLibrary(DelfinaBase); DelfinaBase=NULL;}
  158. }
  159.  
  160.