home *** CD-ROM | disk | FTP | other *** search
/ Dave Lowe: S5 Stereo Sound Sampler / Lowe_S5StereoSoundSampler.adf / Progs / C / lp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-26  |  4.5 KB  |  188 lines

  1. /*
  2.  
  3. lp.c PAR Aug 87 Sophus Software
  4.  
  5. A long sample player and recorder.
  6. Facilities from recording , playing , saving and loading samples using
  7. the FAST memory on an expanded Amiga.
  8.  
  9. Avoids Intuition etc to make the example easier to read and smaller
  10. at run-time.  
  11.  
  12. */
  13.          
  14. #include   "lattice/stdio.h"
  15. #include   "exec/types.h"
  16. #include   "s5.h"
  17.  
  18. struct  Frag   *Left,*Right;
  19. int     LeftSize,RightSize;
  20. APTR    S5Base; /* any pointer would do */
  21.  
  22. main()
  23. {
  24. int  ok;
  25. char  c,newline;
  26.  
  27. printf("lp : Record/play long samples \n");
  28. printf("Note: files are loaded/saved in SOP3 format. i.e. no format\n");
  29. printf("Enter option letter followed by RETURN\n\n");
  30.  
  31. if((S5Base=(APTR)OpenLibrary(S5NAME,0))==0)
  32.       TidyUp("Failed to open S5.library");
  33.  
  34. LeftSize = 10;
  35. RightSize = 10;
  36. Left = (struct Frag *)S5AllocateFrag(LeftSize,1); /* allocate atrivial buffer in FAST memory */
  37. if(Left==0)
  38.       TidyUp("Not enough memory for LEFT frag");
  39.  
  40. Right = (struct Frag *)S5AllocateFrag(RightSize,1); /* Same. Not actually used */
  41. if(Right==0)
  42.       TidyUp("Not enough memory for RIGHT frag");
  43.  
  44. ok = TRUE;
  45. while(ok)
  46.       {
  47.       printf(" Options : \n\n");
  48.       printf("     B ... set buffer size. (now %ld)\n",LeftSize);
  49.       printf("     R ... record.\n");
  50.       printf("     P ... play.\n");
  51.       printf("     S ... save to file.\n");
  52.       printf("     L ... load from file.\n");
  53.       printf("     Q ... quit.\n");
  54.       printf("\n\n     ? ");
  55.  
  56.       scanf("%c%c",&c,&newline);
  57.       printf("\n\n");
  58.       switch(toupper(c))
  59.             {
  60.             case 'B':
  61.                         NewSize();
  62.                         break;
  63.             case 'R':
  64.                         Disable();
  65.                         S5FastMemRecord(Left);
  66.                         Enable();
  67.                         break;
  68.             case 'P':
  69.                         S5FastMemPlay(Left);
  70.                         break;
  71.             case 'S':
  72.                         SaveFile();
  73.                         break;
  74.             case 'L':
  75.                         LoadFile();
  76.                         break;
  77.             case 'Q':
  78.                         ok = FALSE;
  79.                         break;
  80.             default:
  81.                         printf("Unrecognised option\n");
  82.             }
  83.  
  84.       }
  85.  
  86. CloseLibrary(S5Base);
  87. printf("lp finished\n");
  88. }
  89.  
  90.  
  91. TidyUp(mess)
  92. char *mess;
  93. {
  94. printf("lp exiting : %s\n",mess);
  95. if(Left)    S5DeallocateFrag(Left);
  96. if(Right)   S5DeallocateFrag(Right);
  97. if(S5Base)  CloseLibrary(S5Base);
  98. exit(-1);
  99. }
  100.  
  101.  
  102. NewSize()
  103. {
  104. char  nl;
  105. int   size;
  106. S5DeallocateFrag(Left);
  107. Left = 0;
  108. LeftSize = 0;
  109. do {
  110.    printf("New size ? ");
  111.    scanf("%d%c",&size,&nl);
  112.    Left = (struct Frag *)S5AllocateFrag(size,1);
  113.    if(Left) printf("OK\n");
  114.    else     printf("Failed to allocate. Try again.\n");
  115.    } while(Left==0);
  116. LeftSize = size;
  117. }
  118.  
  119. #define EOLN '\n'
  120. #define FNL 32
  121. char filename[FNL];
  122.  
  123.  
  124. GetFileName()
  125. {
  126. char *p,c;
  127. int count;
  128. /* NB this assumes that getchar() doesnt get any characters until \n is
  129.    pressed - as usual. This permits simple line editing
  130. */
  131. count = 0;
  132. p = filename;
  133. do {
  134.    c = getchar();
  135.    if(c!=EOLN)
  136.          if(count < (FNL-1))
  137.                {
  138.                *p++ = c;
  139.                count++;
  140.                }
  141.    } while(c!=EOLN);
  142. *p++ = 0;
  143. return( count>0 );
  144. }
  145.  
  146.  
  147. LoadFile()
  148. {
  149. int Success;
  150. printf("Loader. NB buffer will not be reallocated\n");
  151. printf("Load from which file ? ");
  152. if(GetFileName()==0) return(0);
  153. Success = S5LoadSOP3(Left,filename,1);
  154. ECode(Success);
  155. return(Success==E_OK);
  156. }
  157.  
  158. SaveFile()
  159. {
  160. int Success;
  161. printf("Save to which file ? ");
  162. if(GetFileName()==0) return(0);
  163. Success = S5SaveSOP3(Left,filename,0);
  164. ECode(Success);
  165. return(Success=E_OK);
  166. }
  167.  
  168.  
  169. ECode(v)
  170. int v;
  171. {
  172. switch(v)
  173.    {
  174.    case E_OK:     printf("No error.\n"); break;
  175.    case E_OPEN:   printf("Failed to open file.\n"); break;
  176.    case E_CLOSE:  printf("Failed to close file.\n"); break;
  177.    case E_READ:   printf("Read Error. / End of file.\n"); break;
  178.    case E_WRITE:  printf("Write Error. / Disk full.\n"); break;
  179.    case E_FORMAT: printf("File is of an inappropriate format.\n"); break;
  180.    case E_MEM:    printf("Insufficient memory.\n"); break;
  181.    case E_DEV:    printf("Device Open error.\n"); break;
  182.    case E_PORT:   printf("Unable to create port.\n"); break;
  183.    case E_OKNULL: printf("Operation was null. Inappropriate conditions.\n");
  184.    default:       printf("Unspecified error.\n");
  185.    }
  186. }
  187.  
  188.