Commit 9269a6b0 authored by Your Name's avatar Your Name
Browse files

more portaudio in zmqaudio.cc

parent a9592890
......@@ -10,7 +10,7 @@ LDLIBS+=-lzmq -lboost_program_options -lpigpio -llsl32
#INCLUDE+=RS-232
CXXFLAGS+=-Wall -std=c++11
all: zmq_trigger playtone msgqueue_pubsub zmq_trigger_subscriber playsound lsldertc hwserver hwserver2 hwserver3
all: zmq_trigger playtone msgqueue_pubsub zmq_trigger_subscriber playsound lsldertc hwserver hwserver2 hwserver3 zmqaudio
lsldertc: lsldertc.c
hwserver: hwserver.c
......@@ -23,7 +23,8 @@ playtone: playtone.cc
zmq_trigger: zmq_trigger.cc
msgqueue_pubsub: msgqueue_pubsub.cc
zmq_trigger_subscriber: zmq_trigger_subscriber.cc
zmqaudio: zmqaudio.cc zmqaudio.h
paex_sine: paex_sine.c
paex_sine: LDLIBS+=-lm -lportaudio
zmqaudio paex_sine: LDLIBS+=-lm -lportaudio
sine: sine.cc
sine: LDLIBS+=-lportaudio -lportaudiocpp
To get rid of the errors of ALSA PCM devices when using portaudio
comment out these devices in /usr/share/alsa/alsa.conf like below
#
# PCM interface
#
# redirect to load-on-demand extended pcm definitions
pcm.cards cards.pcm
pcm.default cards.pcm.default
pcm.sysdefault cards.pcm.default
pcm.front cards.pcm.front
#GWpcm.rear cards.pcm.rear
#GWpcm.center_lfe cards.pcm.center_lfe
#GWpcm.side cards.pcm.side
pcm.surround21 cards.pcm.surround21
pcm.surround40 cards.pcm.surround40
pcm.surround41 cards.pcm.surround41
pcm.surround50 cards.pcm.surround50
pcm.surround51 cards.pcm.surround51
pcm.surround71 cards.pcm.surround71
pcm.iec958 cards.pcm.iec958
pcm.spdif iec958
#GWpcm.hdmi cards.pcm.hdmi
pcm.dmix cards.pcm.dmix
pcm.dsnoop cards.pcm.dsnoop
#GWpcm.modem cards.pcm.modem
#GWpcm.phoneline cards.pcm.phoneline
No preview for this file type
File added
#include <iostream>
int main()
{
std::cout << "float " << sizeof(float) << std::endl
<< "double " << sizeof(double) << std::endl;
}
......@@ -419,7 +419,7 @@ int main(int argc, const char *argv[]) {
cout << "sound_cmd=" << sound_cmd << endl;
cout << "sound_arg=" << sound_arg << endl;
}
lsldert_sound_cmd(sound_cmd, sound_arg, msg, nmsg);
//lsldert_sound_cmd(sound_cmd, sound_arg, msg, nmsg);
}
}
}
......
/** @file paex_sine.c
@ingroup examples_src
@brief Play a sine wave for several seconds.
@author Ross Bencina <rossb@audiomulch.com>
@author Phil Burk <philburk@softsynth.com>
*/
/*
* $Id$
*
* This program uses the PortAudio Portable Audio Library.
* For more information see: http://www.portaudio.com/
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
#include "zmqaudio.h"
#include "portaudio.h"
#include <math.h>
#include <stdio.h>
#define NUM_SECONDS (5)
#define SAMPLE_RATE (44100)
#ifndef M_PI
#define M_PI (3.14159265)
#endif
#define TABLE_SIZE (200)
typedef struct {
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
char message[20];
} paTestData;
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
int zmq_audiobuffer::stream_callback(
const void * /*inputBuffer*/, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo * /*timeInfo*/,
PaStreamCallbackFlags /*statusFlags*/, void *userData) {
zmq_audiobuffer *data = (zmq_audiobuffer *)userData;
float *out = (float *)outputBuffer;
unsigned long i;
for (i = 0; i < framesPerBuffer; i++) {
if (data->idata < data->nsamp) {
*out++ = data->data[2*(data->idata)]; // left
*out++ = data->data[2*(data->idata)+1]; // right
data->idata++;
}
else {
*out++ = 0;
*out++ = 0;
}
}
return paContinue;
}
/*
* This routine is called by portaudio when playback is done.
*/
void zmq_audiobuffer::stream_finished(void *userData) {
zmq_audiobuffer *data = (zmq_audiobuffer *)userData;
printf("Stream Completed: %s\n", data->message);
}
int zmq_audiobuffer::play()
{
idata=0;
PaStreamParameters outputParameters;
outputParameters.device =
Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice) {
fprintf(stderr, "Error: No default output device.\n");
return 1;
}
outputParameters.channelCount = nchan; /* stereo output */
outputParameters.sampleFormat =
paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency =
Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaStream *stream;
PaError err;
err = Pa_OpenStream(&stream, NULL, /* no input */
&outputParameters, fsamp, frames_per_buffer,
paClipOff, /* we won't output out of range samples so
don't bother clipping them */
zmq_audiobuffer::stream_callback, this);
if (err != paNoError)
return err;
sprintf(message, "No Message");
err =
Pa_SetStreamFinishedCallback(stream, &zmq_audiobuffer::stream_finished);
if (err != paNoError)
return err;
err = Pa_StartStream(stream);
if (err != paNoError)
return err;
printf("Play for %d seconds.\n", NUM_SECONDS);
Pa_Sleep((unsigned int)(duration() * 1000));
err = Pa_StopStream(stream);
if (err != paNoError)
return err;
err = Pa_CloseStream(stream);
if (err != paNoError)
return err;
return err;
}
void zmq_audiobuffer::fill()
{
// initialise sinusoidal wavetable
const float fl {440};
const float fr {880};
const float vol {0.1};
for (int i = 0; i < nsamp-1; i++) {
data[2*i] = vol*sin((2*M_PI*fl*i)/fsamp);
data[2*i+1] = vol*sin((2*M_PI*fr*i)/fsamp);
}
}
PaError print_error(PaError e)
{
if (e != paNoError)
Pa_Terminate();
fprintf(stderr, "An error occured while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", e);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(e));
return e;
}
/*******************************************************************/
int main(void);
int main(void) {
PaError err = Pa_Initialize();
if (err != paNoError)
return print_error(err);
zmq_audiobuffer data(44100, 5*48000, 2);
data.fill();
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n",
SAMPLE_RATE, data.frames_per_buffer);
err = data.play();
if (err != paNoError)
return print_error(err);
Pa_Terminate();
printf("Test finished.\n");
return err;
}
#include "portaudio.h"
#include <cassert>
#include <cstdint>
class zmq_audiobuffer {
public:
std::uint32_t fsamp;
std::uint32_t nsamp;
std::uint32_t nchan;
float *data;
std::uint32_t idata;
char message[20];
int frames_per_buffer;
zmq_audiobuffer(std::uint32_t _fsamp, std::uint32_t _nsamp,
std::uint32_t _nchan)
: fsamp{_fsamp}, nsamp{_nsamp}, nchan{_nchan},
data{new float[_nsamp * _nchan]}, idata{0}, frames_per_buffer{64} {
assert(sizeof(float) == 4); // need 32 bit IEEE 754 floats
}
~zmq_audiobuffer() { delete data; }
void fill();
int play();
double duration() {
return double(nsamp)/fsamp;
}
static int stream_callback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData);
static void stream_finished(void *userData);
};
Supports Markdown
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