static qboolean S_MODPLUG_CodecOpenStream (snd_stream_t *stream) { /* need to load the whole file into memory and pass it to libmodplug */ byte *moddata; long len; int mark; len = FS_filelength (&stream->fh); mark = Hunk_LowMark(); moddata = (byte *) Hunk_Alloc(len); FS_fread(moddata, 1, len, &stream->fh); S_MODPLUG_SetSettings(stream); stream->priv = ModPlug_Load(moddata, len); Hunk_FreeToLowMark(mark); /* free original file data */ if (!stream->priv) { Con_DPrintf("Could not load module %s\n", stream->name); return false; } ModPlug_Seek((ModPlugFile*)stream->priv, 0); #if 0 /* default volume (128) sounds rather low? */ ModPlug_SetMasterVolume((ModPlugFile*)stream->priv, 384); /* 0-512 */ #endif return true; }
/* (Re)fill the stream buffer that is to be decoded. If any data * still exists in the buffer then they are first shifted to be * front of the stream buffer. */ static int mp3_inputdata(snd_stream_t *stream) { mp3_priv_t *p = (mp3_priv_t *) stream->priv; size_t bytes_read; size_t remaining; remaining = p->Stream.bufend - p->Stream.next_frame; /* libmad does not consume all the buffer it's given. Some * data, part of a truncated frame, is left unused at the * end of the buffer. That data must be put back at the * beginning of the buffer and taken in account for * refilling the buffer. This means that the input buffer * must be large enough to hold a complete frame at the * highest observable bit-rate (currently 448 kb/s). * TODO: Is 2016 bytes the size of the largest frame? * (448000*(1152/32000))/8 */ memmove(p->mp3_buffer, p->Stream.next_frame, remaining); bytes_read = FS_fread(p->mp3_buffer + remaining, 1, MP3_BUFFER_SIZE - remaining, &stream->fh); if (bytes_read == 0) { return -1; } mad_stream_buffer(&p->Stream, p->mp3_buffer, bytes_read+remaining); p->Stream.error = MAD_ERROR_NONE; return 0; }
static ssize_t mp3_read (void *f, void *buf, size_t size) { ssize_t ret = (ssize_t) FS_fread(buf, 1, size, (fshandle_t *)f); if (ret == 0 && errno != 0) ret = -1; return ret; }
static qboolean S_XMP_CodecOpenStream (snd_stream_t *stream) { /* need to load the whole file into memory and pass it to libxmp * using xmp_load_module_from_memory() which requires libxmp >= 4.2. * libxmp-4.0/4.1 only have xmp_load_module() which accepts a file * name which isn't good with files in containers like paks, etc. */ xmp_context c; byte *moddata; long len; int mark; c = xmp_create_context(); if (c == NULL) return false; len = FS_filelength (&stream->fh); mark = Hunk_LowMark(); moddata = (byte *) Hunk_Alloc(len); FS_fread(moddata, 1, len, &stream->fh); if (xmp_load_module_from_memory(c, moddata, len) != 0) { Con_DPrintf("Could not load module %s\n", stream->name); goto err1; } Hunk_FreeToLowMark(mark); /* free original file data */ stream->priv = c; if (shm->speed > XMP_MAX_SRATE) stream->info.rate = 44100; else if (shm->speed < XMP_MIN_SRATE) stream->info.rate = 11025; else stream->info.rate = shm->speed; stream->info.bits = shm->samplebits; stream->info.width = stream->info.bits / 8; stream->info.channels = shm->channels; if (S_XMP_StartPlay(stream) != 0) goto err2; /* percentual left/right channel separation, default is 70. */ if (stream->info.channels == 2) if (xmp_set_player(c, XMP_PLAYER_MIX, 100) != 0) goto err3; /* interpolation type, default is XMP_INTERP_LINEAR */ if (xmp_set_player(c, XMP_PLAYER_INTERP, XMP_INTERP_SPLINE) != 0) goto err3; return true; err3: xmp_end_player(c); err2: xmp_release_module(c); err1: xmp_free_context(c); return false; }
static int opc_fread (void *f, unsigned char *buf, int size) { int ret; if (size < 0) { errno = EINVAL; return -1; } ret = (int) FS_fread(buf, 1, (size_t)size, (fshandle_t *)f); if (ret == 0 && errno != 0) ret = -1; return ret; }
/* ************************************************************************************************************ * * function * * 函数名称: * * 参数列表: * * 返回值 : * * 说明 : * * ************************************************************************************************************ */ __s32 eGon2_driver_install(const char *pstr) { void *paddr; H_FILE pfile; __s32 ret; __u32 length; pfile = FS_fopen(pstr, "r+"); if(!pfile) { eGon2_printf("can't find %s\n", pstr); return -1; } //获取文件长度 length = FS_filelen(pfile); if(!length) { eGon2_printf("error: file %s length is 0\n", pstr); FS_fclose(pfile); return -1; } //根据文件长度获取内存 paddr = eGon2_malloc(length); if(!paddr) { eGon2_printf("unable to malloc memory for install driver\n"); FS_fclose(pfile); return -1; } if(!FS_fread(paddr, length, 1, pfile)) { eGon2_printf("read %s fail\n", pstr); FS_fclose(pfile); eGon2_free(paddr); return -1; } FS_fclose(pfile); ret = elf_loader(paddr, 0); eGon2_free(paddr); return ret; }
static void MID2STREAM_readfile (void *buffer, DWORD bytes_wanted, DWORD *bytes_read) { size_t nmemb = FS_fread (buffer, 1, bytes_wanted, &midi_fh); *bytes_read = nmemb; }
static int mp3_startread(snd_stream_t *stream) { mp3_priv_t *p = (mp3_priv_t *) stream->priv; size_t ReadSize; mad_stream_init(&p->Stream); mad_frame_init(&p->Frame); mad_synth_init(&p->Synth); mad_timer_reset(&p->Timer); /* Decode at least one valid frame to find out the input * format. The decoded frame will be saved off so that it * can be processed later. */ ReadSize = FS_fread(p->mp3_buffer, 1, MP3_BUFFER_SIZE, &stream->fh); if (ReadSize != MP3_BUFFER_SIZE) { if (FS_feof(&stream->fh) || FS_ferror(&stream->fh)) return -1; } mad_stream_buffer(&p->Stream, p->mp3_buffer, ReadSize); /* Find a valid frame before starting up. This makes sure * that we have a valid MP3 and also skips past ID3v2 tags * at the beginning of the audio file. */ p->Stream.error = MAD_ERROR_NONE; while (mad_frame_decode(&p->Frame,&p->Stream)) { /* check whether input buffer needs a refill */ if (p->Stream.error == MAD_ERROR_BUFLEN) { if (mp3_inputdata(stream) == -1) return -1; continue; } /* Consume any ID3 tags */ mp3_inputtag(stream); /* FIXME: We should probably detect when we've read * a bunch of non-ID3 data and still haven't found a * frame. In that case we can abort early without * scanning the whole file. */ p->Stream.error = MAD_ERROR_NONE; } if (p->Stream.error) { Con_Printf("MP3: No valid MP3 frame found\n"); return -1; } switch(p->Frame.header.mode) { case MAD_MODE_SINGLE_CHANNEL: case MAD_MODE_DUAL_CHANNEL: case MAD_MODE_JOINT_STEREO: case MAD_MODE_STEREO: stream->info.channels = MAD_NCHANNELS(&p->Frame.header); break; default: Con_Printf("MP3: Cannot determine number of channels\n"); return -1; } p->FrameCount = 1; mad_timer_add(&p->Timer,p->Frame.header.duration); mad_synth_frame(&p->Synth,&p->Frame); stream->info.width = MP3_MAD_SAMPLEWIDTH; stream->info.rate = p->Synth.pcm.samplerate; p->cursamp = 0; return 0; }
__s32 eGon2_run_app(__s32 argc, char **argv) { void *paddr; H_FILE pfile; __u32 entry; app_func func; __s32 ret; __u32 length; if(argc <= 0) { return -1; } //打开文件 pfile = FS_fopen(&argv[0][0], "r+"); if(!pfile) { eGon2_printf("can't find %s\n", argv[0]); return -1; } //获取文件长度 length = FS_filelen(pfile); if(!length) { eGon2_printf("error: file %s length is 0\n", argv[0]); FS_fclose(pfile); return -1; } paddr = eGon2_malloc(length); if(!paddr) { eGon2_printf("unable to malloc memory for install driver\n"); FS_fclose(pfile); return -1; } if(!FS_fread(paddr, length, 1, pfile)) { eGon2_printf("read %s fail\n", argv[0]); FS_fclose(pfile); eGon2_free(paddr); return -1; } FS_fclose(pfile); ret = elf_loader(paddr, &entry); eGon2_free(paddr); if(ret < 0) { eGon2_printf("elf file %s load fail\n", argv[0]); return -1; } func = (app_func)entry; //刷新cache flush_icache(); flush_dcache(); func(argc, argv); return 0; }
static BOOL MIK_Read (MREADER *r, void *ptr, size_t siz) { return !!FS_fread(ptr, siz, 1, ((mik_priv_t *)r)->fh); }