Annotation of Examples/SoundAndMusic/SoundLibrary/recordchaintest.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.