Created
September 13, 2018 15:35
-
-
Save hecomi/f6a45a3e2972850a4a667bfdedef0c91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory> | |
#include <portaudio.h> | |
#include <sndfile.h> | |
void checkError(PaError code) | |
{ | |
if (code == paNoError) { return; } | |
std::cout << Pa_GetErrorText(code) << std::endl; | |
exit(EXIT_FAILURE); | |
} | |
int recordCallback( | |
const void* input, | |
void* output, | |
unsigned long frameCount, | |
const PaStreamCallbackTimeInfo* timeInfo, | |
PaStreamCallbackFlags statusFlags, | |
void* userData | |
) { | |
auto sfw = static_cast<SNDFILE*>(userData); | |
auto data = reinterpret_cast<const short*>(input); | |
sf_writef_short(sfw, data, frameCount) ; | |
return 0; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
// 初期化 | |
checkError( Pa_Initialize() ); | |
std::shared_ptr<void> terminator(nullptr, [](void*) { | |
checkError( Pa_Terminate() ); | |
std::cout << "terminate" << std::endl; | |
}); | |
// WAVE ファイルの作成 | |
SF_INFO sfinfo; | |
sfinfo.samplerate = 44100; | |
sfinfo.channels = 1; | |
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; | |
auto sfw = sf_open("output.wav", SFM_WRITE, &sfinfo); | |
std::shared_ptr<void> sndfileCloser(nullptr, [&](void*) { | |
sf_close(sfw); | |
}); | |
// ストリームを開く | |
PaStream *stream; | |
auto err = Pa_OpenDefaultStream( | |
&stream, 1, 0, paInt16, 44100, 1024, recordCallback, sfw); | |
checkError(err); | |
std::shared_ptr<void> portAudioCloser(nullptr, [&](void*) { | |
checkError(Pa_CloseStream(stream)); | |
std::cout << "close" << std::endl; | |
}); | |
// 録音の開始と停止 | |
checkError(Pa_StartStream(stream)); | |
Pa_Sleep(3 * 1000); | |
checkError(Pa_StopStream(stream)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment