Commit 6c228089 authored by Your Name's avatar Your Name
Browse files

more zmqaudio.cc

parent b3a5a77c
No preview for this file type
......@@ -45,6 +45,7 @@
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <sched.h>
#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
......@@ -61,8 +62,10 @@ typedef struct {
char message[20];
} paTestData;
zmq_audiobuffer *zmq_audiobuffer::_playing_now = nullptr;
int verbose = 1;
const int nrbuffers = 5;
const int nrbuffers = 21;
zmq_audiobuffer *buffer[nrbuffers];
PaError print_error(PaError e) {
......@@ -80,6 +83,7 @@ zmq_audiobuffer::zmq_audiobuffer(std::uint32_t _fsamp, std::uint32_t _nsamp,
data{new float[_nsamp * _nchan]}, idata{0},
frames_per_buffer{paFramesPerBufferUnspecified}, playing{false}, stream{nullptr} {
assert(sizeof(float) == 4); // need 32 bit IEEE 754 floats
frames_per_buffer = 100;
}
zmq_audiobuffer::~zmq_audiobuffer() {
......@@ -97,7 +101,7 @@ int zmq_audiobuffer::stream_callback(
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *,
PaStreamCallbackFlags, void *userData) {
std::cerr << "<callback>" << std::flush;
zmq_audiobuffer *pbuf = (zmq_audiobuffer *)userData;
float *out = (float *)outputBuffer;
unsigned long i;
......@@ -112,9 +116,12 @@ int zmq_audiobuffer::stream_callback(
for (int j = 0; j < pbuf->nchan; j++)
*out++ = 0;
ret = paComplete;
//std::cerr << "</callback>" << std::endl;
//return ret;
}
}
std::cerr << "idata=" << pbuf->idata << " fpb=" << framesPerBuffer << " nsamp=" << pbuf->nsamp << std::endl;
std::cerr << "idata=" << pbuf->idata << " fpb=" << framesPerBuffer << " nsamp=" << pbuf->nsamp << std::flush;
std::cerr << "</callback>" << std::endl;
return ret;
}
......@@ -124,13 +131,27 @@ int zmq_audiobuffer::stream_callback(
void zmq_audiobuffer::stream_finished(void *userData) {
zmq_audiobuffer *pbuf = (zmq_audiobuffer *)userData;
pbuf->playing = false;
zmq_audiobuffer::_playing_now = nullptr;
printf("Stream Completed: %s\n", pbuf->message);
}
int zmq_audiobuffer::play() {
if (zmq_audiobuffer * p = zmq_audiobuffer::_playing_now) {
std::cerr
<< "warning: zmq_audiobuffer::play: a buffer is already playing, stopping it"
<< std::endl;
p->stop();
}
std::cerr << "Start!" << std::endl;
zmq_audiobuffer::_playing_now = this;
idata = 0;
PaError err = Pa_Initialize();
if (err != paNoError)
return print_error(err);
PaStreamParameters outputParameters;
outputParameters.device =
Pa_GetDefaultOutputDevice(); // default output device
......@@ -145,7 +166,7 @@ int zmq_audiobuffer::play() {
Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err;
//PaError err;
err = Pa_OpenStream(&stream, NULL, // no input
&outputParameters, fsamp, frames_per_buffer,
paClipOff, // we won't output out of range samples so
......@@ -172,9 +193,18 @@ int zmq_audiobuffer::play() {
return paNoError;
}
void zmq_audiobuffer::abort() { Pa_AbortStream(stream); }
void zmq_audiobuffer::abort() {
std::cerr << "Abort!" << std::endl;
Pa_AbortStream(stream);
Pa_CloseStream(stream);
}
void zmq_audiobuffer::stop() { Pa_StopStream(stream); }
void zmq_audiobuffer::stop() {
std::cerr << "Stop!" << std::endl;
Pa_StopStream(stream);
Pa_CloseStream(stream);
Pa_Terminate();
}
int zmq_audiobuffer::fill() {
// initialise sinusoidal wavetable
......@@ -246,12 +276,31 @@ int zmq_recv_multi(zmq::socket_t &socket, zmq::message_t parts[], int nmax) {
return n;
}
int rtpriority(int n) {
int sched_policy = SCHED_FIFO;
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
if (n > sched_get_priority_max(sched_policy))
sched.sched_priority = sched_get_priority_max(sched_policy);
else
sched.sched_priority = n;
return sched_setscheduler(0, sched_policy, &sched);
}
int main(void) {
PaError err = Pa_Initialize();
if (err != paNoError)
return print_error(err);
int prio1 = 40;
int prio2 = 50;
rtpriority(prio2);
//PaError err = Pa_Initialize();
//if (err != paNoError)
// return print_error(err);
rtpriority(prio1);
// Prepare our context and socket
zmq::context_t context(1);
......@@ -260,6 +309,7 @@ int main(void) {
// ssub.connect("tcp://lsldert00.local:5557");
ssub.setsockopt(ZMQ_SUBSCRIBE, "A", 1);
rtpriority(prio2);
buffer[0] = new zmq_audiobuffer(44100, 5 * 48000, 2);
buffer[0]->fill();
......@@ -320,7 +370,7 @@ int main(void) {
}
else if (cmd == "AP") { // Play
if (buffer[ibuf]) {
err=buffer[ibuf]->play();
PaError err=buffer[ibuf]->play();
if (err != paNoError) {
print_error(err);
return err;
......@@ -348,8 +398,9 @@ int main(void) {
//if (err != paNoError)
//return print_error(err);
}
Pa_Terminate();
// Pa_Terminate();
printf("Test finished.\n");
return err;
return 0;
//return err;
}
......@@ -29,8 +29,13 @@ class zmq_audiobuffer {
void dump(int nsamples);
const zmq_audiobuffer *playing_now() {
return zmq_audiobuffer::_playing_now;
}
protected:
bool playing;
bool playing; // if this buffer is playing
static zmq_audiobuffer *_playing_now;// points to any buffer that is playing,
// else it's a nullptr
PaStream *stream;
static int stream_callback(const void *inputBuffer, void *outputBuffer,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment