|
本帖最后由 jxcylxh 于 2014-9-22 22:30 编辑
哈哈,高兴之余给大家分享一下哦,其实非常简单的,这里我用的是EasyARM283,这个板子没有提供声卡,那么这里我们使用一张USB DAC ,步骤如下:
1:编译内核,解压linux-2.6.35.3.tar.bz2,这里我使用的是arm-linux-gcc这个交叉编译链,所以要修改一下Makefile:
root@LinuxUser:/opt/fslkernel# cd linux-2.6.35.3
root@LinuxUser:/opt/fslkernel/linux-2.6.35.3# vi Makefile
找到CROSS_COMPILE让他等于你的编译器
export KBUILD_BUILDHOST := $(SUBARCH)
#ARCH ?= $(SUBARCH)
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
好后面我们直接执行 make menuconfig
我们把USB DAC编译到内核里面去,如下:
好我们保存退出,执行make zImage -j8
后面就用Uboot更新一下内核,或者网络NFS加载也可以,具体的我就不讲了。
2.编译Libmad,这个是一个MP3的解码器,使用定点运算,输出的是24Bit的PCM,这个其实也很多教程,大家可以网上找,把编译好的 .so库拷贝到开发板下面去,要是你不想编译,这里有:
解压以后拷贝到你开发板上面的lib文件夹下面,
3.写一个简单的播放程序,贴上一个我在CSDN上面下载的:
- /*
- * 本程序是从 minimad 改进而来,如要更详细的说明请参看 minimad.c
- * 编译: arm-linux-gcc minimad.c -o MyMinimad -lmad -g -Wall
- * 运行: ./MyMinimad filename.mp3
- */
- # include <stdio.h>
- # include <stdlib.h>
- # include <unistd.h>
- # include <sys/stat.h>
- # include <sys/mman.h>
- # include <sys/soundcard.h>
- # include <sys/ioctl.h>
- # include <sys/fcntl.h>
- # include <sys/types.h>
- # include <mad.h>
- struct buffer {
- unsigned char const *start;
- unsigned long length;
- };
- static int sfd; /*声音设备的描述符 */
- static int decode(unsigned char const *, unsigned long);
-
- int main(int argc, char *argv[])
- {
- struct stat stat;
- void *fdm;
- char const *file;
- int fd;
- file = argv[1];
- fd = open(file, O_RDONLY);
- if ((sfd = open("/dev/dsp", O_WRONLY)) < 0) {
- printf("can not open device!!!/n");
- return 5;
- }
-
- ioctl(sfd, SNDCTL_DSP_SYNC, 0); /*此句可以不要 */
- if (fstat(fd, &stat) == -1 || stat.st_size == 0)
- return 2;
- fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (fdm == MAP_FAILED)
- return 3;
- decode(fdm, stat.st_size);
- if (munmap(fdm, stat.st_size) == -1)
- return 4;
- ioctl(sfd, SNDCTL_DSP_RESET, 0);
- close(sfd);
- return 0;
- }
- static
- enum mad_flow input(void *data, struct mad_stream *stream)
- {
- struct buffer *buffer = data;
- if (!buffer->length)
- return MAD_FLOW_STOP;
- mad_stream_buffer(stream, buffer->start, buffer->length);
- buffer->length = 0;
- return MAD_FLOW_CONTINUE;
- }
- /*这一段是处理采样后的pcm音频 */
- static inline signed int scale(mad_fixed_t sample)
- {
-
- if (sample >= MAD_F_ONE)
- sample = MAD_F_ONE - 1;
- else if (sample < -MAD_F_ONE)
- sample = -MAD_F_ONE;
- return (sample /8192); //28 -16 = 13
- //return sample;
- }
- static
- enum mad_flow output(void *data,
- struct mad_header const *header, struct mad_pcm *pcm)
- {
- unsigned int nchannels, nsamples, n;
- mad_fixed_t const *left_ch, *right_ch;
- unsigned char Output[6912*10], *OutputPtr;
- int fmt, wrote, speed;
- nchannels = pcm->channels;
- n = nsamples = pcm->length;
- left_ch = pcm->samples[0];
- right_ch = pcm->samples[1];
- fmt = AFMT_S16_LE;
- speed = (pcm->samplerate <<1 ); /*播放速度是采样率的两倍 */
- ioctl(sfd, SNDCTL_DSP_SPEED, &(speed));
- ioctl(sfd, SNDCTL_DSP_SETFMT, &fmt);
- ioctl(sfd, SNDCTL_DSP_CHANNELS, &(pcm->channels));
- OutputPtr = Output;
- while (nsamples--) {
- signed int sample;
- sample = scale(*left_ch++);
- *(OutputPtr++) = ( unsigned char)(sample >> 0);
- *(OutputPtr++) = ( unsigned char)(sample >> 8);
-
- if (nchannels == 2) {
- sample = scale(*right_ch++);
- *(OutputPtr++) = ( unsigned char)(sample >> 0);
- *(OutputPtr++) = ( unsigned char)(sample >> 8);
- }
- }
- n *= 4; /*数据长度为pcm音频采样的4倍 */
- OutputPtr = Output;
- while (n) {
- wrote = write(sfd, OutputPtr, n);
- OutputPtr += wrote;
- n -= wrote;
- }
- OutputPtr = Output;
- return MAD_FLOW_CONTINUE;
- }
- static
- enum mad_flow error(void *data,
- struct mad_stream *stream, struct mad_frame *frame)
- {
- return MAD_FLOW_CONTINUE;
- }
- static
- int decode(unsigned char const *start, unsigned long length)
- {
- struct buffer buffer;
- struct mad_decoder decoder;
- int result;
- buffer.start = start;
- buffer.length = length;
- mad_decoder_init(&decoder, &buffer, input, 0, 0, output, error, 0);
- mad_decoder_options(&decoder, 0);
- result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
- mad_decoder_finish(&decoder);
- return result;
- }
复制代码
看到这里是16位的,如果你的声卡支持24位,也可以修改为24位输出,怎么改,自己查咯。
/*这一段是处理采样后的pcm音频 */
static inline signed int scale(mad_fixed_t sample)
{
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
return (sample /8192); //28 -16 = 13
//return sample;
}
编译的话上面有提示怎么编译,我自己还简单的写了个Makefile,但是貌似不见了,大家直接执行上面那个编译命令就可以了,这里也有编译出来的可执行文件:
好了,把他通过NFS拷贝到开发板,弄一首自己喜欢的歌,这样 ./miniplay ****.mp3
插上耳机,应该有声音了吧,好了,先到这里,我也是刚刚才玩15天,之前都没有接触过这些东西,哎,万里长征只跨出第一步。加油。。。。。。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|