Commit 00909b05 authored by yanmorin's avatar yanmorin

Removing portaudio cpp binding and debugging dtmf and audiolayer

parent 9fd31b32
......@@ -112,9 +112,10 @@ AudioLayer::startStream(void)
try {
ost::MutexLock guard(_mutex);
if (_stream && !_stream->isActive()) {
//_debug("Starting sound stream\n");
_debug("Starting sound stream\n");
_stream->start();
}
}
else { _debug ("stream doesn't exist or is already active\n"); }
} catch (const portaudio::PaException &e) {
_debugException("Portaudio error: error on starting audiolayer stream");
throw;
......@@ -136,7 +137,7 @@ AudioLayer::stopStream(void)
_micRingBuffer.flush();
}
} catch (const portaudio::PaException &e) {
_debugException("Portaudio error: error on stoping audiolayer stream");
_debugException("Portaudio error: stoping audiolayer stream failed");
throw;
} catch(...) {
_debugException("stream stop error");
......@@ -156,12 +157,14 @@ bool
AudioLayer::isStreamActive (void)
{
ost::MutexLock guard(_mutex);
if(_stream && _stream->isActive()) {
return true;
}
else {
return false;
try {
if(_stream && _stream->isActive()) {
return true;
}
} catch (const portaudio::PaException &e) {
_debugException("Portaudio error: isActive returned an error");
}
return false;
}
int
......@@ -189,6 +192,7 @@ AudioLayer::flushMain()
int
AudioLayer::putUrgent(void* buffer, int toCopy)
{
ost::MutexLock guard(_mutex);
if (_stream) {
int a = _urgentRingBuffer.AvailForPut();
if ( a >= toCopy ) {
......@@ -230,12 +234,14 @@ bool
AudioLayer::isStreamStopped (void)
{
ost::MutexLock guard(_mutex);
if(_stream && _stream->isStopped()) {
return true;
}
else {
return false;
try {
if(_stream && _stream->isStopped()) {
return true;
}
} catch (const portaudio::PaException &e) {
_debugException("Portaudio error: isStopped returned an exception");
}
return false;
}
int
......@@ -261,9 +267,10 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
// framePerBuffer are the number of int16 for one channel (left)
urgentAvail = _urgentRingBuffer.AvailForGet();
if (urgentAvail > 0) {
// Urgent data (dtmf, incoming call signal) come first.
// Urgent data (dtmf, incoming call signal) come first.
toGet = (urgentAvail < (int)(framesPerBuffer * sizeof(int16) * _outChannel)) ? urgentAvail : framesPerBuffer * sizeof(int16) * _outChannel;
_urgentRingBuffer.Get(out, toGet, spkrVolume);
_debug("out: %p, toGet: %d, spkrVolume: %d\n", out, toGet, spkrVolume);
// Consume the regular one as well (same amount of bytes)
_mainSndRingBuffer.Discard(toGet);
......@@ -283,7 +290,9 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
} else {
//_debug("padding %d...\n", (int)(framesPerBuffer * sizeof(int16)*_outChannel));
//_mainSndRingBuffer.debug();
bzero(out, framesPerBuffer * sizeof(int16) * _outChannel);
//portaudio::System::instance().sleep(framesPerBuffer*sizeof(int16));
bzero(out, framesPerBuffer * sizeof(int16) * _outChannel);
}
}
}
......@@ -293,6 +302,8 @@ AudioLayer::audioCallback (const void *inputBuffer, void *outputBuffer,
toPut = (micAvailPut <= (int)(framesPerBuffer * sizeof(int16) * _inChannel)) ? micAvailPut : framesPerBuffer * sizeof(int16) * _inChannel;
_micRingBuffer.Put(in, toPut, micVolume );
if (toPut==0 && toGet==0) { return 1; }
return paContinue;
}
......
/*
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Copyright (C) 2004-2006 Savoir-Faire Linux inc.
* Author : Yan Morin <yan.morin@savoirfairelinux.com>
* Author : Laurielle Lea <laurielle.lea@savoirfairelinux.com>
*
......@@ -23,33 +23,38 @@
#include "dtmf.h"
DTMF::DTMF (unsigned int sampleRate, unsigned int nbChannel) : dtmf(sampleRate, nbChannel) {
DTMF::DTMF (unsigned int sampleRate, unsigned int nbChannel)
: dtmfgenerator(sampleRate, nbChannel)
{
currentTone = 0;
newTone = 0;
}
DTMF::~DTMF (void) {
DTMF::~DTMF (void)
{
}
void
DTMF::startTone (char code) {
DTMF::startTone (char code)
{
newTone = code;
}
bool
DTMF::generateDTMF (int16* buffer, size_t n) {
if (!buffer) return false;
DTMF::generateDTMF (int16* buffer, size_t n)
{
if (!buffer) return false;
try {
if (currentTone != 0) {
// Currently generating a DTMF tone
if (currentTone == newTone) {
// Continue generating the same tone
dtmf.getNextSamples(buffer, n);
dtmfgenerator.getNextSamples(buffer, n);
return true;
} else if (newTone != 0) {
// New tone requested
dtmf.getSamples(buffer, n, newTone);
dtmfgenerator.getSamples(buffer, n, newTone);
currentTone = newTone;
return true;
} else {
......@@ -61,7 +66,7 @@ DTMF::generateDTMF (int16* buffer, size_t n) {
// Not generating any DTMF tone
if (newTone) {
// Requested to generate a DTMF tone
dtmf.getSamples(buffer, n, newTone);
dtmfgenerator.getSamples(buffer, n, newTone);
currentTone = newTone;
return true;
}
......
/*
* Copyright (C) 2004-2005-2006 Savoir-Faire Linux inc.
* Author : Yan Morin <yan.morin@savoirfairelinux.com>
* Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
*
* Portions Copyright (c) 2000 Billy Biggs <bbiggs@div8.net>
......@@ -31,20 +32,25 @@
*/
class DTMF {
public:
/**
* Create a new DTMF.
* @param samplingRate frequency of the sample (ex: 8000 hz)
*/
/**
* Create a new DTMF.
* @param samplingRate frequency of the sample (ex: 8000 hz)
*/
DTMF (unsigned int sampleRate, unsigned int nbChannel);
~DTMF (void);
void startTone (char);
bool generateDTMF (int16*, size_t);
/**
* Copy the sound inside the int16* buffer
* @param buffer : a int16* buffer
* @param n :
*/
bool generateDTMF (int16* buffer, size_t n);
char currentTone;
char newTone;
DTMFGenerator dtmf;
DTMFGenerator dtmfgenerator;
};
#endif // __KEY_DTMF_H_
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
/*
* Copyright (C) 2004-2006 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
* Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
*
......@@ -90,12 +90,17 @@ public:
/*
* Get n samples of the signal of code code
* @param buffer a int16 pointer to an allocated buffer
* @param n number of int16 to get, should be lower or equal to buffer size
* @parma code dtmf code to get sound
*/
void getSamples(int16* buffer, size_t n, unsigned char code) throw (DTMFException);
/*
* Get next n samples (continues where previous call to
* genSample or genNextSamples stopped
* @param buffer a int16 pointer to an allocated buffer
* @param n number of int16 to get, should be lower or equal to buffer size
*/
void getNextSamples(int16* buffer, size_t n) throw (DTMFException);
......
SUBDIRS = source include
\ No newline at end of file
#include <iostream>
#include "portaudiocpp/PortAudioCpp.hxx"
#ifdef WIN32
#include "portaudiocpp/AsioDeviceAdapter.hxx"
#endif
// ---------------------------------------------------------------------------------------
void printSupportedStandardSampleRates(
const portaudio::DirectionSpecificStreamParameters &inputParameters,
const portaudio::DirectionSpecificStreamParameters &outputParameters)
{
static double STANDARD_SAMPLE_RATES[] = {
8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0,
44100.0, 48000.0, 88200.0, 96000.0, -1 }; // negative terminated list
int printCount = 0;
for (int i = 0; STANDARD_SAMPLE_RATES[i] > 0; ++i)
{
portaudio::StreamParameters tmp = portaudio::StreamParameters(inputParameters, outputParameters, STANDARD_SAMPLE_RATES[i], 0, paNoFlag);
if (tmp.isSupported())
{
if (printCount == 0)
{
std::cout << " " << STANDARD_SAMPLE_RATES[i]; // 8.2
printCount = 1;
}
else if (printCount == 4)
{
std::cout << "," << std::endl;
std::cout << " " << STANDARD_SAMPLE_RATES[i]; // 8.2
printCount = 1;
}
else
{
std::cout << ", " << STANDARD_SAMPLE_RATES[i]; // 8.2
++printCount;
}
}
}
if (printCount == 0)
std::cout << "None" << std::endl;
else
std::cout << std::endl;
}
// ---------------------------------------------------------------------------------------
int main(int, char*[]);
int main(int, char*[])
{
try
{
portaudio::AutoSystem autoSys;
portaudio::System &sys = portaudio::System::instance();
std::cout << "PortAudio version number = " << sys.version() << std::endl;
std::cout << "PortAudio version text = '" << sys.versionText() << "'" << std::endl;
int numDevices = sys.deviceCount();
std::cout << "Number of devices = " << numDevices << std::endl;
for (portaudio::System::DeviceIterator i = sys.devicesBegin(); i != sys.devicesEnd(); ++i)
{
std::cout << "--------------------------------------- device #" << (*i).index() << std::endl;
// Mark global and API specific default devices:
bool defaultDisplayed = false;
if ((*i).isSystemDefaultInputDevice())
{
std::cout << "[ Default Input";
defaultDisplayed = true;
}
else if ((*i).isHostApiDefaultInputDevice())
{
std::cout << "[ Default " << (*i).hostApi().name() << " Input";
defaultDisplayed = true;
}
if ((*i).isSystemDefaultOutputDevice())
{
std::cout << (defaultDisplayed ? "," : "[");
std::cout << " Default Output";
defaultDisplayed = true;
}
else if ((*i).isHostApiDefaultOutputDevice())
{
std::cout << (defaultDisplayed ? "," : "[");
std::cout << " Default " << (*i).hostApi().name() << " Output";
defaultDisplayed = true;
}
if (defaultDisplayed)
std::cout << " ]" << std::endl;
// Print device info:
std::cout << "Name = " << (*i).name() << std::endl;
std::cout << "Host API = " << (*i).hostApi().name() << std::endl;
std::cout << "Max inputs = " << (*i).maxInputChannels() << ", Max outputs = " << (*i).maxOutputChannels() << std::endl;
std::cout << "Default low input latency = " << (*i).defaultLowInputLatency() << std::endl; // 8.3
std::cout << "Default low output latency = " << (*i).defaultLowOutputLatency() << std::endl; // 8.3
std::cout << "Default high input latency = " << (*i).defaultHighInputLatency() << std::endl; // 8.3
std::cout << "Default high output latency = " << (*i).defaultHighOutputLatency() << std::endl; // 8.3
#ifdef WIN32
// ASIO specific latency information:
if ((*i).hostApi().typeId() == paASIO)
{
portaudio::AsioDeviceAdapter asioDevice((*i));
std::cout << "ASIO minimum buffer size = " << asioDevice.minBufferSize() << std::endl;
std::cout << "ASIO maximum buffer size = " << asioDevice.maxBufferSize() << std::endl;
std::cout << "ASIO preferred buffer size = " << asioDevice.preferredBufferSize() << std::endl;
if (asioDevice.granularity() == -1)
std::cout << "ASIO buffer granularity = power of 2" << std::endl;
else
std::cout << "ASIO buffer granularity = " << asioDevice.granularity() << std::endl;
}
#endif // WIN32
std::cout << "Default sample rate = " << (*i).defaultSampleRate() << std::endl; // 8.2
// Poll for standard sample rates:
portaudio::DirectionSpecificStreamParameters inputParameters((*i), (*i).maxInputChannels(), portaudio::INT16, true, 0.0, NULL);
portaudio::DirectionSpecificStreamParameters outputParameters((*i), (*i).maxOutputChannels(), portaudio::INT16, true, 0.0, NULL);
if (inputParameters.numChannels() > 0)
{
std::cout << "Supported standard sample rates" << std::endl;
std::cout << " for half-duplex 16 bit " << inputParameters.numChannels() << " channel input = " << std::endl;
printSupportedStandardSampleRates(inputParameters, portaudio::DirectionSpecificStreamParameters::null());
}
if (outputParameters.numChannels() > 0)
{
std::cout << "Supported standard sample rates" << std::endl;
std::cout << " for half-duplex 16 bit " << outputParameters.numChannels() << " channel output = " << std::endl;
printSupportedStandardSampleRates(portaudio::DirectionSpecificStreamParameters::null(), outputParameters);
}
if (inputParameters.numChannels() > 0 && outputParameters.numChannels() > 0)
{
std::cout << "Supported standard sample rates" << std::endl;
std::cout << " for full-duplex 16 bit " << inputParameters.numChannels() << " channel input, " << outputParameters.numChannels() << " channel output = " << std::endl;
printSupportedStandardSampleRates(inputParameters, outputParameters);
}
}
std::cout << "----------------------------------------------" << std::endl;
}
catch (const portaudio::PaException &e)
{
std::cout << "A PortAudio error occured: " << e.paErrorText() << std::endl;
}
catch (const portaudio::PaCppException &e)
{
std::cout << "A PortAudioCpp error occured: " << e.what() << std::endl;
}
catch (const std::exception &e)
{
std::cout << "A generic exception occured: " << e.what() << std::endl;
}
catch (...)
{
std::cout << "An unknown exception occured." << std::endl;
}
return 0;
}
// ---------------------------------------------------------------------------------------
#include <iostream>
#include <cmath>
#include <cassert>
#include <cstddef>
#include "portaudiocpp/PortAudioCpp.hxx"
// ---------------------------------------------------------------------------------------
// Some constants:
const int NUM_SECONDS = 5;
const double SAMPLE_RATE = 44100.0;
const int FRAMES_PER_BUFFER = 64;
const int TABLE_SIZE = 200;
// ---------------------------------------------------------------------------------------
// SineGenerator class:
class SineGenerator
{
public:
SineGenerator(int tableSize) : tableSize_(tableSize), leftPhase_(0), rightPhase_(0)
{
const double PI = 3.14159265;
table_ = new float[tableSize];
for (int i = 0; i < tableSize; ++i)
{
table_[i] = 0.125f * (float)sin(((double)i/(double)tableSize)*PI*2.);
}
}
~SineGenerator()
{
delete[] table_; table_ = NULL;
}
int generate(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags)
{
assert(outputBuffer != NULL);
float **out = static_cast<float **>(outputBuffer);
for (unsigned int i = 0; i < framesPerBuffer; ++i)
{
out[0][i] = table_[leftPhase_];
out[1][i] = table_[rightPhase_];
leftPhase_ += 1;
if (leftPhase_ >= tableSize_)
leftPhase_ -= tableSize_;
rightPhase_ += 3;
if (rightPhase_ >= tableSize_)
rightPhase_ -= tableSize_;
}
return paContinue;
}
private:
float *table_;
int tableSize_;
int leftPhase_;
int rightPhase_;
};
// ---------------------------------------------------------------------------------------
// main:
int main(int, char *[]);
int main(int, char *[])
{
try
{
// Create a SineGenerator object:
SineGenerator sineGenerator(TABLE_SIZE);
std::cout << "Setting up PortAudio..." << std::endl;
// Set up the System:
portaudio::AutoSystem autoSys;
portaudio::System &sys = portaudio::System::instance();
// Set up the parameters required to open a (Callback)Stream:
portaudio::DirectionSpecificStreamParameters outParams(sys.defaultOutputDevice(), 2, portaudio::FLOAT32, false, sys.defaultOutputDevice().defaultLowOutputLatency(), NULL);
portaudio::StreamParameters params(portaudio::DirectionSpecificStreamParameters::null(), outParams, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff);
std::cout << "Opening stereo output stream..." << std::endl;
// Create (and open) a new Stream, using the SineGenerator::generate function as a callback:
portaudio::MemFunCallbackStream<SineGenerator> stream(params, sineGenerator, &SineGenerator::generate);
std::cout << "Starting playback for " << NUM_SECONDS << " seconds." << std::endl;
// Start the Stream (audio playback starts):
stream.start();
// Wait for 5 seconds:
sys.sleep(NUM_SECONDS * 1000);
std::cout << "Closing stream..." <<std::endl;
// Stop the Stream (not strictly needed as termintating the System will also stop all open Streams):
stream.stop();
// Close the Stream (not strictly needed as terminating the System will also close all open Streams):
stream.close();
// Terminate the System (not strictly needed as the AutoSystem will also take care of this when it
// goes out of scope):
sys.terminate();
std::cout << "Test finished." << std::endl;
}
catch (const portaudio::PaException &e)
{
std::cout << "A PortAudio error occured: " << e.paErrorText() << std::endl;
}
catch (const portaudio::PaCppException &e)
{
std::cout << "A PortAudioCpp error occured: " << e.what() << std::endl;
}
catch (const std::exception &e)
{
std::cout << "A generic exception occured: " << e.what() << std::endl;
}
catch (...)
{
std::cout << "An unknown exception occured." << std::endl;
}
return 0;
}
SUBDIRS = portaudiocpp
\ No newline at end of file
#ifndef INCLUDED_PORTAUDIO_ASIODEVICEADAPTER_HXX
#define INCLUDED_PORTAUDIO_ASIODEVICEADAPTER_HXX
namespace portaudio
{
// Forward declaration(s):
class Device;
// Declaration(s):
//////
/// @brief Adapts the given Device to an ASIO specific extension.
///
/// Deleting the AsioDeviceAdapter does not affect the underlaying
/// Device.
//////
class AsioDeviceAdapter
{
public:
AsioDeviceAdapter(Device &device);
Device &device();
long minBufferSize() const;
long maxBufferSize() const;
long preferredBufferSize() const;
long granularity() const;
void showControlPanel(void *systemSpecific);
private:
Device *device_;
long minBufferSize_;
long maxBufferSize_;
long preferredBufferSize_;
long granularity_;
};
}
#endif // INCLUDED_PORTAUDIO_ASIODEVICEADAPTER_HXX
#ifndef INCLUDED_PORTAUDIO_AUTOSYSTEM_HXX
#define INCLUDED_PORTAUDIO_AUTOSYSTEM_HXX
// ---------------------------------------------------------------------------------------
#include "portaudiocpp/System.hxx"
// ---------------------------------------------------------------------------------------
namespace portaudio
{
//////
/// @brief A RAII idiom class to ensure automatic clean-up when an exception is
/// raised.
///
/// A simple helper class which uses the 'Resource Acquisition is Initialization'
/// idiom (RAII). Use this class to initialize/terminate the System rather than
/// using System directly. AutoSystem must be created on stack and must be valid
/// throughout the time you wish to use PortAudioCpp. Your 'main' function might be
/// a good place for it.
///
/// To avoid having to type portaudio::System::instance().xyz() all the time, it's usually
/// a good idea to make a reference to the System which can be accessed directly.
/// @verbatim
/// portaudio::AutoSys autoSys;
/// portaudio::System &sys = portaudio::System::instance();
/// @endverbatim
//////
class AutoSystem
{
public:
AutoSystem(bool initialize = true)
{
if (initialize)
System::initialize();
}
~AutoSystem()
{
if (System::exists())
System::terminate();
}
void initialize()
{
System::initialize();
}
void terminate()
{
System::terminate();
}
};
} // namespace portaudio
// ---------------------------------------------------------------------------------------
#endif // INCLUDED_PORTAUDIO_AUTOSYSTEM_HXX
#ifndef INCLUDED_PORTAUDIO_BLOCKINGSTREAM_HXX
#define INCLUDED_PORTAUDIO_BLOCKINGSTREAM_HXX
// ---------------------------------------------------------------------------------------
#include "portaudiocpp/Stream.hxx"
// ---------------------------------------------------------------------------------------
namespace portaudio
{
//////
/// @brief Stream class for blocking read/write-style input and output.
//////
class BlockingStream : public Stream
{
public:
BlockingStream();
BlockingStream(const StreamParameters &parameters);
~BlockingStream();
void open(const StreamParameters &parameters);
void read(void *buffer, unsigned long numFrames);
void write(const void *buffer, unsigned long numFrames);
signed long availableReadSize() const;
signed long availableWriteSize() const;
private:
BlockingStream(const BlockingStream &); // non-copyable
BlockingStream &operator=(const BlockingStream &); // non-copyable
};
} // portaudio
// ---------------------------------------------------------------------------------------
#endif // INCLUDED_PORTAUDIO_BLOCKINGSTREAM_HXX
#ifndef INCLUDED_PORTAUDIO_CFUNCALLBACKSTREAM_HXX
#define INCLUDED_PORTAUDIO_CFUNCALLBACKSTREAM_HXX
// ---------------------------------------------------------------------------------------
#include "portaudio.h"
#include "portaudiocpp/CallbackStream.hxx"
// ---------------------------------------------------------------------------------------
// Forward declaration(s)
namespace portaudio