ref: 99d14fa43f2ee64ebf542ab5b7e87e77939c0cdb
dir: /snd_9.c/
#include <u.h> #include <libc.h> #include <stdio.h> #include <thread.h> #include "quakedef.h" int audio_fd; int snd_inited; int wpos; int stid = -1; enum{ Nbuf = 16 }; Channel *schan; void sproc (void *) { int n; for(;;){ if(recv(schan, nil) < 0){ Con_Printf("sproc:recv %r\n"); break; } if((n = write(audio_fd, shm->buffer, shm->samplebits/8 * shm->samples)) < 0){ Con_Printf("sproc:write: %r\n"); break; } wpos += n; } stid = -1; } qboolean SNDDMA_Init(void) { int i; snd_inited = 0; if((audio_fd = open("/dev/audio", OWRITE)) < 0){ Con_Printf("Could not open /dev/audio: %r\n"); return 0; } shm = &sn; shm->splitbuffer = 0; if((i = COM_CheckParm("-sndbits")) != 0) shm->samplebits = atoi(com_argv[i+1]); if(shm->samplebits != 16 && shm->samplebits != 8) shm->samplebits = 16; if((i = COM_CheckParm("-sndspeed")) != 0) shm->speed = atoi(com_argv[i+1]); else shm->speed = 44100; shm->channels = 2; if(COM_CheckParm("-sndmono") != 0) shm->channels = 1; //shm->samples = info.fragstotal * info.fragsize / (shm->samplebits/8); shm->samples = 1024; shm->submission_chunk = 1; if((shm->buffer = mallocz(shm->samplebits/8 * shm->samples, 1)) == nil) Sys_Error("SNDDMA_Init:malloc: %r\n"); shm->samplepos = 0; snd_inited = 1; schan = chancreate(sizeof(int), Nbuf); if((stid = proccreate(sproc, nil, mainstacksize)) < 0){ stid = -1; SNDDMA_Shutdown(); Sys_Error("SNDDMA_Init:proccreate: %r\n"); } return 1; } int SNDDMA_GetDMAPos(void) { if(!snd_inited) return 0; shm->samplepos = wpos / (shm->samplebits/8); return shm->samplepos; } void SNDDMA_Shutdown(void) { if(snd_inited){ close(audio_fd); free(shm->buffer); if(stid != -1){ threadkill(stid); stid = -1; } chanclose(schan); chanfree(schan); snd_inited = 0; } } void SNDDMA_Submit(void) { if(nbsend(schan, nil) < 0){ Con_Printf("SNDDMA_Submit:send: %r\n"); SNDDMA_Shutdown(); } }