⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sndoperation.cpp

📁 linux下用c++编写的录音放音程序
💻 CPP
字号:
// SndOperation.cpp: implementation of the CSndOperation class.
//
//////////////////////////////////////////////////////////////////////

#include "SndOperation.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include "ParamDef.h"#include <stdio.h>#include <stdlib.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSndOperation::CSndOperation()
{

}

CSndOperation::~CSndOperation()
{

}

bool CSndOperation::OpenDev(unsigned int flag)
{
	if (flag == ACT_RECORD) {	
		m_devfd = open("/dev/dsp", O_RDONLY);
	}else {
		m_devfd = open("/dev/dsp", O_WRONLY);
	}
	if(m_devfd < 0) {
		printf("error in open /dev/dsp\n");
		return false;
	}
	
	return true;
}

bool CSndOperation::CloseDev()
{
	close(m_devfd);
	return true;
}

bool CSndOperation::SetFormat(unsigned int bits, unsigned int chn,unsigned int hz)
{
	int ioctl_val;
	
    /* set bit format */
    ioctl_val = bits;
    if (ioctl(m_devfd, SNDCTL_DSP_SETFMT, &ioctl_val) == -1) {
		fprintf(stderr, "Set fmt to bit %d failed\n", bits);
		return false;
    }
    if (ioctl_val != bits) {
		fprintf(stderr, "do not support bit %d, supported %d\n", bits,
			ioctl_val);
		return false;
    }
	
    /*set channel */
    ioctl_val = chn;
    if ((ioctl(m_devfd, SNDCTL_DSP_CHANNELS, &ioctl_val)) == -1) {
		fprintf(stderr, "Set Audio Channels %d failed\n", chn);
		return false;
    }
    if (ioctl_val != chn) {
		fprintf(stderr, "do not support channel %d,supported %d\n", chn,
			ioctl_val);
		return false;
    }
	
    /*set speed */
    ioctl_val = hz;
    if (ioctl(m_devfd, SNDCTL_DSP_SPEED, &ioctl_val) == -1) {
		fprintf(stderr, "Set speed to %d failed\n", hz);
		return false;
    }
    if (ioctl_val != hz) {
		fprintf(stderr, "do not support speed %d,supported is %d\n", hz,
			ioctl_val);
		return false;
    }
	return true;
}

int CSndOperation::Record(char *buf, int size)
{
	int tCount = 0;
	tCount = read(m_devfd, buf, size);
	return tCount;
}

int CSndOperation::Play(char *buf, int size)
{
	int tCount = 0;
	tCount = write(m_devfd, buf, size);
	return tCount;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -