home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / SoundAndMusic / SoundLibrary / recordchaintest.c < prev    next >
Text File  |  1990-10-10  |  3KB  |  106 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        5.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 *)sW(r->dataLocation), 1, s->dataSize, sfp) !=
  30.     s->dataSize)
  31.     fprintf(stderr, "recordDone: could not write data to soundfile\n");
  32.     recordedDataSize += s->dataSize;
  33.     record((lastBufNum + 1) % NUM_BUFFERS);
  34.     return 0;
  35. }
  36.  
  37. static void record(int bufNum)
  38. /*
  39.  * Initiates recording into a buffers[bufNum].
  40.  */
  41. {
  42.     int err;
  43.     static int tag = 1;
  44.     
  45.     lastBufNum = bufNum;
  46.     if (err = SNDStartRecording(buffers[bufNum], tag++, 0, 0, SND_NULL_FUN,
  47.                 recordDone))
  48.         fprintf(stderr, "record: %s\n", SNDSoundError(err));
  49. }
  50.  
  51. static void init(int n, int size, char *fileName)
  52. /*
  53.  * Allocate n sound buffers.
  54.  * Creates the soundfile.
  55.  */
  56. {
  57.     int i, err;
  58.     SNDSoundStruct s;
  59.  
  60.     for (i = 0; i < n; i++)
  61.         if (err = SNDAlloc(&buffers[i], size, SND_FORMAT_MULAW_8,
  62.                SND_RATE_CODEC, 1, 4))
  63.         fprintf(stderr, "init: %s\n", SNDSoundError(err));
  64.     if ((sfp = fopen(fileName, "w")) == NULL)
  65.     fprintf(stderr, "init: could not open soundfile %s\n", fileName);
  66.     if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, sfp) != 1)
  67.     fprintf(stderr, "init: could not write dummy header to soundfile\n");
  68. }
  69.  
  70. static void cleanup(void)
  71. /*
  72.  * Write the soundfile header.
  73.  */
  74. {
  75.     SNDSoundStruct s;
  76.  
  77.     s.magic = SND_MAGIC;
  78.     s.dataLocation = sizeof(SNDSoundStruct);
  79.     s.dataSize = recordedDataSize;
  80.     s.dataFormat = SND_FORMAT_MULAW_8;
  81.     s.samplingRate = SND_RATE_CODEC;
  82.     s.channelCount = 1;
  83.  
  84.     rewind(sfp);
  85.     if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, sfp) != 1)
  86.     fprintf(stderr, "cleanup: could not write header to soundfile\n");
  87. }
  88.  
  89. main(int argc, char *argv[])
  90. {
  91.     int i;
  92.  
  93.     if (argc != 2) {
  94.     fprintf(stderr, "usage: recordchaintest file\n");
  95.     exit(1);
  96.     }
  97.  
  98.     requestedDataSize = SECONDS * SND_RATE_CODEC;
  99.     init(NUM_BUFFERS, BUF_SIZE, argv[1]);
  100.     printf("recording...\n");
  101.     for (i = 0; i < NUM_BUFFERS; i++)
  102.     record(i);
  103.     SNDWait(0);
  104.     cleanup();
  105. }
  106.