portaudio获取录音并播放录音

// PortAudioTest.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<iostream>
#include"portAudio/portaudio.h"
using namespace std;
#pragma comment(lib,"portAudio/portaudio_x86.lib")
PaStream *in_stream;
PaStream *out_stream;


int paOutStreamBk(const void* input, void* output, unsigned long frameCount,
	const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void * userData)
{
	Pa_WriteStream(out_stream, input, frameCount);
	return 0;
}


bool StartPlay()
{


	PaStreamParameters ouputParameters;
	PaError err = paNoError;
	ouputParameters.device = Pa_GetDefaultOutputDevice();
	ouputParameters.channelCount = 2;
	ouputParameters.sampleFormat = paFloat32;
	ouputParameters.suggestedLatency = Pa_GetDeviceInfo(ouputParameters.device)->defaultLowOutputLatency;
	ouputParameters.hostApiSpecificStreamInfo = NULL;


	err = Pa_OpenStream(&out_stream, NULL, &ouputParameters, 44100, 256, paFramesPerBufferUnspecified, NULL, NULL);
	if (err != paNoError) {
		return false;
	}


	err = Pa_StartStream(out_stream);
	if (err != paNoError) {
		return false;
	}
	return true;
}


bool InitPortAudio(int deviceIndex)
{
	PaStreamParameters intputParameters;
	PaError err = paNoError;


	err = Pa_Initialize();
	if (err != paNoError) goto error;


	if (deviceIndex < 0)
	{
		deviceIndex = Pa_GetDefaultInputDevice();
	}
	intputParameters.device = deviceIndex;
	intputParameters.channelCount = 2;
	intputParameters.sampleFormat = paFloat32;
	intputParameters.suggestedLatency = Pa_GetDeviceInfo(intputParameters.device)->defaultLowInputLatency;
	intputParameters.hostApiSpecificStreamInfo = NULL;


	err = Pa_OpenStream(&in_stream, &intputParameters, NULL, 44100, 256, paFramesPerBufferUnspecified, paOutStreamBk, NULL);
	if (err != paNoError) {
		return false;
	}
	return StartPlay();




error:
	Pa_Terminate();
	fprintf(stderr, "An error occured while using the portaudio stream\n");
	fprintf(stderr, "Error number: %d\n", err);
	fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
	return -1;
}


int main()
{
	if (InitPortAudio(0)) {
		Pa_StartStream(in_stream);
	}
	else {
		cout << "init PortAudio failed" << endl;
	}
	getchar();
	Pa_CloseStream(out_stream);
	Pa_CloseStream(in_stream);
	Pa_Terminate();
    return 0;
}




代码下载地址:https://download.csdn.net/download/xzpblog/10275152

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值