home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 119 / af119sub.adf / SamEd.lzx / SamEd / Developers / 8SVX.filer.c next >
C/C++ Source or Header  |  2001-04-09  |  10KB  |  289 lines

  1. #include <exec/exec.h>
  2. #include <dos/dos.h>
  3. #include <libraries/iffparse.h>
  4. #include <iffp/8svx.h>
  5. #include "extproc.h"
  6.  
  7. #define ID_VHDR MAKE_ID('V','H','D','R')
  8. #define ID_8SVX MAKE_ID('8','S','V','X')
  9. #define ID_BODY MAKE_ID('B','O','D','Y')
  10. #define ID_CHAN MAKE_ID('C','H','A','N')
  11.  
  12. #define LOAD    1
  13. #define SAVE    2
  14.  
  15. BOOL Load_Sample(VOID);
  16. BOOL Save_Sample(VOID);
  17. void Make_Stereo(void);
  18. BYTE * Make_Separate(void);
  19.  
  20. struct sam_info *sample;
  21. char *filename;
  22. ULONG action;
  23.  
  24. /*typedef struct {
  25.     ULONG oneShotHiSamples,     / * # samples in the high octave 1-shot part * /
  26.           repeatHiSamples,      / * # samples in the high octave repeat part * /
  27.           samplesPerHiCycle;    / * # samples/cycle in high octave, else 0 * /
  28.     UWORD samplesPerSec;        / * data sampling rate * /
  29.     UBYTE ctOctave,             / * # of octaves of waveforms * /
  30.           sCompression;         / * data compression technique used * /
  31.     Fixed volume;               / * playback nominal volume from 0 to Unity
  32.                                  * (full volume). Map this value into
  33.                                  * the output hardware's dynamic range.
  34.                                  * /
  35.     } Voice8Header;*/
  36.  
  37. ULONG main(int argc, char *argv[])
  38. {
  39.     if (argc != 5) return(0);
  40.  
  41.     if (atol(argv[1]) != FILER_MAGIC) return(FERR_NOTMAGIC);
  42.  
  43.     action = atol(argv[2]);
  44.     sample = (struct sam_info *)atol(argv[3]);
  45.     filename = (char *) argv[4];
  46.  
  47.     printf("%d\t%d\t%s\n", action, sample, filename);
  48.  
  49.     if (action == FACT_LOAD)
  50.     {
  51.         if (Load_Sample())
  52.         {
  53.             printf("waveform... %ld\tlength... %ld\tfreq... %ld\tvolume... %ld\n",sample->waveform,sample->length,sample->def_freq,sample->def_vol);
  54.             printf("Loaded ... Exiting from IFF filer\n");
  55.             return (FERR_NOERR);
  56.         }
  57.         else return (FERR_GENERAL);
  58.     }
  59.  
  60.     if (action == FACT_SAVE)
  61.     {
  62.         if (Save_Sample())
  63.         {
  64.             printf("Saved ... Exiting from IFF filer\n");
  65.             return (FERR_NOERR);
  66.         }
  67.         else return (FERR_GENERAL);
  68.     }
  69. }
  70.  
  71. BOOL Load_Sample(VOID)
  72. {
  73.     ULONG n=0;
  74.     struct StoredProperty *vhdrsp, *bodysp, *chansp;
  75.     Voice8Header *data;
  76.     BOOL loaded = FALSE;
  77.     BPTR file;
  78.     APTR newmem;
  79.  
  80.     /*find the requested file*/
  81.     if (file = Open(filename,MODE_OLDFILE))
  82.     {
  83.         printf("Finding IFF FORM... ");
  84.         struct IFFHandle *iff;
  85.         if (iff = (struct IFFHandle *)AllocIFF())
  86.         {
  87.             iff->iff_Stream = file;
  88.             InitIFFasDOS (iff);
  89.             if (!OpenIFF (iff, IFFF_READ))
  90.             {
  91.                 PropChunk (iff, ID_8SVX, ID_VHDR);
  92.                 PropChunk (iff, ID_8SVX, ID_BODY);
  93.                 PropChunk (iff, ID_8SVX, ID_CHAN);
  94.                 StopOnExit (iff, ID_8SVX, ID_FORM);
  95.                 ParseIFF (iff, IFFPARSE_SCAN);
  96.  
  97.                 if ((vhdrsp = FindProp (iff, ID_8SVX, ID_VHDR)) && (bodysp = FindProp (iff, ID_8SVX, ID_BODY)))
  98.                 {
  99.                     if (bodysp->sp_Size)
  100.                     {
  101.                         printf("Found VHDR, and BODY ");
  102.                         data = vhdrsp->sp_Data;
  103.                         if (data->sCompression == sCmpNone)
  104.                         {
  105.                             if (newmem = AllocMem(bodysp->sp_Size, MEMF_PUBLIC));
  106.                             {
  107.                                 if (sample->length) /*Free old waveform*/
  108.                                 {
  109.                                     FreeMem(sample->waveform,sample->length);
  110.                                     printf("FreeMem oldwaveform\t%d bytes at %d\n",sample->length,sample->waveform);
  111.                                 }
  112.  
  113.                                 sample->length = bodysp->sp_Size;
  114.                                 sample->def_freq = data->samplesPerSec;
  115.                                 sample->def_vol = data->volume;
  116.                                 sample->waveform = (BYTE *)newmem;
  117.                                 sample->type = STYPE_M8;
  118.                                 if (data->repeatHiSamples)
  119.                                 {
  120.                                     sample->loop = TRUE;
  121.                                     sample->loop_begin = data->oneShotHiSamples;
  122.                                     sample->loop_length = data->repeatHiSamples;
  123.                                 }
  124.                                 else
  125.                                 {
  126.                                     sample->loop = FALSE;
  127.                                     sample->loop_begin = 0;
  128.                                     sample->loop_length = 0;
  129.                                 }
  130.                                 if (chansp = FindProp (iff, ID_8SVX, ID_CHAN))
  131.                                 {
  132.                                     ULONG *chanval = chansp->sp_Data;
  133.                                     printf("\nCHAN... %d\n",*chanval);
  134.                                     if (*chanval == 6) /*stereo sample*/
  135.                                         sample->type = STYPE_S8;
  136.                                 }
  137.  
  138.                                 CopyMem (bodysp->sp_Data, sample->waveform, sample->length);
  139.  
  140.                                 printf("Repeat start = %d ",data->oneShotHiSamples);
  141.                                 printf("Repeat length = %d\n",data->repeatHiSamples);
  142.                                 loaded = TRUE;
  143.                             }
  144.                         }
  145.                     }
  146.                 }
  147.                 CloseIFF (iff);
  148.             }
  149.             FreeIFF (iff);
  150.         }
  151.         Close(file);
  152.     }
  153.     if (sample->type == STYPE_S8) Make_Stereo();
  154.     return(loaded);
  155. }
  156.  
  157. void Make_Stereo(void)
  158. {
  159.     if (!sample->length) return();
  160.  
  161.     BYTE *oldwave = sample->waveform;
  162.  
  163.     if (sample->waveform = AllocMem(sample->length, MEMF_PUBLIC | MEMF_CLEAR))
  164.     {
  165.         printf("Making stereo...\n");
  166.         BYTE *dest = sample->waveform +1;
  167.         for (BYTE *src = oldwave; dest < (sample->waveform + sample->length); *dest = *src, dest +=2, src++);
  168.         dest = sample->waveform;
  169.         for (BYTE *src = oldwave +(sample->length /2); dest < (sample->waveform + sample->length); *dest = *src, dest +=2, src++);
  170.         FreeMem(oldwave, sample->length);
  171.     }
  172.     else
  173.     {
  174.         sample->waveform = oldwave;
  175.     }
  176. }
  177.  
  178. BYTE * Make_Separate(void)
  179. {
  180.     if (!sample->length) return(NULL);
  181.  
  182.     BYTE *savebuf = NULL;
  183.  
  184.     if (savebuf = AllocMem(sample->length, MEMF_PUBLIC | MEMF_CLEAR))
  185.     {
  186.         printf("Separating stereo...\n");
  187.         BYTE *dest = savebuf;
  188.         for (BYTE *src = sample->waveform +1; src < (sample->waveform + sample->length); *dest = *src, dest++, src+=2);
  189.         dest = savebuf + (sample->length /2);
  190.         for (BYTE *src = sample->waveform; src < (sample->waveform + sample->length); *dest = *src, dest ++, src+=2);
  191.     }
  192.     return(savebuf);
  193. }
  194.  
  195. BOOL Save_Sample(VOID)
  196. {
  197.     ULONG n=0;
  198.     BYTE *savebuf = NULL, *buf;
  199.     Voice8Header data;
  200.     BOOL saved = FALSE;
  201.     BPTR file;
  202.  
  203.     if ((sample->type != STYPE_M8) && (sample->type != STYPE_S8)) return(FALSE);
  204.  
  205.     data.oneShotHiSamples = sample->loop_begin;
  206.     data.repeatHiSamples = sample->loop_length;
  207.     data.samplesPerHiCycle;
  208.     data.samplesPerSec = sample->def_freq;
  209.     data.ctOctave = 1;
  210.     data.sCompression =0;
  211.     data.volume = sample->def_vol;
  212.  
  213.     /*find the requested file*/
  214.     if (file = Open(filename,MODE_NEWFILE))
  215.     {
  216.         struct IFFHandle *iff;
  217.         if (iff = (struct IFFHandle *)AllocIFF())
  218.         {
  219.             iff->iff_Stream = file;
  220.             InitIFFasDOS (iff);
  221.             if (!OpenIFF (iff, IFFF_WRITE))
  222.             {
  223.                 printf("Opened iffparse for writes... ");
  224.                 if (!PushChunk (iff, ID_8SVX, ID_FORM, IFFSIZE_UNKNOWN))
  225.                 {
  226.                     printf("Creating FORM, ");
  227.                     if (!PushChunk (iff, ID_8SVX, ID_VHDR, sizeof (Voice8Header)))
  228.                     {
  229.                         printf("VHDR, ");
  230.                         if (WriteChunkBytes (iff, &data, sizeof (Voice8Header)))
  231.                         {
  232.                             printf("writing VHDR... ");
  233.                             if (!PopChunk (iff))
  234.                             {
  235.                                 printf("written, ");
  236.                                 saved = TRUE;
  237.                             }
  238.                         }
  239.                     }
  240.  
  241.                     if (sample->type == STYPE_S8)
  242.                     {
  243.                         savebuf = Make_Separate();
  244.                         ULONG type = 6;
  245.  
  246.                         if (!PushChunk (iff, ID_8SVX, ID_CHAN, 4))
  247.                         {
  248.                             printf("CHAN, ");
  249.                             if (!WriteChunkBytes (iff, &type, 4))
  250.                                 saved = FALSE;
  251.                             else
  252.                             {
  253.                                 printf("writing CHAN. ");
  254.                                 if (PopChunk (iff))
  255.                                     saved = FALSE;
  256.                             }
  257.                         }
  258.                         else
  259.                             saved = FALSE;
  260.                     }
  261.  
  262.                     if (saved && !PushChunk (iff, ID_8SVX, ID_BODY, sample->length))
  263.                     {
  264.                         printf("BODY, ");
  265.                         if (savebuf) buf = savebuf; else buf = sample->waveform;
  266.                         if (!WriteChunkBytes (iff, buf, sample->length))
  267.                             saved = FALSE;
  268.                         else
  269.                         {
  270.                             printf("writing BODY. ");
  271.                             if (PopChunk (iff))
  272.                                 saved = FALSE;
  273.                         }
  274.                     }
  275.                     else
  276.                         saved = FALSE;
  277.  
  278.                     if (savebuf) FreeMem(savebuf, sample->length);
  279.                 }
  280.                 CloseIFF (iff);
  281.             }
  282.             FreeIFF (iff);
  283.         }
  284.         Close(file);
  285.     }
  286.     printf("\n");
  287.     return(saved);
  288. }
  289.