static int ESD_Init_internal(void) { format=(md_mode&DMODE_16BITS?ESD_BITS16:ESD_BITS8)| (md_mode&DMODE_STEREO?ESD_STEREO:ESD_MONO)|ESD_STREAM|ESD_PLAY; if (md_mixfreq > ESD_DEFAULT_RATE) md_mixfreq = ESD_DEFAULT_RATE; /* make sure we can open an esd stream with our parameters */ if (!(SETENV)) { if ((sndfd=esd_playstream(format,md_mixfreq,espeaker,"libmikmod"))<0) { _mm_errno=MMERR_OPENING_AUDIO; return 1; } fcntl(sndfd, F_SETFL, fcntl(sndfd, F_GETFL) | O_NONBLOCK); } else { _mm_errno=MMERR_OUT_OF_MEMORY; return 1; } /* Initialize the audiobuffer */ audiobuffer_init(); return VC_Init(); }
static BOOL OS2_Init(void) { MCI_OPEN_PARMS mciOpenParms; MCI_WAVE_SET_PARMS mciWaveSetParms; int i, bpsrate; ULONG ulInterval; if (VC_Init()) return 1; DeviceID = 0; ThreadID = 0; Timer = NULLHANDLE; Update = NULLHANDLE; Play = NULLHANDLE; AudioBuffer = NULL; /* Compute the bytes per second output rate */ bpsrate = md_mixfreq; if (md_mode & DMODE_STEREO) bpsrate <<= 1; if (md_mode & DMODE_16BITS) bpsrate <<= 1; /* Compute audio buffer size if necessary */ if (BufferSize == (ULONG)-1) { int bit; BufferSize = bpsrate >> 3; for (bit = 15; bit >= 12; bit--) if (BufferSize & (1 << bit)) break; BufferSize = 1 << bit; }
static BOOL NDS_SW_Init(void) { md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; md_mode &= ~DMODE_STEREO; ipc = (NDS_SW_IPC*)_mm_malloc(sizeof(NDS_SW_IPC)); if (ipc == NULL) { MikMod_errno = MMERR_OUT_OF_MEMORY; return 1; } ipc->buffer = (SBYTE*)_mm_malloc(BUFFERSIZE); if (ipc->buffer == NULL) { _mm_free(ipc); ipc = NULL; MikMod_errno = MMERR_OUT_OF_MEMORY; return 1; } if (VC_Init()) { return 1; } ipc->bufferSize = BUFFERSIZE; ipc->sampleRate = md_mixfreq; ipc->format = (md_mode & DMODE_16BITS) ? 16 : 8; MikMod9_SendCommand(NDS_SW_CMD_INIT << 28 | (u32)ipc); return 0; }
static BOOL RAW_Init(void) { #if defined unix || (defined __APPLE__ && defined __MACH__) if(!MD_Access(filename?filename:FILENAME)) { _mm_errno=MMERR_OPENING_FILE; return 1; } #endif if((rawout=open(filename?filename:FILENAME,O_RDWR|O_TRUNC|O_CREAT|O_BINARY #if !defined(macintosh) && !defined(__MWERKS__) ,S_IREAD|S_IWRITE #endif ))<0) { _mm_errno=MMERR_OPENING_FILE; return 1; } md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; if (!(audiobuffer=(SBYTE*)_mm_malloc(BUFFERSIZE))) { close(rawout);unlink(filename?filename:FILENAME); rawout=-1; return 1; } if ((VC_Init())) { close(rawout);unlink(filename?filename:FILENAME); rawout=-1; return 1; } return 0; }
static BOOL NS_Init(void) { #ifndef SDL_MIXER_ONLY zerobuf=(SBYTE*)_mm_malloc(ZEROLEN); #endif return VC_Init(); }
static BOOL stdout_Init(void) { if(!(audiobuffer=(SBYTE*)_mm_malloc(BUFFERSIZE))) return 1; #ifdef __EMX__ _fsetmode(stdout,"b"); #endif return VC_Init(); }
static int GP32_Init(void) { md_mode = DMODE_STEREO | DMODE_16BITS | DMODE_SOFT_MUSIC | DMODE_SOFT_SNDFX; md_mixfreq = 44100; gp_initSound(44100, 16, GP32_ringsize); /* 44k sound, 16bpp, 2x4k buffers */ GP32_buffer = (SBYTE *) MikMod_malloc(GP32_buffersize); /* Half of the 8k ringbuffer */ if (!GP32_buffer) return 1; gp_clearRingbuffer(); return VC_Init(); }
static BOOL pipe_Init(void) { if(!target) { _mm_errno=MMERR_OPENING_FILE; return 1; } #if !defined unix && (!defined __APPLE__ || !defined __MACH__) #ifdef __EMX__ _fsetmode(stdout, "b"); #endif #ifdef __WATCOMC__ pipefile = _popen(target, "wb"); #else pipefile = popen(target, "wb"); #endif if (!pipefile) { _mm_errno=MMERR_OPENING_FILE; return 1; } #else /* poor man's popen() */ if (pipe(pipefd)) { _mm_errno = MMERR_OPENING_FILE; return 1; } switch (pid=fork()) { case -1: close(pipefd[0]); close(pipefd[1]); pipefd[0]=pipefd[1]=-1; _mm_errno=MMERR_OPENING_FILE; return 1; case 0: if (pipefd[0]) { dup2(pipefd[0],0); close(pipefd[0]); } close(pipefd[1]); if (!MD_DropPrivileges()) execl("/bin/sh","sh","-c",target,NULL); exit(127); } close(pipefd[0]); if (!(pipefile=fdopen(pipefd[1],"wb"))) { _mm_errno=MMERR_OPENING_FILE; return 1; } #endif if(!(pipeout=_mm_new_file_writer(pipefile))) return 1; if(!(audiobuffer=(SBYTE*)MikMod_malloc(BUFFERSIZE))) return 1; md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; return VC_Init(); }
BOOL MD_Init(void) { if (sl_buffer == NULL) sl_buffer = (SWORD*)mod_malloc(1024*sizeof(SWORD)); // return md_driver->Init(); if(!VC_Init()) { return 0; } return 1; }
static BOOL PSP_Init(void) { if (VC_Init()) return 1; // pspAudioInit(); // pspAudioSetChannelCallback(0, (void *)sound_callback); return 0; }
static BOOL ANDROID_Init(void) { audio_ready = 1; if (VC_Init()) return 1; audio_ready = 0; return 0; }
static BOOL HP_Init(void) { int flags; if (!(md_mode&DMODE_16BITS)) { _mm_errno=MMERR_16BIT_ONLY; return 1; } if ((fd=open("/dev/audio",O_WRONLY|O_NDELAY,0))<0) { _mm_errno=MMERR_OPENING_AUDIO; return 1; } if ((flags=fcntl(fd,F_GETFL,0))<0) { _mm_errno=MMERR_NON_BLOCK; return 1; } flags|=O_NDELAY; if (fcntl(fd,F_SETFL,flags)<0) { _mm_errno=MMERR_NON_BLOCK; return 1; } if (ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_LINEAR16BIT)) { _mm_errno=MMERR_HP_SETSAMPLESIZE; return 1; } if (ioctl(fd,AUDIO_SET_SAMPLE_RATE,md_mixfreq)) { _mm_errno=MMERR_HP_SETSPEED; return 1; } if (ioctl(fd,AUDIO_SET_CHANNELS,(md_mode&DMODE_STEREO)?2:1)) { _mm_errno=MMERR_HP_CHANNELS; return 1; } if (ioctl(fd,AUDIO_SET_OUTPUT, headphone?AUDIO_OUT_HEADPHONE:AUDIO_OUT_SPEAKER)) { _mm_errno=MMERR_HP_AUDIO_OUTPUT; return 1; } if (ioctl(fd,AUDIO_SET_TXBUFSIZE,buffersize<<3)) { _mm_errno=MMERR_HP_BUFFERSIZE; return 1; } if (!(audiobuffer=(SBYTE*)_mm_malloc(buffersize))) return 1; return VC_Init(); }
static BOOL WSS_Init(void) { if (!wss_open()) { _mm_errno = MMERR_INVALID_DEVICE; return 1; } /* Adjust mixing frequency according to card capabilities */ md_mixfreq = wss_adjust_freq(md_mixfreq); return VC_Init(); }
static BOOL WIN_Init(void) { WAVEFORMATEX wfe; WORD samplesize; MMRESULT mmr; int n; samplesize=1; if (md_mode&DMODE_STEREO) samplesize<<=1; if (md_mode&DMODE_16BITS) samplesize<<=1; wfe.wFormatTag=WAVE_FORMAT_PCM; wfe.nChannels=md_mode&DMODE_STEREO?2:1; wfe.nSamplesPerSec=md_mixfreq; wfe.nAvgBytesPerSec=md_mixfreq*samplesize; wfe.nBlockAlign=samplesize; wfe.wBitsPerSample=md_mode&DMODE_16BITS?16:8; wfe.cbSize=sizeof(wfe); mmr=waveOutOpen(&hwaveout,WAVE_MAPPER,&wfe,(DWORD)WIN_CallBack,0,CALLBACK_FUNCTION); if (mmr!=MMSYSERR_NOERROR) { _mm_errno=WIN_GetError(mmr); return 1; } buffersize=md_mixfreq*samplesize*BUFFERSIZE/1000; for (n=0;n<NUMBUFFERS;n++) { buffer[n]=_mm_malloc(buffersize); header[n].lpData=buffer[n]; header[n].dwBufferLength=buffersize; mmr=waveOutPrepareHeader(hwaveout,&header[n],sizeof(WAVEHDR)); if (!buffer[n]||mmr!=MMSYSERR_NOERROR) { if (!buffer[n]) _mm_errno=MMERR_OUT_OF_MEMORY; else _mm_errno=WIN_GetError(mmr); return 1; } } md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; buffersout=nextbuffer=0; return VC_Init(); }
static int WAV_Init(void) { #if (MIKMOD_UNIX) if (!MD_Access(filename?filename:FILENAME)) { _mm_errno=MMERR_OPENING_FILE; return 1; } #endif if(!(wavfile=fopen(filename?filename:FILENAME,"wb"))) { _mm_errno=MMERR_OPENING_FILE; return 1; } if(!(wavout=_mm_new_file_writer (wavfile))) { fclose(wavfile);unlink(filename?filename:FILENAME); wavfile=NULL; return 1; } if(!(audiobuffer=(SBYTE*)MikMod_malloc(BUFFERSIZE))) { _mm_delete_file_writer(wavout); fclose(wavfile);unlink(filename?filename:FILENAME); wavfile=NULL;wavout=NULL; return 1; } md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; if (VC_Init()) { _mm_delete_file_writer(wavout); fclose(wavfile);unlink(filename?filename:FILENAME); wavfile=NULL;wavout=NULL; return 1; } dumpsize=0; putheader(); return 0; }
static BOOL ALSA_Init_internal(void) { snd_pcm_format_t pformat; int mask,card; /* adjust user-configurable settings */ if((getenv("MM_NUMFRAGS"))&&(numfrags==DEFAULT_NUMFRAGS)) { numfrags=atoi(getenv("MM_NUMFRAGS")); if ((numfrags<2)||(numfrags>16)) numfrags=DEFAULT_NUMFRAGS; } if((getenv("ALSA_CARD"))&&(!cardmin)&&(cardmax==SND_CARDS)) { cardmin=atoi(getenv("ALSA_CARD")); cardmax=cardmin+1; if(getenv("ALSA_PCM")) device=atoi(getenv("ALSA_PCM")); } /* setup playback format structure */ memset(&pformat,0,sizeof(pformat)); #ifdef SND_LITTLE_ENDIAN pformat.format=(md_mode&DMODE_16BITS)?SND_PCM_SFMT_S16_LE:SND_PCM_SFMT_U8; #else pformat.format=(md_mode&DMODE_16BITS)?SND_PCM_SFMT_S16_BE:SND_PCM_SFMT_U8; #endif pformat.channels=(md_mode&DMODE_STEREO)?2:1; pformat.rate=md_mixfreq; /* scan for appropriate sound card */ mask=alsa_cards_mask(); _mm_errno=MMERR_OPENING_AUDIO; for (card=cardmin;card<cardmax;card++) { struct snd_ctl_hw_info info; snd_ctl_t *ctl_h; int dev,devmin,devmax; /* no card here, onto the next */ if (!(mask&(1<<card))) continue; /* try to open the card in query mode */ if(alsa_ctl_open(&ctl_h,card)<0) continue; /* get hardware information */ if(alsa_ctl_hw_info(ctl_h,&info)<0) { alsa_ctl_close(ctl_h); continue; } /* scan subdevices */ if(device==-1) { devmin=0;devmax=info.pcmdevs; } else devmin=devmax=device; for(dev=devmin;dev<devmax;dev++) { snd_pcm_info_t pcminfo; snd_pcm_playback_info_t ctlinfo; struct snd_pcm_playback_info pinfo; struct snd_pcm_playback_params pparams; int size,bps; /* get PCM capabilities */ if(alsa_ctl_pcm_info(ctl_h,dev,&pcminfo)<0) continue; /* look for playback capability */ if(!(pcminfo.flags&SND_PCM_INFO_PLAYBACK)) continue; /* get playback information */ #if defined(SND_LIB_VERSION) && (SND_LIB_VERSION >= 0x400) if(alsa_ctl_pcm_playback_info(ctl_h,dev,0,&ctlinfo)<0) continue; #else if(alsa_ctl_pcm_playback_info(ctl_h,dev,&ctlinfo)<0) continue; #endif /* If control goes here, we have found a sound device able to play PCM data. Let's open in in playback mode and see if we have compatible playback settings. */ if (alsa_pcm_open(&pcm_h,card,dev,SND_PCM_OPEN_PLAYBACK)<0) continue; if (alsa_pcm_playback_info(pcm_h,&pinfo)<0) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } /* check we have compatible settings */ if((pinfo.min_rate>pformat.rate)||(pinfo.max_rate<pformat.rate)|| (!(pinfo.formats&(1<<pformat.format)))) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } fragmentsize=pinfo.buffer_size/numfrags; #ifdef MIKMOD_DEBUG if ((fragmentsize<512)||(fragmentsize>16777216L)) fprintf(stderr,"\rweird pinfo.buffer_size:%d\n",pinfo.buffer_size); #endif alsa_pcm_flush_playback(pcm_h); /* set new parameters */ if(alsa_pcm_playback_format(pcm_h,&pformat)<0) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } /* compute a fragmentsize hint each fragment should be shorter than, but close to, half a second of playback */ bps=(pformat.rate*pformat.channels*(md_mode&DMODE_16BITS?2:1))>>1; size=fragmentsize;while (size>bps) size>>=1; #ifdef MIKMOD_DEBUG if (size < 16) { fprintf(stderr,"\rweird hint result:%d from %d, bps=%d\n",size,fragmentsize,bps); size=16; } #endif memset(&pparams,0,sizeof(pparams)); pparams.fragment_size=size; pparams.fragments_max=-1; /* choose the best */ pparams.fragments_room=-1; if(alsa_pcm_playback_params(pcm_h,&pparams)<0) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } if (!(audiobuffer=(SBYTE*)MikMod_malloc(fragmentsize))) { alsa_ctl_close(ctl_h); return 1; } /* sound device is ready to work */ if (VC_Init()) { alsa_ctl_close(ctl_h); return 1; } else return 0; } alsa_ctl_close(ctl_h); } return 0; }
static int ALSA_Init_internal(void) { snd_pcm_format_t pformat; unsigned int btime = 250000; /* 250ms */ unsigned int ptime = 50000; /* 50ms */ snd_pcm_uframes_t psize; snd_pcm_uframes_t bsize; unsigned int rate, channels; snd_pcm_hw_params_t * hwparams; int err; /* setup playback format structure */ pformat = (md_mode&DMODE_FLOAT)? SND_PCM_FORMAT_FLOAT : (md_mode&DMODE_16BITS)? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U8; channels = (md_mode&DMODE_STEREO)?2:1; rate = md_mixfreq; #define MIKMOD_ALSA_DEVICE "default" if ((err = alsa_pcm_open(&pcm_h, MIKMOD_ALSA_DEVICE, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) { _mm_errno = MMERR_OPENING_AUDIO; goto END; } snd_pcm_hw_params_alloca(&hwparams); err = alsa_pcm_hw_params_any(pcm_h, hwparams); if (err < 0) { _mm_errno = MMERR_ALSA_NOCONFIG; goto END; } err = alsa_pcm_hw_params_set_access(pcm_h, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); if (!err) err = alsa_pcm_hw_params_set_format(pcm_h, hwparams, pformat); if (!err) err = alsa_pcm_hw_params_set_rate_near(pcm_h, hwparams, &rate, NULL); if (!err) err = alsa_pcm_hw_params_set_channels_near(pcm_h, hwparams, &channels); if (!err) err = alsa_pcm_hw_params_set_buffer_time_near(pcm_h, hwparams, &btime, NULL); if (!err) err = alsa_pcm_hw_params_set_period_time_near(pcm_h, hwparams, &ptime, NULL); if (!err) err = alsa_pcm_hw_params(pcm_h, hwparams); if (err < 0) { _mm_errno = MMERR_ALSA_SETPARAMS; goto END; } if (rate != md_mixfreq) { _mm_errno = MMERR_ALSA_SETRATE; goto END; } if (!(md_mode&DMODE_STEREO) && channels != 1) { _mm_errno = MMERR_ALSA_SETCHANNELS; goto END; } if ((md_mode&DMODE_STEREO) && channels != 2) { _mm_errno = MMERR_ALSA_SETCHANNELS; goto END; } err = alsa_pcm_hw_params_current(pcm_h, hwparams); if (!err) err = alsa_pcm_hw_params_get_buffer_size(hwparams, &bsize); if (!err) err = alsa_pcm_hw_params_get_period_size(hwparams, &psize, NULL); if (err < 0) { _mm_errno = MMERR_ALSA_BUFFERSIZE; goto END; } period_size = psize; global_frame_size = channels * ((md_mode&DMODE_FLOAT)? 4 : (md_mode&DMODE_16BITS)? 2 : 1); if (!(audiobuffer=(SBYTE*)MikMod_malloc(period_size * global_frame_size))) { _mm_errno = MMERR_OUT_OF_MEMORY; goto END; } /* sound device is ready to work */ if (!VC_Init()) { enabled = 1; return 0; } END: alsa_pcm_close(pcm_h); pcm_h = NULL; return 1; }
static BOOL ANDROID_Reset(void) { VC_Exit(); return VC_Init(); }
static BOOL DC_Init(void) { dcmikmod_status = INP_DECODE_ERROR; return VC_Init(); }
static BOOL WSS_Reset(void) { wss_reset(); VC_Exit(); return VC_Init(); }
static BOOL stdout_Reset(void) { VC_Exit(); return VC_Init(); }
static BOOL OSLES_Init(void) { short samplesize; int n; samplesize=1; if (md_mode&DMODE_STEREO) samplesize<<=1; if (md_mode&DMODE_16BITS) samplesize<<=1; SLresult result; // create engine result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); if(SL_RESULT_SUCCESS != result) return 1; // realize the engine result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); if(SL_RESULT_SUCCESS != result) return 1; // get the engine interface, which is needed in order to create other objects result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); if(SL_RESULT_SUCCESS != result) return 1; // create output mix, with environmental reverb specified as a non-required interface const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; const SLboolean req[1] = {SL_BOOLEAN_FALSE}; result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, ids, req); if(SL_RESULT_SUCCESS != result) return 1; // realize the output mix result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); if(SL_RESULT_SUCCESS != result) return 1; /* // get the environmental reverb interface // this could fail if the environmental reverb effect is not available, // either because the feature is not present, excessive CPU load, or // the required MODIFY_AUDIO_SETTINGS permission was not requested and granted result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, &outputMixEnvironmentalReverb); if (SL_RESULT_SUCCESS == result) { result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties( outputMixEnvironmentalReverb, &reverbSettings); } */ createBufferQueueAudioPlayer(); buffersize=md_mixfreq*samplesize*BUFFERSIZE/1000; for (n=0;n<NUMBUFFERS;n++) { buffer[n]=_mm_malloc(buffersize); if (!buffer[n]) { _mm_errno=MMERR_OUT_OF_MEMORY; return 1; } } md_mode|=DMODE_SOFT_MUSIC|DMODE_SOFT_SNDFX; buffersout=nextbuffer=0; return VC_Init(); }
static BOOL mod_mpd_Init() { return VC_Init(); }
static BOOL DS_Init(void) { DSBUFFERDESC soundBufferFormat; WAVEFORMATEX pcmwf; DSBPOSITIONNOTIFY positionNotifications[2]; DWORD updateBufferThreadID; if (DirectSoundCreate(NULL,&pSoundCard,NULL)!=DS_OK) { _mm_errno=MMERR_OPENING_AUDIO; return 1; } if (pSoundCard->lpVtbl->SetCooperativeLevel (pSoundCard,GetForegroundWindow(),DSSCL_PRIORITY)!=DS_OK) { _mm_errno=MMERR_DS_PRIORITY; return 1; } memset(&soundBufferFormat,0,sizeof(DSBUFFERDESC)); soundBufferFormat.dwSize =sizeof(DSBUFFERDESC); soundBufferFormat.dwFlags =DSBCAPS_PRIMARYBUFFER; soundBufferFormat.dwBufferBytes=0; soundBufferFormat.lpwfxFormat =NULL; if (pSoundCard->lpVtbl->CreateSoundBuffer (pSoundCard,&soundBufferFormat,&pPrimarySoundBuffer,NULL)!=DS_OK) { _mm_errno=MMERR_DS_BUFFER; return 1; } memset(&pcmwf,0,sizeof(WAVEFORMATEX)); pcmwf.wFormatTag =WAVE_FORMAT_PCM; pcmwf.nChannels =(md_mode&DMODE_STEREO)?2:1; pcmwf.nSamplesPerSec =md_mixfreq; pcmwf.wBitsPerSample =(md_mode&DMODE_16BITS)?16:8; pcmwf.nBlockAlign =(pcmwf.wBitsPerSample * pcmwf.nChannels) / 8; pcmwf.nAvgBytesPerSec=pcmwf.nSamplesPerSec*pcmwf.nBlockAlign; if (pPrimarySoundBuffer->lpVtbl->SetFormat (pPrimarySoundBuffer,&pcmwf)!=DS_OK) { _mm_errno=MMERR_DS_FORMAT; return 1; } pPrimarySoundBuffer->lpVtbl->Play(pPrimarySoundBuffer,0,0,DSBPLAY_LOOPING); memset(&soundBufferFormat,0,sizeof(DSBUFFERDESC)); soundBufferFormat.dwSize =sizeof(DSBUFFERDESC); soundBufferFormat.dwFlags =controlflags|DSBCAPS_GETCURRENTPOSITION2 ; soundBufferFormat.dwBufferBytes=fragsize*UPDATES; soundBufferFormat.lpwfxFormat =&pcmwf; if (pSoundCard->lpVtbl->CreateSoundBuffer (pSoundCard,&soundBufferFormat,&pSoundBuffer,NULL)!=DS_OK) { _mm_errno=MMERR_DS_BUFFER; return 1; } pSoundBuffer->lpVtbl->QueryInterface (pSoundBuffer,&IID_IDirectSoundNotify,(LPVOID*)&pSoundBufferNotify); if (!pSoundBufferNotify) { _mm_errno=MMERR_DS_NOTIFY; return 1; } notifyUpdateHandle=CreateEvent (NULL,FALSE,FALSE,"libmikmod DirectSound Driver positionNotify Event"); if (!notifyUpdateHandle) { _mm_errno=MMERR_DS_EVENT; return 1; } updateBufferHandle=CreateThread (NULL,0,updateBufferProc,NULL,CREATE_SUSPENDED,&updateBufferThreadID); if (!updateBufferHandle) { _mm_errno=MMERR_DS_THREAD; return 1; } memset(positionNotifications,0,2*sizeof(DSBPOSITIONNOTIFY)); positionNotifications[0].dwOffset =0; positionNotifications[0].hEventNotify=notifyUpdateHandle; positionNotifications[1].dwOffset =fragsize; positionNotifications[1].hEventNotify=notifyUpdateHandle; if (pSoundBufferNotify->lpVtbl->SetNotificationPositions (pSoundBufferNotify,2,positionNotifications) != DS_OK) { _mm_errno=MMERR_DS_UPDATE; return 1; } return VC_Init(); }
static BOOL ALSA_Init_internal(void) { snd_pcm_format_t pformat; #ifdef OLD_ALSA #define channels pformat.channels #define rate pformat.rate int mask,card; #else int rate; int channels; int err; snd_pcm_hw_params_t * hwparams; snd_pcm_sw_params_t * swparams; #endif /* adjust user-configurable settings */ if((getenv("MM_NUMFRAGS"))&&(numfrags==DEFAULT_NUMFRAGS)) { numfrags=atoi(getenv("MM_NUMFRAGS")); if ((numfrags<2)||(numfrags>16)) numfrags=DEFAULT_NUMFRAGS; } #ifdef OLD_ALSA if((getenv("ALSA_CARD"))&&(!cardmin)&&(cardmax==SND_CARDS)) { cardmin=atoi(getenv("ALSA_CARD")); cardmax=cardmin+1; #endif if(getenv("ALSA_PCM")) device=atoi(getenv("ALSA_PCM")); #ifdef OLD_ALSA } #endif /* setup playback format structure */ #define NUM_CHANNELS() ((md_mode&DMODE_STEREO)?2:1) #ifdef OLD_ALSA memset(&pformat,0,sizeof(pformat)); #ifdef SND_LITTLE_ENDIAN pformat.format=(md_mode&DMODE_16BITS)?SND_PCM_SFMT_S16_LE:SND_PCM_SFMT_U8; #else pformat.format=(md_mode&DMODE_16BITS)?SND_PCM_SFMT_S16_BE:SND_PCM_SFMT_U8; #endif #else pformat = (md_mode&DMODE_16BITS)?SND_PCM_FORMAT_S16_LE:SND_PCM_FORMAT_U8; snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); #endif channels = NUM_CHANNELS(); rate=md_mixfreq; /* scan for appropriate sound card */ #ifdef OLD_ALSA mask=alsa_cards_mask(); #endif _mm_errno=MMERR_OPENING_AUDIO; #ifdef OLD_ALSA for (card=cardmin;card<cardmax;card++) #endif { #ifdef OLD_ALSA struct snd_ctl_hw_info info; snd_ctl_t *ctl_h; int dev,devmin,devmax; #endif #ifdef OLD_ALSA /* no card here, onto the next */ if (!(mask&(1<<card))) continue; #endif #ifdef OLD_ALSA /* try to open the card in query mode */ if (alsa_ctl_open(&ctl_h,card)<0) continue; #else if ((err = alsa_pcm_open(&pcm_h, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0)) < 0) { printf("snd_pcm_open() call failed: %s\n", alsa_strerror(err)); goto END; } #endif #ifdef OLD_ALSA /* get hardware information */ if(alsa_ctl_hw_info(ctl_h,&info)<0) { alsa_ctl_close(ctl_h); continue; } /* scan subdevices */ if(device==-1) { devmin=0;devmax=info.pcmdevs; } else devmin=devmax=device; #endif #ifdef OLD_ALSA for(dev=devmin;dev<devmax;dev++) #endif { #ifdef OLD_ALSA int size,bps; snd_pcm_info_t pcminfo; snd_pcm_playback_info_t ctlinfo; struct snd_pcm_playback_info pinfo; struct snd_pcm_playback_params pparams; /* get PCM capabilities */ if(alsa_ctl_pcm_info(ctl_h,dev,&pcminfo)<0) continue; /* look for playback capability */ if(!(pcminfo.flags&SND_PCM_INFO_PLAYBACK)) continue; /* get playback information */ #if defined(SND_LIB_VERSION) && (SND_LIB_VERSION >= 0x400) if(alsa_ctl_pcm_playback_info(ctl_h,dev,0,&ctlinfo)<0) continue; #else if(alsa_ctl_pcm_playback_info(ctl_h,dev,&ctlinfo)<0) continue; #endif /* If control goes here, we have found a sound device able to play PCM data. Let's open in in playback mode and see if we have compatible playback settings. */ if (alsa_pcm_open(&pcm_h,card,dev,SND_PCM_OPEN_PLAYBACK)<0) continue; if (alsa_pcm_playback_info(pcm_h,&pinfo)<0) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } /* check we have compatible settings */ if((pinfo.min_rate>rate)||(pinfo.max_rate<rate)|| (!(pinfo.formats&(1<<pformat.format)))) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } fragmentsize=pinfo.buffer_size/numfrags; #ifdef MIKMOD_DEBUG if ((fragmentsize<512)||(fragmentsize>16777216L)) fprintf(stderr,"\rweird pinfo.buffer_size:%d\n",pinfo.buffer_size); #endif alsa_pcm_flush_playback(pcm_h); /* set new parameters */ if(alsa_pcm_playback_format(pcm_h,&pformat)<0) #else if( alsa_pcm_set_params(pcm_h, pformat, SND_PCM_ACCESS_RW_INTERLEAVED, channels, rate, 1, 500000 /* 0.5sec */ ) < 0) #endif { alsa_pcm_close(pcm_h); pcm_h=NULL; #ifdef OLD_ALSA continue; #else goto END; #endif } global_frame_size = channels*(md_mode&DMODE_16BITS?2:1); #ifdef OLD_ALSA /* compute a fragmentsize hint each fragment should be shorter than, but close to, half a second of playback */ bps=(rate*global_frame_size)>>1; size=fragmentsize;while (size>bps) size>>=1; #endif #ifdef MIKMOD_DEBUG if (size < 16) { fprintf(stderr,"\rweird hint result:%d from %d, bps=%d\n",size,fragmentsize,bps); size=16; } #endif #ifdef OLD_ALSA buffer_size = size; memset(&pparams,0,sizeof(pparams)); pparams.fragment_size=size; pparams.fragments_max=-1; /* choose the best */ pparams.fragments_room=-1; if(alsa_pcm_playback_params(pcm_h,&pparams)<0) { alsa_pcm_close(pcm_h); pcm_h=NULL; continue; } #else /* choose all parameters */ err = alsa_pcm_hw_params_any(pcm_h, hwparams); if (err < 0) { printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err)); goto END; } { snd_pcm_uframes_t temp_u_buffer_size, temp_u_period_size; err = alsa_pcm_get_params(pcm_h, &temp_u_buffer_size, &temp_u_period_size); if (err < 0) { alsa_pcm_close(pcm_h); pcm_h=NULL; printf("Unable to get buffer size for playback: %s\n", alsa_strerror(err)); goto END; } buffer_size_in_frames = 1200; period_size = temp_u_period_size; } /* The set_swparams function was taken from test/pcm.c * in the alsa-lib distribution*/ if ((err = set_swparams(pcm_h, swparams)) < 0) { printf("Setting of swparams failed: %s\n", snd_strerror(err)); goto END; } #endif if (!(audiobuffer=(SBYTE*)MikMod_malloc( #ifdef OLD_ALSA fragmentsize #else buffer_size_in_frames * global_frame_size #endif ))) { #ifdef OLD_ALSA alsa_ctl_close(ctl_h); #else alsa_pcm_close(pcm_h); #endif return 1; } /* sound device is ready to work */ if (VC_Init()) { #ifdef OLD_ALSA alsa_ctl_close(ctl_h); #else alsa_pcm_close(pcm_h); #endif return 1; } else return 0; } #ifdef OLD_ALSA alsa_ctl_close(ctl_h); #else alsa_pcm_close(pcm_h); #endif } END: return 1; }
static BOOL PSP_Reset(void) { VC_Exit(); return VC_Init(); }
static BOOL NS_Init(void) { zerobuf=(SBYTE*)_mm_malloc(ZEROLEN); return VC_Init(); }
static BOOL Sun_Init(void) { int play_stereo, play_rate; #ifdef SUNOS4 int audiotype; #else audio_device_t audiotype; #endif struct audio_info audioinfo; if (getenv("AUDIODEV")) sndfd = open(getenv("AUDIODEV"), O_WRONLY); else { sndfd = open(SOUNDDEVICE, O_WRONLY); #if defined __NetBSD__ || defined __OpenBSD__ if (sndfd < 0) sndfd = open(SOUNDDEVICE "0", O_WRONLY); #endif } if (sndfd < 0) { _mm_errno = MMERR_OPENING_AUDIO; return 1; } if (!(audiobuffer = (SBYTE *)_mm_malloc(fragsize))) return 1; play_precision = (md_mode & DMODE_16BITS) ? 16 : 8; play_stereo = (md_mode & DMODE_STEREO) ? 2 : 1; play_rate = md_mixfreq; /* attempt to guess the encoding */ play_encoding = -1; if (ioctl(sndfd, AUDIO_GETDEV, &audiotype) < 0) { #ifdef MIKMOD_DEBUG fputs("\rSun driver warning: could not determine audio device type\n", stderr); #endif } else { #if defined SUNOS4 /* SunOS 4 */ switch (audiotype) { case AUDIO_DEV_AMD: /* AMD 79C30 */ /* 8bit mono ulaw 8kHz */ play_rate = md_mixfreq = 8000; md_mode &= ~(DMODE_STEREO | DMODE_16BITS); play_precision = 8; play_stereo = 1; play_encoding = AUDIO_ENCODING_ULAW; break; case AUDIO_DEV_SPEAKERBOX: case AUDIO_DEV_CODEC: /* CS 4231 or DBRI or speaker box */ /* 16bit mono/stereo linear 8kHz - 48kHz */ if (play_precision == 16) play_encoding = AUDIO_ENCODING_LINEAR; /* 8bit mono ulaw 8kHz - 48kHz */ else if (play_precision == 8) { md_mode &= ~(DMODE_STEREO); play_stereo = 1; play_encoding = AUDIO_ENCODING_ULAW; } else { _mm_errno = MMERR_SUN_INIT; return 1; } break; } #elif defined SOLARIS /* Solaris */ if (!strcmp(audiotype.name, "SUNW,am79c30")) { /* AMD 79C30 */ /* 8bit mono ulaw 8kHz */ play_rate = md_mixfreq = 8000; md_mode &= ~(DMODE_STEREO | DMODE_16BITS); play_precision = 8; play_stereo = 1; play_encoding = AUDIO_ENCODING_ULAW; } else if ((!strcmp(audiotype.name, "SUNW,CS4231")) || (!strcmp(audiotype.name, "SUNW,dbri")) || (!strcmp(audiotype.name, "speakerbox"))) { /* CS 4231 or DBRI or speaker box */ /* 16bit mono/stereo linear 8kHz - 48kHz */ if (play_precision == 16) play_encoding = AUDIO_ENCODING_LINEAR; /* 8bit mono ulaw 8kHz - 48kHz */ else if (play_precision == 8) { md_mode &= ~(DMODE_STEREO); play_stereo = 1; play_encoding = AUDIO_ENCODING_ULAW; } else { _mm_errno = MMERR_SUN_INIT; return 1; } } #else /* NetBSD, OpenBSD */ if (!strcmp(audiotype.name, "amd7930")) { /* AMD 79C30 */ /* 8bit mono ulaw 8kHz */ play_rate = md_mixfreq = 8000; md_mode &= ~(DMODE_STEREO | DMODE_16BITS); play_precision = 8; play_stereo = 1; play_encoding = AUDIO_ENCODING_ULAW; } if ((!strcmp(audiotype.name, "Am78C201")) || (!strcmp(audiotype.name, "UltraSound")) ) { /* Gravis UltraSound, AMD Interwave and compatible cards */ /* 16bit stereo linear 44kHz */ play_rate = md_mixfreq = 44100; md_mode |= (DMODE_STEREO | DMODE_16BITS); play_precision = 16; play_stereo = 2; play_encoding = AUDIO_ENCODING_SLINEAR; } #endif } /* Sound devices which were not handled above don't have specific limitations, so try and guess optimal settings */ if (play_encoding == -1) { if ((play_precision == 8) && (play_stereo == 1) && (play_rate <= 8000)) play_encoding = AUDIO_ENCODING_ULAW; else #ifdef SUNOS4 play_encoding = AUDIO_ENCODING_LINEAR; #else play_encoding = (play_precision == 16) ? AUDIO_ENCODING_SLINEAR : AUDIO_ENCODING_ULINEAR; #endif } /* get current audio settings if we want to keep the playback output port */ if (!port) { AUDIO_INITINFO(&audioinfo); if (ioctl(sndfd, AUDIO_GETINFO, &audioinfo) < 0) { _mm_errno = MMERR_SUN_INIT; return 1; } port = audioinfo.play.port; } AUDIO_INITINFO(&audioinfo); audioinfo.play.precision = play_precision; audioinfo.play.channels = play_stereo; audioinfo.play.sample_rate = play_rate; audioinfo.play.encoding = play_encoding; audioinfo.play.port = port; #if defined __NetBSD__ || defined __OpenBSD__ #if defined AUMODE_PLAY_ALL audioinfo.mode = AUMODE_PLAY | AUMODE_PLAY_ALL; #else audioinfo.mode = AUMODE_PLAY; #endif #endif if (ioctl(sndfd, AUDIO_SETINFO, &audioinfo) < 0) { _mm_errno = MMERR_SUN_INIT; return 1; } /* check if our changes were accepted */ if (ioctl(sndfd, AUDIO_GETINFO, &audioinfo) < 0) { _mm_errno = MMERR_SUN_INIT; return 1; } if ((audioinfo.play.precision != play_precision) || (audioinfo.play.channels != play_stereo) || (normalize(audioinfo.play.encoding) != normalize(play_encoding))) { _mm_errno = MMERR_SUN_INIT; return 1; } if (audioinfo.play.sample_rate != play_rate) { /* Accept a shift inferior to 5% of the expected rate */ int delta = audioinfo.play.sample_rate - play_rate; if (delta < 0) delta = -delta; if (delta * 20 > play_rate) { _mm_errno = MMERR_SUN_INIT; return 1; } /* Align to what the card gave us */ md_mixfreq = audioinfo.play.sample_rate; } return VC_Init(); }
static int DS_Init(void) { DSBUFFERDESC soundBufferFormat; WAVEFORMATEX pcmwf; DSBPOSITIONNOTIFY positionNotifications[2]; DWORD updateBufferThreadID; LPVOID p = NULL; if (DirectSoundCreate(NULL,&pSoundCard,NULL)!=DS_OK) { _mm_errno=MMERR_OPENING_AUDIO; return 1; } if (IDirectSound_SetCooperativeLevel (pSoundCard,GetForegroundWindow(),DSSCL_PRIORITY)!=DS_OK) { _mm_errno=MMERR_DS_PRIORITY; return 1; } memset(&soundBufferFormat,0,sizeof(DSBUFFERDESC)); soundBufferFormat.dwSize = sizeof(DSBUFFERDESC); soundBufferFormat.dwFlags = DSBCAPS_PRIMARYBUFFER; soundBufferFormat.dwBufferBytes = 0; soundBufferFormat.lpwfxFormat = NULL; if (IDirectSound_CreateSoundBuffer (pSoundCard,&soundBufferFormat,&pPrimarySoundBuffer,NULL)!=DS_OK) { _mm_errno=MMERR_DS_BUFFER; return 1; } memset(&pcmwf,0,sizeof(WAVEFORMATEX)); pcmwf.wFormatTag =(md_mode&DMODE_FLOAT)? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM; pcmwf.nChannels =(md_mode&DMODE_STEREO)?2:1; pcmwf.nSamplesPerSec =md_mixfreq; pcmwf.wBitsPerSample =(md_mode&DMODE_FLOAT)?32:(md_mode&DMODE_16BITS)?16:8; pcmwf.nBlockAlign =(pcmwf.wBitsPerSample * pcmwf.nChannels) / 8; pcmwf.nAvgBytesPerSec=pcmwf.nSamplesPerSec*pcmwf.nBlockAlign; if (IDirectSoundBuffer_SetFormat(pPrimarySoundBuffer,&pcmwf)!=DS_OK) { _mm_errno=MMERR_DS_FORMAT; return 1; } IDirectSoundBuffer_Play(pPrimarySoundBuffer,0,0,DSBPLAY_LOOPING); memset(&soundBufferFormat,0,sizeof(DSBUFFERDESC)); soundBufferFormat.dwSize =sizeof(DSBUFFERDESC); soundBufferFormat.dwFlags =controlflags|DSBCAPS_GETCURRENTPOSITION2 ; soundBufferFormat.dwBufferBytes =fragsize*UPDATES; soundBufferFormat.lpwfxFormat =&pcmwf; if (IDirectSound_CreateSoundBuffer (pSoundCard,&soundBufferFormat,&pSoundBuffer,NULL)!=DS_OK) { _mm_errno=MMERR_DS_BUFFER; return 1; } #ifdef __cplusplus IDirectSoundBuffer_QueryInterface(pSoundBuffer, IID_IDirectSoundNotify,&p); #else IDirectSoundBuffer_QueryInterface(pSoundBuffer,&IID_IDirectSoundNotify,&p); #endif if (!p) { _mm_errno=MMERR_DS_NOTIFY; return 1; } pSoundBufferNotify = (LPDIRECTSOUNDNOTIFY) p; notifyUpdateHandle=CreateEvent (NULL,FALSE,FALSE,"libmikmod DirectSound Driver positionNotify Event"); if (!notifyUpdateHandle) { _mm_errno=MMERR_DS_EVENT; return 1; } updateBufferHandle=CreateThread (NULL,0,updateBufferProc,NULL,CREATE_SUSPENDED,&updateBufferThreadID); if (!updateBufferHandle) { _mm_errno=MMERR_DS_THREAD; return 1; } memset(positionNotifications,0,2*sizeof(DSBPOSITIONNOTIFY)); positionNotifications[0].dwOffset =0; positionNotifications[0].hEventNotify=notifyUpdateHandle; positionNotifications[1].dwOffset =fragsize; positionNotifications[1].hEventNotify=notifyUpdateHandle; if (IDirectSoundNotify_SetNotificationPositions (pSoundBufferNotify,2,positionNotifications) != DS_OK) { _mm_errno=MMERR_DS_UPDATE; return 1; } #if defined HAVE_SSE2 /* this test only works on Windows XP or later */ if (IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE)) { md_mode|=DMODE_SIMDMIXER; } #endif return VC_Init(); }
static int XAudio2_Init(void) { UINT32 flags; DWORD thread_id; WAVEFORMATEX wfmt; memset(&wfmt, 0, sizeof(WAVEFORMATEX)); wfmt.wFormatTag= (md_mode & DMODE_FLOAT)? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM; wfmt.nChannels = (md_mode & DMODE_STEREO)? 2: 1; wfmt.nSamplesPerSec = md_mixfreq; wfmt.wBitsPerSample = (md_mode & DMODE_FLOAT)? 32: (md_mode & DMODE_16BITS)? 16: 8; wfmt.nBlockAlign = (wfmt.wBitsPerSample * wfmt.nChannels) / 8; wfmt.nAvgBytesPerSec = wfmt.nSamplesPerSec * wfmt.nBlockAlign; if (wfmt.nSamplesPerSec < XAUDIO2_MIN_SAMPLE_RATE || wfmt.nSamplesPerSec > XAUDIO2_MAX_SAMPLE_RATE || wfmt.nChannels > XAUDIO2_MAX_AUDIO_CHANNELS) { return 1; } current_buf = 0; flags = 0; #if defined(_DEBUG) && !defined(DRV_XAUDIO28) /* flags |= XAUDIO2_DEBUG_ENGINE;*/ #endif #ifndef _XBOX CoInitializeEx(NULL, COINIT_MULTITHREADED); #endif if (FAILED(XAudio2Create(&pXAudio2, flags, XAUDIO2_DEFAULT_PROCESSOR))) { goto fail; } #if defined(DRV_XAUDIO28) if (FAILED(IXAudio2_CreateMasteringVoice(pXAudio2, &pMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, NULL, NULL, AudioCategory_Other))) { goto fail; } #else if (FAILED(IXAudio2_CreateMasteringVoice(pXAudio2, &pMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, NULL))) { goto fail; } #endif if (FAILED(IXAudio2_CreateSourceVoice(pXAudio2, &pSourceVoice, &wfmt, 0, 1.0f, pcbVoice, NULL, NULL))) { goto fail; } #ifndef __cplusplus if ((hBufferEvent = CreateEvent(NULL, FALSE, FALSE, "libmikmod XAudio2 Driver buffer Event")) == NULL) { goto fail; } #endif if ((UpdateBufferHandle = CreateThread(NULL, 0, UpdateBufferProc, NULL, CREATE_SUSPENDED, &thread_id)) == NULL) { goto fail; } #if defined HAVE_SSE2 /* this test only works on Windows XP or later */ if (IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE)) { md_mode|=DMODE_SIMDMIXER; } #endif return VC_Init(); fail: if (pSourceVoice) { IXAudio2SourceVoice_DestroyVoice(pSourceVoice); pSourceVoice = NULL; } if (pMasterVoice) { IXAudio2MasteringVoice_DestroyVoice(pMasterVoice); pMasterVoice = NULL; } if (pXAudio2) { IXAudio2_Release(pXAudio2); pXAudio2 = NULL; } #ifndef _XBOX CoUninitialize(); #endif return 1; }