请问读取MP3文件的函数和读取WAV格式有什么区别呢??

RabbitLBJ 2009-05-21 08:17:47
能给点代码吗?我现在可以用一个函数把WAV文件读入内存,然后用DSOUND播放,不知道可不可以用一个读取MP3的函数把文件读入内存之后也用DSOUND播放呢?
...全文
293 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongpy 2009-05-25
  • 打赏
  • 举报
回复
别的平台,可移植开源库libmad(mp3解码),或者直接用开源软件madplay(mp3解码&播放)。
dongpy 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 RabbitLBJ 的回复:]
windows平台用WINMM.LIB库如何弄?
别的平台又如何弄?
MP3的解析函数比WAV复杂很多吗??
[/Quote]
windows如下:

#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

MCI_OPEN_PARMS m_mciOpen;
int Stop()
{
if (m_mciOpen.wDeviceID)
{
mciSendCommand(m_mciOpen.wDeviceID,MCI_CLOSE,NULL,NULL);
}
return TRUE;
}

int Play()
{
Stop();
m_mciOpen.lpstrDeviceType = _T("mpegvideo");
m_mciOpen.lpstrElementName = _T("a.mp3");
if( mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)&m_mciOpen) )
{
return FALSE;
}

MCI_PLAY_PARMS mciPlay;
if( mciSendCommand(m_mciOpen.wDeviceID, MCI_PLAY, MCI_WAIT, (DWORD)(LPMCI_PLAY_PARMS)&mciPlay) )
{
return FALSE;
}
Stop();
return TRUE;
}
FCARM 2009-05-25
  • 打赏
  • 举报
回复
如果是想播放而,直接调用播放器,
播放器会自己找到解码器的!
FCARM 2009-05-25
  • 打赏
  • 举报
回复
不懂楼主到底是想写解码器还是想播放这些文件啊?
RabbitLBJ 2009-05-25
  • 打赏
  • 举报
回复
windows平台用WINMM.LIB库如何弄?
别的平台又如何弄?
MP3的解析函数比WAV复杂很多吗??
Sky-Yang 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yuanhong2910 的回复:]
引用 8 楼 FCARM 的回复:
MP3要解码才可以播放,
而WAV则不需要解码

而且mp3和Wav的文件格式不一样,解析肯定不一样
[/Quote]

楼上正解
要播放mp3必须对mp3解码
最近在搞嵌入式语音编程,稍有了解,来学习学习
RabbitLBJ 2009-05-25
  • 打赏
  • 举报
回复
谢谢各位,最后一个问题。
那些MP3播放器(比如千千静听之类)都是用15或者16楼的方法写的吗??
dongpy 2009-05-24
  • 打赏
  • 举报
回复
什么平台的?
windows用winmm.lib这个库的API即可。
FCARM 2009-05-23
  • 打赏
  • 举报
回复
MP3要解码才可以播放,
而WAV则不需要解码
RabbitLBJ 2009-05-23
  • 打赏
  • 举报
回复
没人了吗??
yuanhong2910 2009-05-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 FCARM 的回复:]
MP3要解码才可以播放,
而WAV则不需要解码
[/Quote]
而且mp3和Wav的文件格式不一样,解析肯定不一样
RabbitLBJ 2009-05-22
  • 打赏
  • 举报
回复
需要一个类似这个函数的函数!!!

int DSound_Load_WAV(char *filename, int control_flags)
{
// this function loads a .wav file, sets up the directsound
// buffer and loads the data into memory, the function returns
// the id number of the sound


HMMIO hwav; // handle to wave file
MMCKINFO parent, // parent chunk
child; // child chunk
WAVEFORMATEX wfmtx; // wave format structure

int sound_id = -1, // id of sound to be loaded
index; // looping variable

UCHAR *snd_buffer, // temporary sound buffer to hold voc data
*audio_ptr_1=NULL, // data ptr to first write buffer
*audio_ptr_2=NULL; // data ptr to second write buffer

DWORD audio_length_1=0, // length of first write buffer
audio_length_2=0; // length of second write buffer

// step one: are there any open id's ?
for (index=0; index < MAX_SOUNDS; index++)
{
// make sure this sound is unused
if (sound_fx[index].state==SOUND_NULL)
{
sound_id = index;
break;
} // end if

} // end for index

// did we get a free id?
if (sound_id==-1)
return(-1);

// set up chunk info structure
parent.ckid = (FOURCC)0;
parent.cksize = 0;
parent.fccType = (FOURCC)0;
parent.dwDataOffset = 0;
parent.dwFlags = 0;

// copy data
child = parent;

// open the WAV file
if ((hwav = mmioOpen(filename, NULL, MMIO_READ | MMIO_ALLOCBUF))==NULL)
return(-1);

// descend into the RIFF
parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');

if (mmioDescend(hwav, &parent, NULL, MMIO_FINDRIFF))
{
// close the file
mmioClose(hwav, 0);

// return error, no wave section
return(-1);
} // end if

// descend to the WAVEfmt
child.ckid = mmioFOURCC('f', 'm', 't', ' ');

if (mmioDescend(hwav, &child, &parent, 0))
{
// close the file
mmioClose(hwav, 0);

// return error, no format section
return(-1);
} // end if

// now read the wave format information from file
if (mmioRead(hwav, (char *)&wfmtx, sizeof(wfmtx)) != sizeof(wfmtx))
{
// close file
mmioClose(hwav, 0);

// return error, no wave format data
return(-1);
} // end if

// make sure that the data format is PCM
if (wfmtx.wFormatTag != WAVE_FORMAT_PCM)
{
// close the file
mmioClose(hwav, 0);

// return error, not the right data format
return(-1);
} // end if

// now ascend up one level, so we can access data chunk
if (mmioAscend(hwav, &child, 0))
{
// close file
mmioClose(hwav, 0);

// return error, couldn't ascend
return(-1);
} // end if

// descend to the data chunk
child.ckid = mmioFOURCC('d', 'a', 't', 'a');

if (mmioDescend(hwav, &child, &parent, MMIO_FINDCHUNK))
{
// close file
mmioClose(hwav, 0);

// return error, no data
return(-1);
} // end if

// finally!!!! now all we have to do is read the data in and
// set up the directsound buffer

// allocate the memory to load sound data
snd_buffer = (UCHAR *)malloc(child.cksize);

// read the wave data
mmioRead(hwav, (char *)snd_buffer, child.cksize);

// close the file
mmioClose(hwav, 0);

// set rate and size in data structure
sound_fx[sound_id].rate = wfmtx.nSamplesPerSec;
sound_fx[sound_id].size = child.cksize;
sound_fx[sound_id].state = SOUND_LOADED;

// set up the format data structure
memset(&pcmwf, 0, sizeof(WAVEFORMATEX));

pcmwf.wFormatTag = WAVE_FORMAT_PCM; // pulse code modulation
pcmwf.nChannels = 1; // mono
pcmwf.nSamplesPerSec = 11025; // always this rate
pcmwf.nBlockAlign = 1;
pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
pcmwf.wBitsPerSample = 8;
pcmwf.cbSize = 0;

// prepare to create sounds buffer
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = control_flags | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE;
dsbd.dwBufferBytes = child.cksize;
dsbd.lpwfxFormat = &pcmwf;

// create the sound buffer
if (FAILED(lpds->CreateSoundBuffer(&dsbd,&sound_fx[sound_id].dsbuffer,NULL)))
{
// release memory
free(snd_buffer);

// return error
return(-1);
} // end if

// copy data into sound buffer
if (FAILED(sound_fx[sound_id].dsbuffer->Lock(0,
child.cksize,
(void **) &audio_ptr_1,
&audio_length_1,
(void **)&audio_ptr_2,
&audio_length_2,
DSBLOCK_FROMWRITECURSOR)))
return(0);

// copy first section of circular buffer
memcpy(audio_ptr_1, snd_buffer, audio_length_1);

// copy last section of circular buffer
memcpy(audio_ptr_2, (snd_buffer+audio_length_1),audio_length_2);

// unlock the buffer
if (FAILED(sound_fx[sound_id].dsbuffer->Unlock(audio_ptr_1,
audio_length_1,
audio_ptr_2,
audio_length_2)))
return(0);

// release the temp buffer
free(snd_buffer);

// return id
return(sound_id);

} // end DSound_Load_WAV
RabbitLBJ 2009-05-21
  • 打赏
  • 举报
回复
没试过,我连文件都读不进去,怎么试??
majun01 2009-05-21
  • 打赏
  • 举报
回复
RabbitLBJ 2009-05-21
  • 打赏
  • 举报
回复
2L,那不是我们学校的网站吗?我想要一个函数,我觉得自己写不出来,想拿一个来研究一下!!

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧