Skip to contentSkip to conversation

successive mmap()s will not work to record sound?

DISCUSSION Britton 2 participants 3 messages Feb 11, 1999 Sound

lore
Share & tools
lore

Britton

author
#0 lore raw

I have been trying to write a brec-like program that will record long tracks of raw sound using many successive mmap()s of the same file, but it isn't working. I get stuff like this:

incorrect number of bytes written: Interrupted system call
Sound: Recording overrun
<DMA somethingorother times> out - IRQ/DRQ config error?

ad nauseum

Does this mean that my code is not keeping up with the sound card?

Is this approach not feasible? Here are the salient features of the program:

/* appropriate headers */

int target[10];               /* current file descriptors to write to */
char *region[10];             /* pointers to the current mapped file */
int nblocks;                  /* the total number of 65536 byte "blocks" */
int cblock;                   /* the current block less than nblocks */
int cmmap;                    /* the current mmap in 10 block loop */
pid_t pid;                    /* the pid of the msyncing munmaping child */

/* ioctrl dsp device with RATE BITS TIME and STEREO */

/* figure out how many 65536 byte mmaps we will need */ nblocks = (LENGTH*RATE*SIZE*CHANNELS)/(8*65536) ;

  for (cblock = 0; cblock < nblocks; cblock++) {
    cmmap = cblock % 10;
    printf("cmmap: %d       looping: %d\n", cmmap, cblock);
    /* open, make zero hole, and mmap the target region */
    status = target[cmmap] = open("targetfile", O_CREAT | O_RDWR,
             0644);
    if (status < 0) {
      perror("error opening target file");
      exit(1);
    }
    if (lseek(target[cmmap], 65535, SEEK_END) = (off_t) (-1)) {
      perror("lseek() error");
      exit(1);
    }
    if (write(target[cmmap], "", 1) < 0) {
      perror("write() error");
      exit(1);
    }
    region[cmmap] = mmap(NULL, 65536, PROT_WRITE, MAP_SHARED, target[cmmap],
                             65536*cblock);
    if (region[cmmap] = (caddr_t) (-1)) {
      perror("mmap error");
      exit(1);
    }

    /* record sound into mmaped region */
    status = read(dsp, (region[cmmap]), 65536);
    if (status != 65536) {
      perror("incorrect number of bytes written");
      exit(1);
    }

    /* fork a low priority child for msync with disk */
    if ((pid = fork()) < 0)
      perror("fork error");
    else if (pid = 0) {        /* child */

      /* make ourselves very nice */
      if (nice(19) = -1)
        perror("nice error");

      /* make sure the mmaped stuff gets written to disk "soon" */
      msync(region[cmmap], 65536, MS_ASYNC);

      /* unmap the current region, so it can be reused next time around */
      if (munmap(region[cmmap], 65536) = -1) {
        perror("munmap error");
        exit(1);
      }

      /* close the file descriptor to the cmmap */
      if (close(target[cmmap]) = -1) {
        perror("close error");
        exit(1);
      }
    }

}
}

Any help or advice greatly appreciated.

Britton Kerin

#1 lore raw

In article <[email protected]>,

Quoted 8 lines · Britton

I have been trying to write a brec-like program that will record long tracks of raw sound using many successive mmap()s of the same file, but it isn't working. I get stuff like this:

incorrect number of bytes written: Interrupted system call
Sound: Recording overrun
<DMA somethingorother times> out - IRQ/DRQ config error?

Quoted 6 lines

ad nauseum

Does this mean that my code is not keeping up with the sound card?

Is this approach not feasible? Here are the salient features of the program:

No, it's not feasible.

The filesystem will block at inconvenient times sometimes for up to fives seconds (this on 33MB/s UDMA AT drives). This only gets worse if you are doing mmap().

What you want to do is read into shared memory with one process or thread and write to disk with another process or thread. Depending on your performance requirements even just two processes with select() is enough.

Signature · 4 lines
Zygo Blaxell, Linux Engineer, Corel Corporation, [email protected] (work),
[email protected] (play).  It's my opinion, I tell you! Mine! All MINE!
Size of 'diff -Nurw [...] winehq corel' as of Thu Feb 11 11:14:01 EST 1999
Lines/files:  In 489 / 13, Out 6321 / 74, Both 6807 / 86
#2 lore raw
In reply to Britton

In article <[email protected]>,

Quoted 5 lines · Zygo Blaxell

In article <[email protected]>,

Quoted 3 lines · Britton

I have been trying to write a brec-like program that will record long tracks of raw sound using many successive mmap()s of the same file, but it isn't working. I get stuff like this:

Come to think of it, if you _really_ want to do this, why don't you mmap() the entire file and just read() into that?

Also, make sure that the file is already large enough to hold the data you are writing; otherwise you'll get a segfault.

Signature · 4 lines
Zygo Blaxell, Linux Engineer, Corel Corporation, [email protected] (work),
[email protected] (play).  It's my opinion, I tell you! Mine! All MINE!
Size of 'diff -Nurw [...] winehq corel' as of Thu Feb 11 13:14:02 EST 1999
Lines/files:  In 489 / 13, Out 6383 / 76, Both 6869 / 88