current version.. forgot to commit
This commit is contained in:
62
pa/main.cpp
Executable file
62
pa/main.cpp
Executable file
@@ -0,0 +1,62 @@
|
||||
#include "pulseaudio.h"
|
||||
|
||||
#include "../lib/BeatDetection2.h"
|
||||
|
||||
#include "../lib/Socket.h"
|
||||
SocketUDP sck;
|
||||
|
||||
void sendUDP(const std::string& msg) {
|
||||
NetworkAddress destination("127.0.0.1", 5050);
|
||||
sck.sendDatagram(msg, destination);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
sck.bind(5051);
|
||||
sendUDP("starting");
|
||||
|
||||
// sanity check
|
||||
if (argc != 2) {
|
||||
std::cout << "usage: paBeatRecorder [pa_deviceName]" << std::endl;
|
||||
std::cout << "e.g. paBeatRecorder alsa_output.pci-0000_00_1b.0.analog-stereo.monitor" << std::endl;
|
||||
std::cout << "list devices with: pactl list | grep '\\.monitor'" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get the to-be-used device name
|
||||
std::string devName = argv[1];
|
||||
|
||||
PulseAudio pa(devName);
|
||||
//pa.join();
|
||||
|
||||
int16_t buf[1024];
|
||||
|
||||
BeatDetection2<float> bestBass(Mode::BASS);
|
||||
BeatDetection2<float> beatSnare(Mode::SNARE);
|
||||
|
||||
while(true) {
|
||||
|
||||
pa.read(buf, 1024);
|
||||
|
||||
for (int i = 0; i < 1024; ++i) {
|
||||
const float left = buf[i];
|
||||
const float right = buf[i];
|
||||
|
||||
const bool bBass = bestBass.add(left, right);
|
||||
if (bBass) {
|
||||
std::cout << "bass\n" << std::flush;
|
||||
sendUDP("bass");
|
||||
}
|
||||
|
||||
const bool bSnare = beatSnare.add(left, right);
|
||||
if (bSnare) {
|
||||
std::cout << "snare...\n" << std::flush;
|
||||
sendUDP("snare");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
107
pa/pulseaudio.h
Executable file
107
pa/pulseaudio.h
Executable file
@@ -0,0 +1,107 @@
|
||||
#ifndef PULSEAUDIO_H
|
||||
#define PULSEAUDIO_H
|
||||
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include <pulse/simple.h>
|
||||
#include <iostream>
|
||||
#include "../exception.h"
|
||||
#include <thread>
|
||||
|
||||
/**
|
||||
* receive events from pulse audio
|
||||
*/
|
||||
class PulseAudio {
|
||||
|
||||
private:
|
||||
|
||||
pa_sample_spec sampleSpec;
|
||||
pa_simple* simple;
|
||||
std::thread thread;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
PulseAudio(const std::string& devName) {
|
||||
|
||||
sampleSpec.format = PA_SAMPLE_S16LE;
|
||||
sampleSpec.rate = 44100;
|
||||
sampleSpec.channels = 1;
|
||||
|
||||
// pactl list | grep "\.monitor"
|
||||
|
||||
const char* server = nullptr;
|
||||
const char* clientName = "beat detection";
|
||||
const char* streamName = "recording beats";
|
||||
const char* dev = devName.c_str(); //nullptr;
|
||||
//const char* dev = "alsa_output.usb-0d8c_USB_Sound_Device-00-Device.analog-surround-51.monitor";
|
||||
|
||||
// connect
|
||||
int error;
|
||||
simple = pa_simple_new(server, clientName, PA_STREAM_RECORD, dev, streamName, &sampleSpec, NULL, NULL, &error);
|
||||
if (!simple) {throw Exception("error while connecting to PulseAudio");}
|
||||
std::cout << simple << std::endl;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void start() {
|
||||
std::cout << "starting read loop" << std::endl;
|
||||
thread = std::thread(&PulseAudio::readLoop, this);
|
||||
}
|
||||
|
||||
void join() {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
void read(int16_t* dst, int samples) {
|
||||
const int bytes = samples * sizeof(int16_t);
|
||||
int error;
|
||||
const int res = pa_simple_read(simple, dst, bytes, &error);
|
||||
if (res < 0) {throw Exception("error while reading data");}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void readLoop() {
|
||||
|
||||
std::cout << "hello from read loop" << std::endl;
|
||||
int16_t buffer[1024];
|
||||
int error = 0;
|
||||
|
||||
while(true) {
|
||||
|
||||
const int res = pa_simple_read(simple, buffer, sizeof(buffer), &error);
|
||||
std::cout << res << ", " << error << std::endl;
|
||||
if (res < 0) {throw Exception("error while reading data");}
|
||||
|
||||
std::cout << buffer[0] << "," << buffer[1] << "," << buffer[2] << "," << buffer[3] << std::endl;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
int i = -1;
|
||||
|
||||
|
||||
while (!exit_program) {
|
||||
i = (i+1) % BUFNUMBER;
|
||||
|
||||
pthread_mutex_lock(&(buffer[i].write));
|
||||
// Record data and save it to the buffer
|
||||
if (pa_simple_read(s, buffer[i].buf, sizeof(buffer[i].buf), &error) < 0) {
|
||||
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
|
||||
pa_simple_free(s);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// unlock the reading mutex
|
||||
pthread_mutex_unlock(&(buffer[i].read)); // open up for reading
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
#endif // PULSEAUDIO_H
|
||||
Reference in New Issue
Block a user