Commit 33874733 authored by Tristan Matthews's avatar Tristan Matthews

* #14457: dtmf: use std::vector instead of new [] SFLDataFormat

parent 31c50af3
......@@ -43,20 +43,20 @@ void DTMF::startTone(char code)
newTone_ = code;
}
bool DTMF::generateDTMF(SFLDataFormat* buffer, size_t n)
{
if (!buffer) return false;
using std::vector;
bool DTMF::generateDTMF(vector<SFLDataFormat> &buffer)
{
try {
if (currentTone_ != 0) {
// Currently generating a DTMF tone
if (currentTone_ == newTone_) {
// Continue generating the same tone
dtmfgenerator_.getNextSamples(buffer, n);
dtmfgenerator_.getNextSamples(buffer);
return true;
} else if (newTone_ != 0) {
// New tone requested
dtmfgenerator_.getSamples(buffer, n, newTone_);
dtmfgenerator_.getSamples(buffer, newTone_);
currentTone_ = newTone_;
return true;
} else {
......@@ -68,7 +68,7 @@ bool DTMF::generateDTMF(SFLDataFormat* buffer, size_t n)
// Not generating any DTMF tone
if (newTone_) {
// Requested to generate a DTMF tone
dtmfgenerator_.getSamples(buffer, n, newTone_);
dtmfgenerator_.getSamples(buffer, newTone_);
currentTone_ = newTone_;
return true;
}
......
......@@ -58,10 +58,9 @@ class DTMF {
/**
* Copy the sound inside the sampling* buffer
* @param buffer : a SFLDataFormat* buffer
* @param n : The size to generate
* @param buffer : a vector of SFLDataFormat
*/
bool generateDTMF(SFLDataFormat* buffer, size_t n);
bool generateDTMF(std::vector<SFLDataFormat> &buffer);
private:
char currentTone_;
......
......@@ -37,7 +37,6 @@
#include <cassert>
#include "dtmfgenerator.h"
#include "global.h"
/*
* Tone frequencies
......@@ -81,14 +80,13 @@ DTMFGenerator::~DTMFGenerator()
delete [] toneBuffers_[i];
}
using std::vector;
/*
* Get n samples of the signal of code code
*/
void DTMFGenerator::getSamples(SFLDataFormat* buffer, size_t n, unsigned char code)
void DTMFGenerator::getSamples(vector<SFLDataFormat> &buffer, unsigned char code)
{
if (!buffer)
throw DTMFException("Invalid parameter value");
code = toupper(code);
if (code >= '0' and code <= '9')
......@@ -112,6 +110,7 @@ void DTMFGenerator::getSamples(SFLDataFormat* buffer, size_t n, unsigned char co
}
size_t i;
const size_t n = buffer.size();
for (i = 0; i < n; ++i)
buffer[i] = state.sample[i % sampleRate_];
......@@ -122,15 +121,13 @@ void DTMFGenerator::getSamples(SFLDataFormat* buffer, size_t n, unsigned char co
* Get next n samples (continues where previous call to
* genSample or genNextSamples stopped
*/
void DTMFGenerator::getNextSamples(SFLDataFormat* buffer, size_t n)
void DTMFGenerator::getNextSamples(vector<SFLDataFormat> &buffer)
{
if (!buffer)
throw DTMFException("Invalid parameter");
if (state.sample == 0)
throw DTMFException("DTMF generator not initialized");
size_t i;
const size_t n = buffer.size();
for (i = 0; i < n; i++)
buffer[i] = state.sample[(state.offset + i) % sampleRate_];
......
......@@ -38,6 +38,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include "noncopyable.h"
#include "tone.h"
......@@ -100,19 +101,17 @@ class DTMFGenerator {
/*
* Get n samples of the signal of code code
* @param buffer a SFLDataFormat pointer to an allocated buffer
* @param n number of sampling to get, should be lower or equal to buffer size
* @param buffer a SFLDataFormat vector
* @param code dtmf code to get sound
*/
void getSamples(SFLDataFormat* buffer, size_t n, unsigned char code);
void getSamples(std::vector<SFLDataFormat> &buffer, unsigned char code);
/*
* Get next n samples (continues where previous call to
* genSample or genNextSamples stopped
* @param buffer a SFLDataFormat pointer to an allocated buffer
* @param n number of sampling to get, should be lower or equal to buffer size
* @param buffer a SFLDataFormat vector
*/
void getNextSamples(SFLDataFormat* buffer, size_t n);
void getNextSamples(std::vector<SFLDataFormat> &buffer);
private:
......
......@@ -1301,24 +1301,22 @@ void ManagerImpl::playDtmf(char code)
// this buffer is for mono
// TODO <-- this should be global and hide if same size
SFLDataFormat *buf = new SFLDataFormat[size];
std::vector<SFLDataFormat> buf(size);
// Handle dtmf
dtmfKey_->startTone(code);
// copy the sound
if (dtmfKey_->generateDTMF(buf, size)) {
if (dtmfKey_->generateDTMF(buf)) {
// Put buffer to urgentRingBuffer
// put the size in bytes...
// so size * 1 channel (mono) * sizeof (bytes for the data)
// audiolayer->flushUrgent();
audiodriver_->startStream();
audiodriver_->putUrgent(buf, size * sizeof(SFLDataFormat));
audiodriver_->putUrgent(&(*buf.begin()), size * sizeof(SFLDataFormat));
}
// TODO Cache the DTMF
delete [] buf;
}
// Multi-thread
......
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