Last active
March 5, 2018 19:38
-
-
Save ivanstepanovftw/bc1f68af82277c33b6664373fad97135 to your computer and use it in GitHub Desktop.
Should record audio from speakers. UPD: https://github.com/AshFTW/output-audio-stream
This file contains 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 <cstdint> | |
#include <cstring> | |
#include <unistd.h> | |
#include <iostream> | |
#include <portaudio.h> | |
#include <cmath> | |
using namespace std; | |
#define LEN(n) sizeof(n)/sizeof(n[0]) | |
#define SAMPLE int16_t | |
/* Declaration */ | |
SAMPLE* CaptureBuffer; | |
bool lastBufferNull = false; | |
/* | |
* Log a PaError to console. | |
*/ | |
static void log_pa(PaError err) { | |
cout << "PortAudio: " << Pa_GetErrorText(err) << endl; | |
} | |
/* | |
* Record Callback and display sound | |
*/ | |
static int recordCallback(const void *inputBuffer, void *outputBuffer, | |
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, | |
PaStreamCallbackFlags statusFlags, void *userData) { | |
/* Declaration */ | |
/* Since we only use memcpy, these casts are not strictly neccessary. */ | |
// const SAMPLE *rptr = (SAMPLE*)inputBuffer; | |
// SAMPLE *wptr = (SAMPLE*)CaptureBuffer+16; | |
// | |
// if (inputBuffer) { | |
// for(int i = 0; i < framesPerBuffer; ++i) { | |
// wptr += 2; | |
// rptr += 2; | |
//// cout << *rptr++ << ":"; | |
// } | |
// cout << endl; | |
// lastBufferNull = false; | |
// } else { | |
// if (!lastBufferNull) { | |
// cerr<<"Record callback received null buffer"<<endl; | |
// lastBufferNull = true; | |
// } | |
// for (int i = 0; i < framesPerBuffer; ++i) { | |
// wptr += 2; | |
// } | |
// } | |
// for (int i = 0; i < framesPerBuffer; i++) { | |
// *wptr++ = FUZZ(*rptr++); /* left - distorted */ | |
// *wptr++ = *rptr++; /* right - clean */ | |
// } | |
int16_t *in = (int16_t *) inputBuffer; | |
for(int i = 0; i < LEN(in); i++) { | |
cout << in[i] << ":"; | |
} | |
cout << endl; | |
return paContinue; | |
} | |
int main(int argc, char **argv) { | |
// PaError err = 0; | |
// const PaDeviceInfo *devInfo; | |
// const PaStreamInfo *streamInfo; | |
// int32_t defaultSampleRate = 44100; | |
// char* _CaptureBuffer; | |
// char* _CaptureBufferComp; | |
// int32_t _CaptureBufferSizeComp = 0x790; | |
// int32_t _CurrentCaptureIndexComp = 0x10; | |
// | |
// PaStreamParameters inputParameters; | |
// PaStream *stream; | |
// | |
// err = Pa_Initialize(); | |
// if (err) { | |
// cerr << "An error occurred while opening the portaudio stream." << endl; | |
// goto error; | |
// } | |
// | |
// inputParameters.device = Pa_GetDefaultInputDevice(); | |
// if (inputParameters.device == -1){ | |
// cerr<<"Error: No default input device."<<endl; | |
// err = -10000; | |
// goto error; | |
// } | |
// | |
// devInfo = Pa_GetDeviceInfo(inputParameters.device); | |
// if (!devInfo) { | |
// cerr<<"Error: Invalid default input device."<<endl; | |
// err = -10000; | |
// goto error; | |
// } | |
// inputParameters.channelCount = 2; | |
// inputParameters.sampleFormat = paInt16; | |
// inputParameters.suggestedLatency = devInfo->defaultLowInputLatency; | |
// inputParameters.hostApiSpecificStreamInfo = NULL; | |
// defaultSampleRate = floor(devInfo->defaultSampleRate + 0.5); | |
// clog<<"Portaudio reported default input device sample rate is "<<defaultSampleRate<<" (may not be accurate)"<<endl; | |
// | |
//// clog<<"stream size "<<sizeof(PaStream**) | |
//// <<", parameters "<<sizeof(const PaStreamParameters*) | |
//// <<", double "<<sizeof(double) | |
//// <<", unsigned long "<<sizeof(unsigned long) | |
//// <<", flags "<<sizeof(PaStreamFlags) | |
//// <<", callback "<<sizeof(PaStreamCallback*) | |
//// <<", void_p "<<sizeof(void*)<<endl; | |
// | |
//// COERCE_UNSIGNED_INT64((long double)defaultSampleRate), | |
//// COERCE_UNSIGNED_INT64((long double)defaultSampleRate) >> 32, | |
//// 338, | |
//// 1, | |
//// Audio::recordCallback, | |
//// 0); | |
// //(1234.1234.1234.1234.1234.1234.1234.1234.1234.1234.1234.1234.0000.0000.0000.0152.0000.0000.0000.0001.1234.1234.1234.1234); | |
// //(link to stream .link to input .link to output .double . unsigned long .PaStreamFlags .Callback pointer | |
// err = Pa_OpenStream(&stream, &inputParameters, NULL, defaultSampleRate, 1, paClipOff, recordCallback, NULL); | |
// if (err) | |
// goto error; | |
// | |
// err = Pa_StartStream(stream); | |
// if (err) | |
// goto error; | |
// | |
// streamInfo = Pa_GetStreamInfo(stream); | |
// cout<<"Audio stream info: sample rate = "<<streamInfo->sampleRate | |
// <<", input latency = "<<streamInfo->inputLatency<<" sec"<<endl; | |
/* Initialize. */ | |
log_pa(Pa_Initialize()); | |
PaStreamParameters input_parameters; | |
/* Set record device (monitor of Default). */ | |
input_parameters.device = Pa_GetDefaultInputDevice(); | |
input_parameters.channelCount = 2; | |
input_parameters.sampleFormat = paInt16; | |
const PaDeviceInfo *input_info = Pa_GetDeviceInfo(input_parameters.device); | |
input_parameters.suggestedLatency = input_info->defaultLowInputLatency; | |
input_parameters.hostApiSpecificStreamInfo = NULL; | |
/* Create and start stream, let it run for 5 seconds, then close it again. */ | |
PaStream *stream; | |
log_pa(Pa_OpenStream(&stream, &input_parameters, NULL, input_info->defaultSampleRate, paFramesPerBufferUnspecified, | |
paClipOff, recordCallback, NULL)); | |
log_pa(Pa_StartStream(stream)); | |
Pa_Sleep(5000); | |
log_pa(Pa_StopStream(stream)); | |
log_pa(Pa_CloseStream(stream)); | |
log_pa(Pa_Terminate()); | |
cerr<<"ASDLASHDAJSDHAJSDJHALKJSDHJAJKLSJD"<<endl; | |
return 0; | |
// | |
// Pa_Sleep(5000000); | |
// | |
// err = Pa_CloseStream(stream); | |
// Pa_Terminate(); | |
// if (err) { | |
// cerr<<"An error occured while closing the portaudio stream"<<endl; | |
// goto error; | |
// } | |
// | |
//error: | |
// cerr<<"Error message("<<err<<"): "<<Pa_GetErrorText(err)<<endl; | |
// return err; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment