home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / misc / NeXT / record.c < prev   
Text File  |  1991-08-13  |  2KB  |  98 lines

  1. /*
  2.  * recordchaintest.c - this test shows how to chain small recordings
  3.  * and append them to a soundfile.
  4.  */ 
  5. #import    <stdio.h>
  6. #import    <sound/sound.h>
  7.  
  8. #define    NUM_BUFFERS    2
  9. #define    BUF_SIZE    8192
  10. #define    SECONDS        10000.0
  11.  
  12. static SNDSoundStruct *buffers[NUM_BUFFERS];
  13. static FILE *sfp;
  14. static int requestedDataSize, recordedDataSize = 0;
  15. static int lastBufNum;
  16.  
  17. static void record(int tag);
  18.  
  19. static int recordDone(SNDSoundStruct *s, int tag, int err)
  20. /*
  21.  * Called when a buffer has been recorded.
  22.  * Appends the buffer to the soundfile and initiates recording into it again.
  23.  */
  24. {
  25.     if (recordedDataSize >= requestedDataSize)
  26.     return 0;
  27.     if (err)
  28.         fprintf(stderr, "recordDone: %s\n", SNDSoundError(err));
  29.     if (fwrite((void*)((char*)s + s->dataLocation), 1, s->dataSize, stdout) !=
  30.     s->dataSize)
  31.     fprintf(stderr, "recordDone: could not write data to soundfile\n");
  32.  
  33.     recordedDataSize += s->dataSize;
  34.     record((lastBufNum + 1) % NUM_BUFFERS);
  35.     return 0;
  36. }
  37.  
  38. static void record(int bufNum)
  39. /*
  40.  * Initiates recording into a buffers[bufNum].
  41.  */
  42. {
  43.     int err;
  44.     static int tag = 1;
  45.     
  46.     lastBufNum = bufNum;
  47.     if (err = SNDStartRecording(buffers[bufNum], tag++, 0, 0, SND_NULL_FUN,
  48.                 recordDone))
  49.         fprintf(stderr, "record: %s\n", SNDSoundError(err));
  50. }
  51.  
  52. static void init(int n, int size)
  53. /*
  54.  * Allocate n sound buffers.
  55.  * Creates the soundfile.
  56.  */
  57. {
  58.     int i, err;
  59.     SNDSoundStruct s;
  60.  
  61.     for (i = 0; i < n; i++)
  62.         if (err = SNDAlloc(&buffers[i], size, SND_FORMAT_MULAW_8,
  63.                SND_RATE_CODEC, 1, 4))
  64.         fprintf(stderr, "init: %s\n", SNDSoundError(err));
  65.     if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, stdout) != 1)
  66.     fprintf(stderr, "init: could not write dummy header to soundfile\n");
  67. }
  68.  
  69. static void cleanup(void)
  70. /*
  71.  * Write the soundfile header.
  72.  */
  73. {
  74.     SNDSoundStruct s;
  75.  
  76.     s.magic = SND_MAGIC;
  77.     s.dataLocation = sizeof(SNDSoundStruct);
  78.     s.dataSize = recordedDataSize;
  79.     s.dataFormat = SND_FORMAT_MULAW_8;
  80.     s.samplingRate = SND_RATE_CODEC;
  81.     s.channelCount = 1;
  82.  
  83.     if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, stdout) != 1)
  84.     fprintf(stderr, "cleanup: could not write header to soundfile\n");
  85. }
  86.  
  87. main(int argc, char *argv[])
  88. {
  89.     int i;
  90.  
  91.     requestedDataSize = SECONDS * SND_RATE_CODEC;
  92.     cleanup();
  93.     init(NUM_BUFFERS, BUF_SIZE);
  94.     for (i=0; i<NUM_BUFFERS; i++)
  95.         record(i);
  96.     SNDWait(0);
  97. }
  98.