CHAR *MD_GetAtom(CHAR *atomname,CHAR *cmdline,BOOL implicit) { CHAR *ret=NULL; if(cmdline) { CHAR *buf=strstr(cmdline,atomname); if((buf)&&((buf==cmdline)||(*(buf-1)==','))) { CHAR *ptr=buf+strlen(atomname); if(*ptr=='=') { for(buf=++ptr;(*ptr)&&((*ptr)!=',');ptr++); ret=MikMod_malloc((1+ptr-buf)*sizeof(CHAR)); if(ret) strncpy(ret,buf,ptr-buf); } else if((*ptr==',')||(!*ptr)) { if(implicit) { ret=MikMod_malloc((1+ptr-buf)*sizeof(CHAR)); if(ret) strncpy(ret,buf,ptr-buf); } } } } return ret; }
BOOL S69_Init(void) { if(!(s69pat=(S69NOTE *)MikMod_malloc(64*8*sizeof(S69NOTE)))) return 0; if(!(mh=(S69HEADER *)MikMod_malloc(sizeof(S69HEADER)))) return 0; return 1; }
static BOOL IMF_Init(void) { if(!(imfpat=(IMFNOTE*)MikMod_malloc(32*256*sizeof(IMFNOTE)))) return 0; if(!(mh=(IMFHEADER*)MikMod_malloc(sizeof(IMFHEADER)))) return 0; return 1; }
static BOOL FAR_Init(void) { if(!(mh1 = (FARHEADER1*)MikMod_malloc(sizeof(FARHEADER1)))) return 0; if(!(mh2 = (FARHEADER2*)MikMod_malloc(sizeof(FARHEADER2)))) return 0; if(!(pat = (FARNOTE*)MikMod_malloc(256*16*4*sizeof(FARNOTE)))) return 0; return 1; }
static BOOL STX_Init(void) { if(!(stxbuf=(STXNOTE*)MikMod_malloc(4*64*sizeof(STXNOTE)))) return 0; if(!(mh=(STXHEADER*)MikMod_malloc(sizeof(STXHEADER)))) return 0; if(!(poslookup=(UBYTE*)MikMod_malloc(sizeof(UBYTE)*256))) return 0; memset(poslookup,-1,256); return 1; }
BOOL S3M_Init(void) { if(!(s3mbuf=(S3MNOTE*)MikMod_malloc(32*64*sizeof(S3MNOTE)))) return 0; if(!(mh=(S3MHEADER*)MikMod_malloc(sizeof(S3MHEADER)))) return 0; if(!(poslookup=(UBYTE*)MikMod_malloc(sizeof(UBYTE)*256))) return 0; memset(poslookup,-1,256); return 1; }
int IT_Init(void) { if(!(mh=(ITHEADER*)MikMod_malloc(sizeof(ITHEADER)))) return 0; if(!(poslookup=(UBYTE*)MikMod_malloc(256*sizeof(UBYTE)))) return 0; if(!(itpat=(ITNOTE*)MikMod_malloc(200*64*sizeof(ITNOTE)))) return 0; if(!(mask=(UBYTE*)MikMod_malloc(64*sizeof(UBYTE)))) return 0; if(!(last=(ITNOTE*)MikMod_malloc(64*sizeof(ITNOTE)))) return 0; return 1; }
static int MTM_Init(void) { if(!(mtmtrk=(MTMNOTE*)MikMod_calloc(64,sizeof(MTMNOTE)))) return 0; if(!(mh=(MTMHEADER*)MikMod_malloc(sizeof(MTMHEADER)))) return 0; return 1; }
static BOOL AMF_Init(void) { if(!(mh=(AMFHEADER*)MikMod_malloc(sizeof(AMFHEADER)))) return 0; if(!(track=(AMFNOTE*)MikMod_calloc(64,sizeof(AMFNOTE)))) return 0; return 1; }
CHAR *StrDup(CHAR *s) { size_t l = strlen(s) + 1; CHAR *d = MikMod_malloc(l); strcpy(d, s); return d; }
MIKMODAPI CHAR* MikMod_InfoLoader(void) { int len=0; MLOADER *l; CHAR *list=NULL; MUTEX_LOCK(lists); /* compute size of buffer */ for(l=firstloader;l;l=l->next) len+=1+(l->next?1:0)+strlen(l->version); if(len) if((list=MikMod_malloc(len*sizeof(CHAR)))) { CHAR * list_end = list; list[0]=0; /* list all registered module loders */ for(l=firstloader;l;l=l->next) { list_end += sprintf(list_end, "%s%s",l->version, ((l->next) ? "\n" : "") ); } } MUTEX_UNLOCK(lists); return list; }
/* Creates a CSTR out of a character buffer of 'len' bytes, but strips any terminating non-printing characters like 0, spaces etc. */ CHAR *DupStr(CHAR* s,UWORD len,BOOL strict) { UWORD t; CHAR *d=NULL; /* Scan for last printing char in buffer [includes high ascii up to 254] */ while(len) { if(s[len-1]>0x20) break; len--; } /* Scan forward for possible NULL character */ if(strict) { for(t=0;t<len;t++) if (!s[t]) break; if (t<len) len=t; } /* When the buffer wasn't completely empty, allocate a cstring and copy the buffer into that string, except for any control-chars */ if((d=(CHAR*)MikMod_malloc(sizeof(CHAR)*(len+1)))) { for(t=0;t<len;t++) d[t]=(s[t]<32)?'.':s[t]; d[len]=0; } return d; }
static int stdout_Init(void) { if(!(audiobuffer=(SBYTE*)MikMod_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 ReadLinedComment(UWORD len,UWORD linelen) { CHAR *tempcomment,*line,*storage; UWORD total=0,t,lines; int i; lines = (len + linelen - 1) / linelen; if (len) { if(!(tempcomment=(CHAR*)MikMod_malloc(len+1))) return 0; if(!(storage=(CHAR*)MikMod_malloc(linelen+1))) { MikMod_free(tempcomment); return 0; } memset(tempcomment, ' ', len); _mm_read_UBYTES(tempcomment,len,modreader); /* compute message length */ for(line=tempcomment,total=t=0;t<lines;t++,line+=linelen) { for(i=linelen;(i>=0)&&(line[i]==' ');i--) line[i]=0; for(i=0;i<linelen;i++) if (!line[i]) break; total+=1+i; } if(total>lines) { if(!(of.comment=(CHAR*)MikMod_malloc(total+1))) { MikMod_free(storage); MikMod_free(tempcomment); return 0; } /* convert message */ for(line=tempcomment,t=0;t<lines;t++,line+=linelen) { for(i=0;i<linelen;i++) if(!(storage[i]=line[i])) break; storage[i]=0; /* if (i==linelen) */ strcat(of.comment,storage);strcat(of.comment,"\r"); } MikMod_free(storage); MikMod_free(tempcomment); } } return 1; }
MWRITER *_mm_new_file_writer(FILE* fp) { MFILEWRITER* writer=(MFILEWRITER*)MikMod_malloc(sizeof(MFILEWRITER)); if (writer) { writer->core.Seek =&_mm_FileWriter_Seek; writer->core.Tell =&_mm_FileWriter_Tell; writer->core.Write=&_mm_FileWriter_Write; writer->core.Put =&_mm_FileWriter_Put; writer->file=fp; } return (MWRITER*) writer; }
MREADER *_mm_new_file_reader(FILE* fp) { MFILEREADER* reader=(MFILEREADER*)MikMod_malloc(sizeof(MFILEREADER)); if (reader) { reader->core.Eof =&_mm_FileReader_Eof; reader->core.Read=&_mm_FileReader_Read; reader->core.Get =&_mm_FileReader_Get; reader->core.Seek=&_mm_FileReader_Seek; reader->core.Tell=&_mm_FileReader_Tell; reader->file=fp; } return (MREADER*)reader; }
static char * readstring(void) { char *str=NULL; UWORD len; len=_mm_read_I_UWORD(modreader); if(len) { str=(char *) MikMod_malloc(len+1); _mm_read_UBYTES(str,len,modreader); str[len]=0; } return str; }
static char* readstring(void) { char *s=NULL; UWORD len; len=_mm_read_I_UWORD(modreader); if(len) { s=MikMod_malloc(len+1); _mm_read_UBYTES(s,len,modreader); s[len]=0; } return s; }
MREADER *_mm_new_mem_reader(const void *buffer, int len) { MMEMREADER* reader=(MMEMREADER*)MikMod_malloc(sizeof(MMEMREADER)); if (reader) { reader->core.Eof =&_mm_MemReader_Eof; reader->core.Read=&_mm_MemReader_Read; reader->core.Get =&_mm_MemReader_Get; reader->core.Seek=&_mm_MemReader_Seek; reader->core.Tell=&_mm_MemReader_Tell; reader->buffer = buffer; reader->len = len; reader->pos = 0; } return (MREADER*)reader; }
BOOL ReadComment(UWORD len) { if(len) { int i; if(!(of.comment=(CHAR*)MikMod_malloc(len+1))) return 0; _mm_read_UBYTES(of.comment,len,modreader); /* translate IT linefeeds */ for(i=0;i<len;i++) if(of.comment[i]=='\r') of.comment[i]='\n'; of.comment[len]=0; /* just in case */ } if(!of.comment[0]) { MikMod_free(of.comment); of.comment=NULL; } return 1; }
void* MikMod_realloc(void *data, size_t size) { if (data) { #if defined __MACH__ void *d = realloc(data, size); if (d) { return d; } return 0; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) //return _aligned_realloc(data, size, ALIGN_STRIDE); return realloc(data, size); #else char *newPtr = (char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer(newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); }
void* MikMod_realloc(void *data, size_t size) { if (data) { #if defined __MACH__ || defined __QNXNTO__ void *d = realloc(data, size); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) { _mm_errorhandler(); } } return d; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) return _aligned_realloc(data, size, ALIGN_STRIDE); #else unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer((char*)newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); }
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; }
MIKMODAPI CHAR* MikMod_InfoDriver(void) { int t; size_t len=0; MDRIVER *l; CHAR *list=NULL; MUTEX_LOCK(lists); /* compute size of buffer */ for(l=firstdriver;l;l=l->next) len+=4+(l->next?1:0)+strlen(l->Version); if(len) if((list=MikMod_malloc(len*sizeof(CHAR)))) { list[0]=0; /* list all registered device drivers : */ for(t=1,l=firstdriver;l;l=l->next,t++) sprintf(list,(l->next)?"%s%2d %s\n":"%s%2d %s", list,t,l->Version); } MUTEX_UNLOCK(lists); return list; }
SAMPLE *Load(const char *fn) { char *file_data; long sysFileGetLength; FILE *fptr; /* return Sample_Load(fn);*/ /* open the file */ fptr = fopen(fn, "rb"); if (fptr == NULL) { perror("fopen"); return 0; } /* calculate the file size */ fseek(fptr, 0, SEEK_END); sysFileGetLength = ftell(fptr); fseek(fptr, 0, SEEK_SET); /* allocate a buffer and load the file into it */ file_data = (char *) MikMod_malloc(sysFileGetLength); if (file_data == NULL) { perror("MikMod_malloc"); fclose(fptr); return 0; } if (fread(file_data, sysFileGetLength, 1, fptr) != 1) { perror("fread"); fclose(fptr); MikMod_free(file_data); return 0; } fclose(fptr); return Sample_LoadMem(file_data, sysFileGetLength); }
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 MODULE *ML_AllocUniMod(void) { MODULE *mf; return (mf=MikMod_malloc(sizeof(MODULE))); }
static BOOL NS_Init(void) { zerobuf=(SBYTE*)MikMod_malloc(ZEROLEN); return VC_Init(); }