Linuux-alsa-左右声道处理
Linuux-alsa-左右声道处理
文章目录
- Linuux-alsa-左右声道处理
- 前言
- 一、具体处理部分
- 1.头文件
- 2.声卡初始化
- 3.接口封装
- 总结
前言
基于alsa实现实现左右声道的分离与合并。此处为对双声道处理的简单示例,
提示:以下是本篇文章正文内容,下面案例可供参考
一、具体处理部分
1.头文件
头文件:
/*************************************************************************> File Name: pcm.h> Author: ***> Mail: 1301751355@qq.com > Created Time: 2021年04月20日 星期二 14时33分50秒************************************************************************/
#ifndef __PCM_H__
#define __PCM_H_#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/stat.h>
#include <time.h>
//#include <tinyalsa/asoundlib.h>#include "include/tinyalsa/asoundlib.h"
//#include <alsa/asoundlib.h>struct pcm* i_pcm_open(int direction);
void i_pcm_close(int direction);
int LefeAndRightChannel(char* LefeChannelFile, char* RightChannelFile); //左声道文件路径、右声道文件路径#endif
2.声卡初始化
代码如下(示例):
/*************************************************************************> File Name: pcm.c> Author: ***> Mail: 1301751355@qq.com > Created Time: 2021年04月20日 星期二 14时35分55秒************************************************************************/#include<stdio.h>
#include<assert.h>
#include"pcm.h"static struct pcm *pcm_in = NULL;
static struct pcm *pcm_out = NULL;
static struct pcm* _i_pcm_open(int direction){struct pcm* pcm;unsigned int card = 0;unsigned int device = 0;unsigned int channels = 2;unsigned int rate = 48000;enum pcm_format format = PCM_FORMAT_S16_LE;unsigned int period_size = 320;//320*4;unsigned int period_count = 4;//4;struct pcm_config config;memset(&config,0,sizeof(config));config.channels = channels;config.rate = rate;config.period_size = period_size;config.period_count = period_count;config.format = format;config.stop_threshold = 0;config.start_threshold = 0;config.silence_threshold = 0;pcm = pcm_open(card, device, direction, &config);if(!pcm || !pcm_is_ready(pcm)){printf("Unable open PCM DEVICE (%s)\n",pcm_get_error(pcm));return ;}return pcm;
}
struct pcm* i_pcm_open(int direction){struct pcm *pcm;switch(direction){case PCM_IN:if(pcm_in == NULL)pcm_in = _i_pcm_open(PCM_IN);pcm = pcm_in;break;case PCM_OUT:if(pcm_out == NULL)pcm_out = _i_pcm_open(PCM_OUT);pcm = pcm_out;break;default:break;}return pcm;
}
void i_pcm_close(int direction){if(direction == PCM_IN){if(pcm_in != NULL)pcm_close(pcm_in);pcm_in = NULL;}if(direction == PCM_OUT){if(pcm_out != NULL)pcm_close(pcm_out);pcm_out = NULL;}
}
3.接口封装
/*************************************************************************> File Name: pcm-play.c> Author: ***> Mail: 1301751355@qq.com > Created Time: 2021年04月20日 星期二 15时59分14秒************************************************************************/#include<stdio.h>
#include"pcm.h"#define MAXSIZE(a,b) ((a>b)?a:b)static void LefeChannel(char * buffer, char *lefeChannel, int MAXBUFFERSIZE)
{int i = 0;
#if 0for (i = 0; i < MAXBUFFERSIZE/2; i+=2){*(lefeChannel+i*2) = *(buffer + i);*(lefeChannel+i*2+1) = *(buffer + i + 1);}
#endiffor (i = 0; i < MAXBUFFERSIZE; i+=4){*(lefeChannel+i) = *(buffer + i);*(lefeChannel+i+1) = *(buffer + i + 1);}}static void RightChannel(char * buffer, char *rightChannel, int MAXBUFFERSIZE)
{int i = 0;for (i = 0; i < MAXBUFFERSIZE; i+=4){*(rightChannel+i+2) = *(buffer + i + 2);*(rightChannel+i+3) = *(buffer + i + 3);}}static void SoundContent(char* soundBuf, char* lefeChannel, char* rightChannel, int size){int i = 0;for(i = 0; i < size; i+=4){*(soundBuf+i) = *(lefeChannel+i);*(soundBuf+i+1) = *(lefeChannel+i+1);*(soundBuf+i+2) = *(rightChannel+i+2);*(soundBuf+i+3) = *(rightChannel+i+3);}
}int LefeAndRightChannel(char* LefeChannelFile, char* RightChannelFile) /*左声道文件路径、右声道文件路径 成功返回:1 失败返回 0*/ /*成功:播放结合后的立体声*/
{/*获取文件大小*/struct stat statbuf;stat(LefeChannelFile, &statbuf);int LefeSize = statbuf.st_size; /*左声道文件大小*/memset(&statbuf,0,sizeof(statbuf));stat(RightChannelFile, &statbuf);int RightSize = statbuf.st_size; /*右声道文件大小*/
#ifdef DEBUG_SOUNDprintf("LefeSize = %d, RightSize = %d\n",LefeSize, RightSize);
#endifchar *LefeFileBuf; /*左声道文件内容*/LefeFileBuf = malloc(LefeSize);if(LefeFileBuf == NULL){fprintf(stderr,"malloc failed \n");}FILE* Lefe_fp = fopen(LefeChannelFile,"rb");if(Lefe_fp == NULL){printf("fopen LefeFile failed\n");return 0;}LefeSize = fread(LefeFileBuf,1,LefeSize,Lefe_fp);char *RightFileBuf; /*右声道文件内容*/RightFileBuf = malloc(RightSize);if(RightFileBuf == NULL){fprintf(stderr,"malloc failed\n");return 0;}FILE* Right_fp = fopen(RightChannelFile,"rb");if(Right_fp == NULL){printf("fopen RightFile failed\n");return 0;}RightSize = fread(RightFileBuf,1,RightSize,Right_fp);struct pcm *pcm;pcm = i_pcm_open(PCM_OUT);if(pcm == NULL)return 0;char *lefeChannel; /*提取左声道数据*/lefeChannel = malloc(LefeSize);memset(lefeChannel,0,LefeSize);LefeChannel(LefeFileBuf,lefeChannel,LefeSize);char *rightChannel; /*提取右声道数据*/rightChannel = malloc(RightSize);memset(rightChannel,0,RightSize);RightChannel(RightFileBuf,rightChannel,RightSize);char *soundBuf; /*立体声数据:左右声道内容结合*/soundBuf = malloc(MAXSIZE(LefeSize,RightSize));memset(soundBuf,0,MAXSIZE(LefeSize,RightSize));
#ifdef DEBUG_SOUNDprintf("soundBuf size %d\n",MAXSIZE(LefeSize,RightSize));
#endifSoundContent(soundBuf,lefeChannel,rightChannel,MAXSIZE(LefeSize,RightSize));pcm_write(pcm,soundBuf,MAXSIZE(LefeSize,RightSize));i_pcm_close(PCM_OUT);free(LefeFileBuf);free(RightFileBuf);free(lefeChannel);free(rightChannel);free(soundBuf);fclose(Lefe_fp);fclose(Right_fp);return 1;
}
总结
以上处理方式需要对I2S协议有一些基础的了解。
Linuux-alsa-左右声道处理相关推荐
- Linux ALSA声卡驱动之八:ASoC架构中的Platform
1. Platform驱动在ASoC中的作用 前面几章内容已经说过,ASoC被分为Machine,Platform和Codec三大部件,Platform驱动的主要作用是完成音频数据的管理,最终通过C ...
- alsa 测试 linux_python语音智能对话聊天机器人--linuxamp;amp;树莓派双平台兼容
此项目只不过是之前大三刚学python就想做点好玩的项目试试看(因此技术含量不高),后来这个成为毕业设计的一部分,长期看博客上访问量也不错,就发布出来,希望有想入门python 的朋友可以参考写来玩玩 ...
- 基于Orangpi Zero和Linux ALSA实现WIFI无线音箱(三)
作品已经完成,先上源码: https://files.cnblogs.com/files/qzrzq1/WIFISpeaker.zip 全文包含三篇,这是第三篇,主要讲述接收端程序的原理和过程. 第一 ...
- linux音频时钟bclk,linux alsa音频中采样率fs、比特率BCLK 、主时钟MCLK关系
转:https://blog.csdn.net/lugandong/article/details/72468831 一. 拿512fs说话: 看图知道采样的位深是32bit(位),左右声道各占了8* ...
- ALSA(二), makefile, Autotools, premake
http://antkillerfarm.github.io/ 从Gstreamer到ALSA(续) 4.SOC_SINGLE类宏 这里对SOC_SINGLE类的宏,详细说明一下,因为只有这些宏才是真 ...
- Alsa中PCM参数设置
分类: LINUX 1) PCM设备的句柄. 2) 指定同时可供回放或截获的PCM流的方向 3) 提供一些关于我们想要使用的设置选项的信息,比如缓冲区大小,采样率,PCM数据格式等 4) 检查硬件是否 ...
- 关于USB-AUDIO使用ALSA编程的一点问题
转载自:http://blog.chinaunix.net/uid-25272011-id-3153434.html 最近在调试一款原相PAP7501摄像头中的USB的麦克风,USB层走的应该是标准的 ...
- 转 alsa录音放音执行流程详解
前言: linux中,无论是oss还是alsa体系,录音和放音的数据流必须分析清楚.先分析alsa驱动层,然后关联到alsa库层和应用层. 链接分析: core/pcm_native.c文件中.mma ...
- Linux ALSA声卡驱动之四:Control设备的创建
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! Control接口 Control接口主要让用户空间的应用程序(alsa-lib)可以访问 ...
- 基于alsa的音量控制代码
http://www.alsa-project.org/alsa-doc/alsa-lib/group___simple_mixer.html ALSA库接口说明文档 基于alsa的音量控制代码 [p ...
最新文章
- wireshark安装
- Linux中断处理与定时器
- C++模版和C#泛型求同存异录(一)sizeof(T)
- 7-10 逆波兰表达式求值 (20 分)(c语言)(数据结构)
- windows下apache+php+mysql 环境配置方法
- 量化干货:量化交易系统设计的六大细节
- 吸收塔如何提高吸收率_缺钙了该如何补钙?饮食补钙更健康
- Flutter进阶第8篇:实现视频播放
- 此时本机的BootLoader程序坏了,也就是说grub第一阶段坏掉了,该如何修复
- 小偷程序原理和简单示例
- css之px自动转rem—“懒人”必备
- 基于GD32MCU程序远程升级IAP设计思路
- Android6.0源码下载
- unittest模块:单元测试
- 根据数据库中提供的坐标(经纬度) 在前端地图上标示坐标点、并显示
- python使用pika订阅rabbitmq消息链接被重置问题
- python编程视频剪辑_专治爱剪辑的片头片尾的脚本(Python)
- MUI开发大全(小程序前端框架)
- 工业协议:DNP协议
- php网页爬虫-简单的类