在 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. 奇点云数据中台技术汇(四)| DataSimba系列之流式计算

    你是否有过这样的念头:如果能立刻马上看到我想要的数据,我就能更好地决策?   市场变化越来越快,企业对于数据及时性的需求,也越来越大,另一方面,当下数据容量呈几何倍暴增,数据的价值在其产生之后,也将随 ...

  2. [LC] 222. Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  3. selenium中quit与close方法的区别

    https://blog.csdn.net/lbxoqy/article/details/71981222

  4. UFT检查点

    一.标准检查点 选择需要插入检查点的语句,点击右键,选择Insert Standard Checkpoint.... 二.图像检查点(Insert Standard Checkpoint....) 在 ...

  5. java面试题 - 框架

    1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对应的servlet,并将request.resp ...

  6. 使用java列举所有给定数组中和为定值的组合

    import java.util.Arrays; public class SolveProb { ]; ;// 记录当前 public SolveProb() { } public static v ...

  7. 在腾讯云服务器上安装JDK+Tomcat并启动tomcat

    由于Java web项目需要使用到tomcat所以决定在腾讯云服务器(centos7.X)上安装JDK和tomcat,来部署我们的项目. 准备工具: 云服务器:centos7.x+ 本地连接服务器:X ...

  8. JavaScript常见排序算法

    1.冒泡排序 function bubble_sort(arr) { if (arr.length <= 1) { return arr; } var len = arr.length; for ...

  9. 阿里云服务器上搭建seafile专业版

    因为官方一键安装教程在阿里云服务器上无法安装,由于水平有限,无法解决,所以选择手动安装 参考资料: 1,.腾讯云搭建seafile服务器 2.How to Install Seafile with N ...

  10. LoraLU

    一.定义动画过程中形成的状态用transform transform 分为2D 和 3D,主要包含以下几种变换:旋转rotate.扭曲skew.缩放scale和移动 translate以及矩阵变形ma ...