[asterisk-commits] russell: branch russell/indications r174541 - /team/russell/indications/main/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Feb 10 08:53:06 CST 2009
Author: russell
Date: Tue Feb 10 08:53:06 2009
New Revision: 174541
URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=174541
Log:
Break out tone definition parsing into its own function
Modified:
team/russell/indications/main/indications.c
Modified: team/russell/indications/main/indications.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/indications/main/indications.c?view=diff&rev=174541&r1=174540&r2=174541
==============================================================================
--- team/russell/indications/main/indications.c (original)
+++ team/russell/indications/main/indications.c Tue Feb 10 08:53:06 2009
@@ -27,7 +27,6 @@
* XXX TODO
* - API documentation
* - fix users of the API to account for ref count (only chan_unistim left)
- * - Integrate work to pull out tone parsing into an API
* - change the code such that tone definitions are only parsed once
* instead of every time they are used.
*/
@@ -113,6 +112,14 @@
*/
static struct ast_tone_zone *default_tone_zone;
+struct ast_tone_zone_part {
+ unsigned int freq1;
+ unsigned int freq2;
+ unsigned int time;
+ unsigned int modulate:1;
+ unsigned int midinote:1;
+};
+
struct playtones_item {
int fac1;
int init_v2_1;
@@ -285,6 +292,63 @@
.generate = playtones_generator,
};
+static int ast_tone_zone_part_parse(const char *s, struct ast_tone_zone_part *tone_data)
+{
+ if (sscanf(s, "%u+%u/%u", &tone_data->freq1, &tone_data->freq2,
+ &tone_data->time) == 3) {
+ /* f1+f2/time format */
+ } else if (sscanf(s, "%u+%u", &tone_data->freq1, &tone_data->freq2) == 2) {
+ /* f1+f2 format */
+ tone_data->time = 0;
+ } else if (sscanf(s, "%u*%u/%u", &tone_data->freq1, &tone_data->freq2,
+ &tone_data->time) == 3) {
+ /* f1*f2/time format */
+ tone_data->modulate = 1;
+ } else if (sscanf(s, "%u*%u", &tone_data->freq1, &tone_data->freq2) == 2) {
+ /* f1*f2 format */
+ tone_data->time = 0;
+ tone_data->modulate = 1;
+ } else if (sscanf(s, "%u/%u", &tone_data->freq1, &tone_data->time) == 2) {
+ /* f1/time format */
+ tone_data->freq2 = 0;
+ } else if (sscanf(s, "%u", &tone_data->freq1) == 1) {
+ /* f1 format */
+ tone_data->freq2 = 0;
+ tone_data->time = 0;
+ } else if (sscanf(s, "M%u+M%u/%u", &tone_data->freq1, &tone_data->freq2,
+ &tone_data->time) == 3) {
+ /* Mf1+Mf2/time format */
+ tone_data->midinote = 1;
+ } else if (sscanf(s, "M%u+M%u", &tone_data->freq1, &tone_data->freq2) == 2) {
+ /* Mf1+Mf2 format */
+ tone_data->time = 0;
+ tone_data->midinote = 1;
+ } else if (sscanf(s, "M%u*M%u/%u", &tone_data->freq1, &tone_data->freq2,
+ &tone_data->time) == 3) {
+ /* Mf1*Mf2/time format */
+ tone_data->modulate = 1;
+ tone_data->midinote = 1;
+ } else if (sscanf(s, "M%u*M%u", &tone_data->freq1, &tone_data->freq2) == 2) {
+ /* Mf1*Mf2 format */
+ tone_data->time = 0;
+ tone_data->modulate = 1;
+ tone_data->midinote = 1;
+ } else if (sscanf(s, "M%u/%u", &tone_data->freq1, &tone_data->time) == 2) {
+ /* Mf1/time format */
+ tone_data->freq2 = -1;
+ tone_data->midinote = 1;
+ } else if (sscanf(s, "M%u", &tone_data->freq1) == 1) {
+ /* Mf1 format */
+ tone_data->freq2 = -1;
+ tone_data->time = 0;
+ tone_data->midinote = 1;
+ } else {
+ return -1;
+ }
+
+ return 0;
+}
+
int ast_playtones_start(struct ast_channel *chan, int vol, const char *playlst, int interruptible)
{
char *s, *data = ast_strdupa(playlst);
@@ -308,7 +372,9 @@
}
while ((s = strsep(&stringp, separator)) && !ast_strlen_zero(s)) {
- int freq1, freq2, duration, modulate = 0, midinote = 0;
+ struct ast_tone_zone_part tone_data = {
+ .time = 0,
+ };
s = ast_strip(s);
@@ -318,72 +384,25 @@
d.reppos = d.nitems;
}
- if (sscanf(s, "%d+%d/%d", &freq1, &freq2, &duration) == 3) {
- /* f1+f2/time format */
- } else if (sscanf(s, "%d+%d", &freq1, &freq2) == 2) {
- /* f1+f2 format */
- duration = 0;
- } else if (sscanf(s, "%d*%d/%d", &freq1, &freq2, &duration) == 3) {
- /* f1*f2/time format */
- modulate = 1;
- } else if (sscanf(s, "%d*%d", &freq1, &freq2) == 2) {
- /* f1*f2 format */
- duration = 0;
- modulate = 1;
- } else if (sscanf(s, "%d/%d", &freq1, &duration) == 2) {
- /* f1/time format */
- freq2 = 0;
- } else if (sscanf(s, "%d", &freq1) == 1) {
- /* f1 format */
- freq2 = 0;
- duration = 0;
- } else if (sscanf(s, "M%d+M%d/%d", &freq1, &freq2, &duration) == 3) {
- /* Mf1+Mf2/time format */
- midinote = 1;
- } else if (sscanf(s, "M%d+M%d", &freq1, &freq2) == 2) {
- /* Mf1+Mf2 format */
- duration = 0;
- midinote = 1;
- } else if (sscanf(s, "M%d*M%d/%d", &freq1, &freq2, &duration) == 3) {
- /* Mf1*Mf2/time format */
- modulate = 1;
- midinote = 1;
- } else if (sscanf(s, "M%d*M%d", &freq1, &freq2) == 2) {
- /* Mf1*Mf2 format */
- duration = 0;
- modulate = 1;
- midinote = 1;
- } else if (sscanf(s, "M%d/%d", &freq1, &duration) == 2) {
- /* Mf1/time format */
- freq2 = -1;
- midinote = 1;
- } else if (sscanf(s, "M%d", &freq1) == 1) {
- /* Mf1 format */
- freq2 = -1;
- duration = 0;
- midinote = 1;
- } else {
- ast_log(LOG_WARNING,"%s: tone component '%s' of '%s' is no good\n", chan->name, s, playlst);
- return -1;
- }
-
- if (midinote) {
+ if (ast_tone_zone_part_parse(s, &tone_data))
+
+ if (tone_data.midinote) {
/* midi notes must be between 0 and 127 */
- switch (freq1) {
+ switch (tone_data.freq1) {
case 0 ... 127:
- freq1 = midi_tohz[freq1];
+ tone_data.freq1 = midi_tohz[tone_data.freq1];
break;
default:
- freq1 = 0;
+ tone_data.freq1 = 0;
}
- switch (freq2) {
+ switch (tone_data.freq2) {
case 0 ... 127:
- freq2 = midi_tohz[freq2];
+ tone_data.freq2 = midi_tohz[tone_data.freq2];
break;
default:
- freq2 = 0;
+ tone_data.freq2 = 0;
}
}
@@ -391,15 +410,15 @@
return -1;
}
- d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0;
- d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * d.vol;
- d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * d.vol;
-
- d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0;
- d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * d.vol;
- d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * d.vol;
- d.items[d.nitems].duration = duration;
- d.items[d.nitems].modulate = modulate;
+ d.items[d.nitems].fac1 = 2.0 * cos(2.0 * M_PI * (tone_data.freq1 / 8000.0)) * 32768.0;
+ d.items[d.nitems].init_v2_1 = sin(-4.0 * M_PI * (tone_data.freq1 / 8000.0)) * d.vol;
+ d.items[d.nitems].init_v3_1 = sin(-2.0 * M_PI * (tone_data.freq1 / 8000.0)) * d.vol;
+
+ d.items[d.nitems].fac2 = 2.0 * cos(2.0 * M_PI * (tone_data.freq2 / 8000.0)) * 32768.0;
+ d.items[d.nitems].init_v2_2 = sin(-4.0 * M_PI * (tone_data.freq2 / 8000.0)) * d.vol;
+ d.items[d.nitems].init_v3_2 = sin(-2.0 * M_PI * (tone_data.freq2 / 8000.0)) * d.vol;
+ d.items[d.nitems].duration = tone_data.time;
+ d.items[d.nitems].modulate = tone_data.modulate;
d.nitems++;
}
More information about the asterisk-commits
mailing list