void MMS_Audio_Init() { MMRESULT status; LPWAVEOUTCAPS lpCaps; if( waveOutGetNumDevs() < 1 ) { fprintf(stderr,"Audio disabled - No Multimedia Services compatible audio devices available\n"); return; } /* Figure out device capabilities - Just use device 0 for now */ if((lpCaps = (LPWAVEOUTCAPS)mmeAllocMem(sizeof(WAVEOUTCAPS))) == NULL ) { fprintf(stderr,"Failed to allocate WAVEOUTCAPS struct\n"); return; } status = waveOutGetDevCaps( 0, lpCaps, sizeof(WAVEOUTCAPS)); if( status != MMSYSERR_NOERROR ) { fprintf(stderr,"waveOutGetDevCaps failed - status = %d\n", status); } mmeFreeMem(lpCaps); }
inline int MmeGrabber::mme_error(MMRESULT code, const char* msg) const { if (code != DV_ERR_OK) { char *buffer; if(buffer=(char *)mmeAllocMem(MAXERRORLENGTH)) { videoGetErrorText(handle_,code,buffer,MAXERRORLENGTH); fprintf(stderr, "vic: %s - %s (%d)\n", msg, buffer, code); mmeFreeMem(buffer); } return (1); } return (0); }
void MmeGrabber::init() { int size; MMRESULT s; /* fprintf(stderr, "vic: Called MmeGrabber::init\n"); */ /* * Allocate & zero some memory for the video header and bitmap * info header. We allocate it as a single block to keep it * all in one shared memory segment. * (Make sure everything's aligned on quadword boundaries) */ size = ((sizeof(*bmh)+sizeof(void*)-1) / sizeof(void*)) * sizeof(void*); bmh = (info*)mmeAllocMem(size+sizeof(*vh_)); if (bmh == 0) { fprintf(stderr, "vic: cannot allocate mme memory\n"); valid_ = 0; return; } memset(bmh, 0, size+sizeof(*vh_)); vh_ = (VIDEOHDR*)((char*)bmh+size); if (server_ < 0) { server_ = mmeServerFileDescriptor(); link(server_, TK_READABLE); } return; bail: mmeFreeMem(bmh); bmh = 0; videoClose(handle_); vh_ = 0; valid_ = 0; /*XXX*/ }
static int MME_OpenDevice(_THIS, const char *devname, int iscapture) { int valid_format = 0; MMRESULT result; Uint8 *mixbuf = NULL; int i; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden, 0, (sizeof *this->hidden)); /* Set basic WAVE format parameters */ this->hidden->shm = mmeAllocMem(sizeof(*this->hidden->shm)); if (this->hidden->shm == NULL) { MME_CloseDevice(this); SDL_OutOfMemory(); return 0; } SDL_memset(this->hidden->shm, '\0', sizeof(*this->hidden->shm)); this->hidden->shm->sound = 0; this->hidden->shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM; /* Determine the audio parameters from the AudioSpec */ /* Try for a closest match on audio format */ for (test_format = SDL_FirstAudioFormat(this->spec.format); !valid_format && test_format;) { valid_format = 1; switch (test_format) { case AUDIO_U8: case AUDIO_S16: case AUDIO_S32: break; default: valid_format = 0; test_format = SDL_NextAudioFormat(); } } if (!valid_format) { MME_CloseDevice(this); SDL_SetError("Unsupported audio format"); return 0; } this->spec.format = test_format; this->hidden->shm->wFmt.wBitsPerSample = SDL_AUDIO_BITSIZE(test_format); /* !!! FIXME: Can this handle more than stereo? */ this->hidden->shm->wFmt.wf.nChannels = this->spec.channels; this->hidden->shm->wFmt.wf.nSamplesPerSec = this->spec.freq; this->hidden->shm->wFmt.wf.nBlockAlign = this->hidden->shm->wFmt.wf.nChannels * this->hidden->shm->wFmt.wBitsPerSample / 8; this->hidden->shm->wFmt.wf.nAvgBytesPerSec = this->hidden->shm->wFmt.wf.nSamplesPerSec * this->hidden->shm->wFmt.wf.nBlockAlign; /* Check the buffer size -- minimum of 1/4 second (word aligned) */ if (this->spec.samples < (this->spec.freq / 4)) this->spec.samples = ((this->spec.freq / 4) + 3) & ~3; /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec); /* Open the audio device */ result = waveOutOpen(&(this->hidden->shm->sound), WAVE_MAPPER, &(this->hidden->shm->wFmt.wf), MME_Callback, NULL, (CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE)); if (result != MMSYSERR_NOERROR) { MME_CloseDevice(this); SetMMerror("waveOutOpen()", result); return 0; } /* Create the sound buffers */ mixbuf = (Uint8 *) mmeAllocBuffer(NUM_BUFFERS * (this->spec.size)); if (mixbuf == NULL) { MME_CloseDevice(this); SDL_OutOfMemory(); return 0; } this->hidden->mixbuf = mixbuf; for (i = 0; i < NUM_BUFFERS; i++) { this->hidden->shm->wHdr[i].lpData = &mixbuf[i * (this->spec.size)]; this->hidden->shm->wHdr[i].dwBufferLength = this->spec.size; this->hidden->shm->wHdr[i].dwFlags = 0; this->hidden->shm->wHdr[i].dwUser = i; this->hidden->shm->wHdr[i].dwLoops = 0; /* loop control counter */ this->hidden->shm->wHdr[i].lpNext = NULL; /* reserved for driver */ this->hidden->shm->wHdr[i].reserved = 0; inUse[i] = FALSE; } this->hidden->next_buffer = 0; return 1; }
static int MME_OpenAudio(_THIS, SDL_AudioSpec *spec) { MMRESULT result; int i; mixbuf = NULL; /* Set basic WAVE format parameters */ shm = mmeAllocMem(sizeof(*shm)); if ( shm == NULL ) { SDL_SetError("Out of memory: shm"); return(-1); } shm->sound = 0; shm->wFmt.wf.wFormatTag = WAVE_FORMAT_PCM; /* Determine the audio parameters from the AudioSpec */ switch ( spec->format & 0xFF ) { case 8: /* Unsigned 8 bit audio data */ spec->format = AUDIO_U8; shm->wFmt.wBitsPerSample = 8; break; case 16: /* Signed 16 bit audio data */ spec->format = AUDIO_S16; shm->wFmt.wBitsPerSample = 16; break; default: SDL_SetError("Unsupported audio format"); return(-1); } shm->wFmt.wf.nChannels = spec->channels; shm->wFmt.wf.nSamplesPerSec = spec->freq; shm->wFmt.wf.nBlockAlign = shm->wFmt.wf.nChannels * shm->wFmt.wBitsPerSample / 8; shm->wFmt.wf.nAvgBytesPerSec = shm->wFmt.wf.nSamplesPerSec * shm->wFmt.wf.nBlockAlign; /* Check the buffer size -- minimum of 1/4 second (word aligned) */ if ( spec->samples < (spec->freq/4) ) spec->samples = ((spec->freq/4)+3)&~3; /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(spec); /* Open the audio device */ result = waveOutOpen(&(shm->sound), WAVE_MAPPER, &(shm->wFmt.wf), MME_CALLBACK, NULL, (CALLBACK_FUNCTION|WAVE_OPEN_SHAREABLE)); if ( result != MMSYSERR_NOERROR ) { SetMMerror("waveOutOpen()", result); return(-1); } /* Create the sound buffers */ mixbuf = (Uint8 *)mmeAllocBuffer(NUM_BUFFERS * (spec->size)); if ( mixbuf == NULL ) { SDL_SetError("Out of memory: mixbuf"); return(-1); } for (i = 0; i < NUM_BUFFERS; i++) { shm->wHdr[i].lpData = &mixbuf[i * (spec->size)]; shm->wHdr[i].dwBufferLength = spec->size; shm->wHdr[i].dwFlags = 0; shm->wHdr[i].dwUser = i; shm->wHdr[i].dwLoops = 0; /* loop control counter */ shm->wHdr[i].lpNext = NULL; /* reserved for driver */ shm->wHdr[i].reserved = 0; inUse[i] = FALSE; } next_buffer = 0; return 0; }
int init_sound (void) { int rate; int dspbits; int channels; MMRESULT status; LPPCMWAVEFORMAT waveformat; if (currprefs.sound_maxbsiz < 128 || currprefs.sound_maxbsiz > 16384) { fprintf(stderr, "Sound buffer size %d out of range.\n", currprefs.sound_maxbsiz); currprefs.sound_maxbsiz = 8192; } sndbufsize = currprefs.sound_maxbsiz; dspbits = currprefs.sound_bits; rate = currprefs.sound_freq; channels = 1; if((waveformat = (LPPCMWAVEFORMAT) mmeAllocMem(sizeof(PCMWAVEFORMAT))) == NULL ) { fprintf(stderr, "Failed to allocate PCMWAVEFORMAT struct\n"); return 0; } waveformat->wf.nSamplesPerSec = rate; waveformat->wf.nChannels = channels; waveformat->wBitsPerSample = dspbits; waveformat->wf.wFormatTag = WAVE_FORMAT_PCM; bytes_per_sample = waveformat->wf.nChannels * (waveformat->wBitsPerSample/8); waveformat->wf.nBlockAlign = bytes_per_sample; waveformat->wf.nAvgBytesPerSec = bytes_per_sample * waveformat->wf.nSamplesPerSec; /* Open the audio device with desired rate/format */ status = waveOutOpen( &mme_handle, WAVE_MAPPER, (LPWAVEFORMAT)waveformat, (void (*)())mme_callback, (unsigned int)NULL, CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE ); mmeFreeMem(waveformat); if(status != MMSYSERR_NOERROR) { fprintf(stderr, "waveOutOpen failed - status = %d\n", status); return 0; } /* Allocate wave header for use in write */ if((WaveHeader = (LPWAVEHDR) mmeAllocMem(sizeof(WAVEHDR))) == NULL ) { fprintf(stderr, "Failed to allocate WAVEHDR struct\n"); return 0; } /* Allocate shared audio buffer for communicating with audio device */ if ((mme_audiobuf = (LPSTR) mmeAllocBuffer(sndbufsize*SOUND_NUMBUF*bytes_per_sample*2)) == NULL) { fprintf(stderr, "Failed to allocate shared audio buffer\n"); mmeFreeMem(WaveHeader); return 0; } sndbuffer = mme_audiobuf; obtained_freq = rate; if (dspbits == 16) { init_sound_table16 (); sample_handler = sample16_handler; } else { init_sound_table8 (); sample_handler = sample8_handler; } sound_available = 1; printf ("Sound driver found and configured for %d bits at %d Hz, buffer is %d bytes\n", dspbits, rate, sndbufsize); mme_sndbufpt = sndbufpt = sndbuffer; return 1; }