在 ARM 2440 开发板上正常播放 16bit  44100 采样率的wav , 为了程序简单,没有判断返回值。

补充,在 ubunto 上也能正常播放。

编译方法: arm-linux-gcc -lasound wplay.c -o wplay  或在 ubuntu 上编译 gcc -lasound wplay.c -o wplay

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <alsa/asoundlib.h>
#ifndef WORD
#define WORD unsigned short
#endif #ifndef DWORD
#define DWORD unsigned int
#endif struct RIFF_HEADER
{
char szRiffID[]; // 'R','I','F','F'
DWORD dwRiffSize;
char szRiffFormat[]; // 'W','A','V','E'
}; struct WAVE_FORMAT
{
WORD wFormatTag;
WORD wChannels;
DWORD dwSamplesPerSec;
DWORD dwAvgBytesPerSec;
WORD wBlockAlign;
WORD wBitsPerSample;
}; struct FMT_BLOCK
{
char szFmtID[]; // 'f','m','t',' '
DWORD dwFmtSize;
struct WAVE_FORMAT wavFormat;
}; struct DATA_BLOCK
{
char szDataID[]; // 'd','a','t','a'
DWORD dwDataSize;
}; void read_wav(unsigned char *wav_buf, int *fs, int *channels, int *bits_per_sample, int *wav_size, int *file_size)
{
struct RIFF_HEADER *headblk;
struct FMT_BLOCK *fmtblk;
struct DATA_BLOCK *datblk; headblk = (struct RIFF_HEADER *) wav_buf;
fmtblk = (struct FMT_BLOCK *) &headblk[];
datblk = (struct DATA_BLOCK *) &fmtblk[]; *file_size = headblk->dwRiffSize; //采样频率
*fs = fmtblk->wavFormat.dwSamplesPerSec; //通道数
*channels = fmtblk->wavFormat.wChannels; *wav_size = datblk->dwDataSize;
//采样bit数 16 24
*bits_per_sample = fmtblk->wavFormat.wBitsPerSample;
} int main(int argc, char ** argv)
{
int fs, channels, bits_per_sample, wav_size, file_size;
unsigned char *wav_buf;
unsigned char *audio_buf;
unsigned char *audio_p;
int fd;
struct stat stat; int size;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;//硬件信息和PCM流配置 snd_pcm_uframes_t chunk_size = ;
unsigned int rate;
snd_pcm_format_t format; if( != argc)
{
printf("usage:%s wav file\n", argv[]);
return -;
} fd = open(argv[], O_RDONLY);
fstat(fd, &stat);
wav_buf = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, );
read_wav(wav_buf, &fs, &channels, &bits_per_sample, &wav_size, &file_size);
printf("wav format: fs = %d, channels = %d, bits_per_sample = %d, wav_size = %d file_size = %d\n\r", fs, channels, bits_per_sample, wav_size, file_size); //真实wav 跳过头部
audio_buf = wav_buf + sizeof(struct RIFF_HEADER) + sizeof(struct FMT_BLOCK) + sizeof(struct DATA_BLOCK); rate = fs;
//初始化声卡
//1. 打开PCM,最后一个参数为0意味着标准配置
if ( > snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, ))
{
printf("snd_pcm_open err\n");
return -;
}
//2. 分配snd_pcm_hw_params_t结构体
if( > snd_pcm_hw_params_malloc (&hw_params))
{
printf("snd_pcm_hw_params_malloc err\n");
return -;
}
//3. 初始化hw_params
if( > snd_pcm_hw_params_any (playback_handle, hw_params))
{
printf("snd_pcm_hw_params_any err\n");
return -;
}
//4. 初始化访问权限
if ( > snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED))
{
printf("snd_pcm_hw_params_any err\n");
return -;
}
//5. 初始化采样格式SND_PCM_FORMAT_U8,8位
if( == bits_per_sample)
{
if ( > snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_U8))
{
printf("snd_pcm_hw_params_set_format err\n");
return -;
}
format = SND_PCM_FORMAT_U8;
} if( == bits_per_sample)
{
if ( > snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE))
{
printf("snd_pcm_hw_params_set_format err\n");
return -;
}
format = SND_PCM_FORMAT_S16_LE;
}
//6. 设置采样率
if ( > snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &rate, ))
{
printf("snd_pcm_hw_params_set_rate_near err\n");
return -;
}
//7. 设置通道数量
if ( > snd_pcm_hw_params_set_channels(playback_handle, hw_params, ))
{
printf("snd_pcm_hw_params_set_channels err\n");
return -;
} //8. 设置hw_params
if ( > snd_pcm_hw_params (playback_handle, hw_params))
{
printf("snd_pcm_hw_params err\n");
return -;
} snd_pcm_hw_params_get_period_size(hw_params, &chunk_size, ); snd_pcm_hw_params_free (hw_params);
if ( > snd_pcm_prepare (playback_handle))
{
printf("snd_pcm_prepare err\n");
return -;
}
//chunk_size = ?;
printf("chunk_size:%ld \n", chunk_size);
audio_p = audio_buf;
while((audio_p - audio_buf) <= stat.st_size)
{
snd_pcm_writei(playback_handle, audio_p, chunk_size);
audio_p += chunk_size * ; //16位 双声道
}
printf("play ok \n");
snd_pcm_close(playback_handle);
return ;
}

使用 ALSAlib 播放 wav的更多相关文章

  1. C#播放wav文件

    C#使用HWQPlayer类播放wav文件 类的代码: using System.IO; using System.Runtime.InteropServices; namespace HoverTr ...

  2. python 播放 wav 文件

    未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...

  3. MmSystem播放Wav格式声音

    //MmSystem播放Wav格式声音 //MmSystem 支持 *.wav声音格式 snd ->SoundRecorderuses MmSystem; //引用MmSystem//播放系统声 ...

  4. 1.1.6-学习Opencv与MFC混合编程之---播放WAV音乐和 alpha融合功能

    源代码:http://download.csdn.net/detail/nuptboyzhb/3961698 Alpha融合菜单项 1.      增加alpha融合菜单项,修改相应的属性,建立类向导 ...

  5. 多浏览器播放wav格式的音频文件

    html5的audio标签只在火狐下支持wav格式的音频播放,无法兼容IE和google , 使用audioplayer.js 基本上能支持大部分浏览器播放wav音频文件,经测试IE.火狐.googl ...

  6. WinAPI: sndPlaySound - 播放 wav 文件

    WinAPI: sndPlaySound - 播放 wav 文件 //声明: sndPlaySound(   lpszSoundName: PChar; {声音文件}   uFlags: UINT{播 ...

  7. C++播放wav音乐和音效

    1.  #include <mmsystem.h>#pragma comment(lib,"winmm.lib")PlaySound(TEXT("c:\\te ...

  8. 用 Qt 的 QAudioOutput 类播放 WAV 音频文件

    用 Qt 的 QAudioOutput 类播放 WAV 音频文件 最近有一个项目,需要同时控制 4 个声卡播放不同的声音,声音文件很简单就是没有任何压缩的 wav 文件. 如果只是播放 wav 文件, ...

  9. 8086汇编语言 调用声卡播放wav文件(sound blaster)

    开更 大概最后做了一个能播放无损音乐(无压缩.不需解码)的播放器 原理是基于dosbox的模拟声卡,通过硬件之间的相互通讯做到的 关于详细内容接下来再讲. 一.从dosbox入手 我们知道cpu可以直 ...

随机推荐

  1. GPIO-FPGA架构

    GPIO是一种软件运行期间能够动态配置和控制的通用引脚 有不同的GPIObank,每个GPIO口的bank都会有编号的区分, 每个GPIO口除了通用的输入输出功能以外,还有其他复用功能,例如GPIO5 ...

  2. [LC] 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. OpenCV、EmguCV函数注解

  4. interrupt 停止线程

    该方法只是给线程设置了一个停止的标记 并不是真正的立即停止线程 interrupted() 测试当前线程是否已经中断 isInterrupted() 测试线程是否已经中断 停止线程的方法: .异常法 ...

  5. VDMA搭建视频通路总结

    全局观查,对整个工程的搭建的关键是要保证PL部分搭建成功,PS部分搭建成功,而且两者配合的很好. 我理解的PL部分涉及到模块的组合以及模块或者IP之间的逻辑的整理,PL部分困扰我比较久的是自动生成的w ...

  6. [LC] 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  7. 同步linux系统时间

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  8. JS实现select去除option的使用注意事项

    网上讲JS动态添加option和删除option的文章很多,在此推荐一篇: http://www.jb51.net/article/35205.htm 我使用的是如下方法: function remo ...

  9. $random 函数用法

    $random函数调用时,返回一个32位的随机数,它是一个带符号的整形数.如下例: reg[23:0] rand; rand = $random % 60; //产生一个在 -59~59 范围的随机数 ...

  10. idea 使用sonarlint报错解决方案

    在idea使用sonarlint可能出现以下报错: Plugin 'org.sonarlint.idea' failed to initialize and will be disabled. Ple ...