Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
savoirfairelinux
jami-daemon
Commits
765508ff
Commit
765508ff
authored
Oct 13, 2011
by
Tristan Matthews
Browse files
* #7165: refactored dtmfgenerator
parent
582df801
Changes
2
Hide whitespace changes
Inline
Side-by-side
daemon/src/audio/sound/dtmfgenerator.cpp
View file @
765508ff
...
...
@@ -33,16 +33,16 @@
* as that of the covered work.
*/
#include
<math.h>
#include
<cmath>
#include
<cassert>
#include
"dtmfgenerator.h"
#include
"global.h"
/*
* Tone frequencies
*/
const
DTMFGenerator
::
DTMFTone
DTMFGenerator
::
tones
[
NUM_TONES
]
=
{
const
DTMFGenerator
::
DTMFTone
DTMFGenerator
::
tones
_
[
NUM_TONES
]
=
{
{
'0'
,
941
,
1336
},
{
'1'
,
697
,
1209
},
{
'2'
,
697
,
1336
},
...
...
@@ -65,169 +65,83 @@ const DTMFGenerator::DTMFTone DTMFGenerator::tones[NUM_TONES] = {
/*
* Initialize the generator
*/
DTMFGenerator
::
DTMFGenerator
(
unsigned
int
sampleRate
)
:
state
(),
_
sampleRate
(
sampleRate
),
tone
(
""
,
sampleRate
)
DTMFGenerator
::
DTMFGenerator
(
unsigned
int
sampleRate
)
:
state
(),
sampleRate
_
(
sampleRate
),
tone
_
(
""
,
sampleRate
)
{
state
.
offset
=
0
;
state
.
sample
=
0
;
for
(
int
i
=
0
;
i
<
NUM_TONES
;
i
++
)
{
samples
[
i
]
=
generateSample
(
i
);
}
for
(
int
i
=
0
;
i
<
NUM_TONES
;
i
++
)
toneBuffers_
[
i
]
=
fillToneBuffer
(
i
);
}
DTMFGenerator
::~
DTMFGenerator
()
{
for
(
int
i
=
0
;
i
<
NUM_TONES
;
i
++
)
{
delete
[]
samples
[
i
];
samples
[
i
]
=
NULL
;
}
for
(
int
i
=
0
;
i
<
NUM_TONES
;
i
++
)
delete
[]
toneBuffers_
[
i
];
}
/*
* Get n samples of the signal of code code
*/
void
DTMFGenerator
::
getSamples
(
SFLDataFormat
*
buffer
,
size_t
n
,
unsigned
char
code
)
throw
(
DTMFException
)
void
DTMFGenerator
::
getSamples
(
SFLDataFormat
*
buffer
,
size_t
n
,
unsigned
char
code
)
{
size_t
i
;
if
(
!
buffer
)
{
if
(
!
buffer
)
throw
DTMFException
(
"Invalid parameter value"
);
}
switch
(
code
)
{
case
'0'
:
state
.
sample
=
samples
[
0
];
break
;
case
'1'
:
state
.
sample
=
samples
[
1
];
break
;
case
'2'
:
state
.
sample
=
samples
[
2
];
break
;
case
'3'
:
state
.
sample
=
samples
[
3
];
break
;
case
'4'
:
state
.
sample
=
samples
[
4
];
break
;
case
'5'
:
state
.
sample
=
samples
[
5
];
break
;
case
'6'
:
state
.
sample
=
samples
[
6
];
break
;
case
'7'
:
state
.
sample
=
samples
[
7
];
break
;
case
'8'
:
state
.
sample
=
samples
[
8
];
break
;
case
'9'
:
state
.
sample
=
samples
[
9
];
break
;
case
'A'
:
case
'a'
:
state
.
sample
=
samples
[
10
];
break
;
case
'B'
:
case
'b'
:
state
.
sample
=
samples
[
11
];
break
;
case
'C'
:
case
'c'
:
state
.
sample
=
samples
[
12
];
break
;
case
'D'
:
case
'd'
:
state
.
sample
=
samples
[
13
];
break
;
case
'*'
:
state
.
sample
=
samples
[
14
];
break
;
case
'#'
:
state
.
sample
=
samples
[
15
];
break
;
default:
throw
DTMFException
(
"Invalid code"
);
return
;
break
;
code
=
toupper
(
code
);
if
(
code
>=
'0'
and
code
<=
'9'
)
state
.
sample
=
toneBuffers_
[
code
-
'0'
];
else
if
(
code
>=
'A'
and
code
<=
'D'
)
state
.
sample
=
toneBuffers_
[
code
-
'A'
+
10
];
else
{
switch
(
code
)
{
case
'*'
:
state
.
sample
=
toneBuffers_
[
14
];
break
;
case
'#'
:
state
.
sample
=
toneBuffers_
[
15
];
break
;
default:
throw
DTMFException
(
"Invalid code"
);
break
;
}
}
for
(
i
=
0
;
i
<
n
;
i
++
)
{
buffer
[
i
]
=
state
.
sample
[
i
%
_sampleRate
];
}
size_t
i
;
for
(
i
=
0
;
i
<
n
;
++
i
)
buffer
[
i
]
=
state
.
sample
[
i
%
sampleRate_
];
state
.
offset
=
i
%
_
sampleRate
;
state
.
offset
=
i
%
sampleRate
_
;
}
/*
* Get next n samples (continues where previous call to
* genSample or genNextSamples stopped
*/
void
DTMFGenerator
::
getNextSamples
(
SFLDataFormat
*
buffer
,
size_t
n
)
throw
(
DTMFException
)
void
DTMFGenerator
::
getNextSamples
(
SFLDataFormat
*
buffer
,
size_t
n
)
{
size_t
i
;
if
(
!
buffer
)
{
if
(
!
buffer
)
throw
DTMFException
(
"Invalid parameter"
);
}
if
(
state
.
sample
==
0
)
{
if
(
state
.
sample
==
0
)
throw
DTMFException
(
"DTMF generator not initialized"
);
}
for
(
i
=
0
;
i
<
n
;
i
++
)
{
buffer
[
i
]
=
state
.
sample
[(
state
.
offset
+
i
)
%
_sampleRate
];
}
for
(
i
=
0
;
i
<
n
;
i
++
)
buffer
[
i
]
=
state
.
sample
[(
state
.
offset
+
i
)
%
sampleRate_
];
state
.
offset
=
(
state
.
offset
+
i
)
%
_
sampleRate
;
state
.
offset
=
(
state
.
offset
+
i
)
%
sampleRate
_
;
}
/*
* Generate a tone sample
*/
SFLDataFormat
*
DTMFGenerator
::
generateSample
(
unsigned
char
code
)
throw
(
DTMFException
)
SFLDataFormat
*
DTMFGenerator
::
fillToneBuffer
(
int
index
)
{
SFLDataFormat
*
ptr
;
try
{
ptr
=
new
SFLDataFormat
[
_sampleRate
];
if
(
!
ptr
)
{
throw
new
DTMFException
(
"No memory left"
);
return
0
;
}
tone
.
genSin
(
ptr
,
tones
[
code
].
higher
,
tones
[
code
].
lower
,
_sampleRate
);
return
ptr
;
}
catch
(...)
{
throw
new
DTMFException
(
"No memory left"
);
return
0
;
}
assert
(
index
>=
0
and
index
<
NUM_TONES
);
SFLDataFormat
*
ptr
=
new
SFLDataFormat
[
sampleRate_
];
tone_
.
genSin
(
ptr
,
tones_
[
index
].
higher
,
tones_
[
index
].
lower
,
sampleRate_
);
return
ptr
;
}
daemon/src/audio/sound/dtmfgenerator.h
View file @
765508ff
...
...
@@ -75,16 +75,16 @@ class DTMFGenerator {
DTMFState
state
;
/** The different kind of tones */
static
const
DTMFTone
tones
[
NUM_TONES
];
static
const
DTMFTone
tones
_
[
NUM_TONES
];
/** Generated samples */
SFLDataFormat
*
samples
[
NUM_TONES
];
/** Generated samples
for each tone
*/
SFLDataFormat
*
toneBuffers_
[
NUM_TONES
];
/** Sampling rate of generated dtmf */
int
_
sampleRate
;
int
sampleRate
_
;
/** A tone object */
Tone
tone
;
Tone
tone
_
;
public:
/**
...
...
@@ -112,7 +112,7 @@ class DTMFGenerator {
* @param n number of sampling to get, should be lower or equal to buffer size
* @param code dtmf code to get sound
*/
void
getSamples
(
SFLDataFormat
*
buffer
,
size_t
n
,
unsigned
char
code
)
throw
(
DTMFException
)
;
void
getSamples
(
SFLDataFormat
*
buffer
,
size_t
n
,
unsigned
char
code
);
/*
* Get next n samples (continues where previous call to
...
...
@@ -120,16 +120,16 @@ class DTMFGenerator {
* @param buffer a SFLDataFormat pointer to an allocated buffer
* @param n number of sampling to get, should be lower or equal to buffer size
*/
void
getNextSamples
(
SFLDataFormat
*
buffer
,
size_t
n
)
throw
(
DTMFException
)
;
void
getNextSamples
(
SFLDataFormat
*
buffer
,
size_t
n
);
private:
/**
*
Generate samples for a specific dtmf code
* @param
code The code
*
Fill tone buffer for a given index of the array of tones.
* @param
index of the tone in the array tones_
* @return SFLDataFormat* The generated data
*/
SFLDataFormat
*
generateSample
(
unsigned
char
code
)
throw
(
DTMFException
);
SFLDataFormat
*
fillToneBuffer
(
int
index
);
};
#endif // DTMFGENERATOR_H
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment