home *** CD-ROM | disk | FTP | other *** search
- /*
- * recordchaintest.c - this test shows how to chain small recordings
- * and append them to a soundfile.
- *
- * 2/28/94 -- Made architecture independent (rkd).
- */
-
- #import <stdio.h>
- #import <sound/sound.h>
- #import <architecture/byte_order.h>
-
- #define NUM_BUFFERS 2
- #define BUF_SIZE 8192
- #define SECONDS 5.0
-
- static SNDSoundStruct *buffers[NUM_BUFFERS];
- static FILE *sfp;
- static int requestedDataSize, recordedDataSize = 0;
- static int lastBufNum;
-
- static void record(int tag);
-
- static int recordDone(SNDSoundStruct *s, int tag, int err)
- /*
- * Called when a buffer has been recorded.
- * Appends the buffer to the soundfile and initiates recording into it again.
- */
- {
- if (recordedDataSize >= requestedDataSize)
- return 0;
- if (err)
- fprintf(stderr, "recordDone: %s\n", SNDSoundError(err));
- if (fwrite((void *)((char *)s + s->dataLocation), 1, s->dataSize, sfp) !=
- s->dataSize)
- fprintf(stderr, "recordDone: could not write data to soundfile\n");
- recordedDataSize += s->dataSize;
- record((lastBufNum + 1) % NUM_BUFFERS);
- return 0;
- }
-
- static void record(int bufNum)
- /*
- * Initiates recording into a buffers[bufNum].
- */
- {
- int err;
- static int tag = 1;
-
- lastBufNum = bufNum;
- if (err = SNDStartRecording(buffers[bufNum], tag++, 0, 0, SND_NULL_FUN,
- recordDone))
- fprintf(stderr, "record: %s\n", SNDSoundError(err));
- }
-
- static void init(int n, int size, char *fileName)
- /*
- * Allocate n sound buffers.
- * Creates the soundfile.
- */
- {
- int i, err;
- SNDSoundStruct s;
-
- for (i = 0; i < n; i++)
- if (err = SNDAlloc(&buffers[i], size, SND_FORMAT_MULAW_8,
- SND_RATE_CODEC, 1, 4))
- fprintf(stderr, "init: %s\n", SNDSoundError(err));
- if ((sfp = fopen(fileName, "w")) == NULL)
- fprintf(stderr, "init: could not open soundfile %s\n", fileName);
- if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, sfp) != 1)
- fprintf(stderr, "init: could not write dummy header to soundfile\n");
- }
-
- static void cleanup(void)
- /*
- * Write the soundfile header.
- */
- {
- SNDSoundStruct s;
-
- s.magic = NXSwapHostIntToBig(SND_MAGIC);
- s.dataLocation = NXSwapHostIntToBig(sizeof(SNDSoundStruct));
- s.dataSize = NXSwapHostIntToBig(recordedDataSize);
- s.dataFormat = NXSwapHostIntToBig(SND_FORMAT_MULAW_8);
- s.samplingRate = NXSwapHostIntToBig(SND_RATE_CODEC);
- s.channelCount = NXSwapHostIntToBig(1);
- s.info[0] = '\0';
-
- rewind(sfp);
- if (fwrite((void *)&s, sizeof(SNDSoundStruct), 1, sfp) != 1)
- fprintf(stderr, "cleanup: could not write header to soundfile\n");
- }
-
- main(int argc, char *argv[])
- {
- int i;
-
- if (argc != 2) {
- fprintf(stderr, "usage: recordchaintest file\n");
- exit(1);
- }
-
- requestedDataSize = SECONDS * SND_RATE_CODEC;
- init(NUM_BUFFERS, BUF_SIZE, argv[1]);
- printf("recording...\n");
- for (i = 0; i < NUM_BUFFERS; i++)
- record(i);
- SNDWait(0);
- cleanup();
- }
-