📄 tone_gen.c
字号:
/* to 3GPP2 Technical Specifications Group C as a basis for */
/* discussion and should not be construed as a binding proposal on */
/* Lucent Technologies Inc. or any other company. Specifically, */
/* Lucent Technologies Inc. reserves the right to modify, amend, or */
/* withdraw the statements contained herein. */
/* */
/* Permission is granted to 3GPP2 Technical Specifications Group C */
/* participants to copy any portion of this document for legitimate */
/* purposes of 3GPP2 Technical Specifications Group C. Copying for */
/* monetary gain or other non-3GPP2 Technical Specifications Group C */
/* purposes is prohibited. */
/* */
/*-------------------------------------------------------------------*/
/* */
/* Grant of license Motorola Inc. grants a free, irrevocable license */
/* to 3GPP2 and its organizational partners to incorporate Motorola- */
/* supplied text or other copyrightable material contained in the */
/* contribution and any modifications thereof in the creation of */
/* 3GPP2 publications, to copyright and sell in organizational */
/* partners name any organizational partners standards publications */
/* even though it may include portions of the contribution; and at */
/* the organizational partners sole discretion to permit others */
/* to reproduce in whole or in part such contributions or the */
/* resulting organizational partners standards publication. Motorola */
/* is also willing to grant licenses under such Motorola copyrights */
/* to third parties on reasonable, non-discriminatory terms and */
/* conditions, as appropriate. */
/* */
/* Notice: */
/* This document has been prepared by Motorola Inc. to assist the */
/* 3GPP2 standards committee. This document is offered to the */
/* committee as a basis for discussion and should not be considered */
/* as a binding proposal on Motorola Inc. or any other company. */
/* Specifically, Motorola Inc. reserves the right to modify, amend, */
/* or withdraw the statement contained herein. Permission is granted */
/* to 3GPP2 and its organizational partners to copy any portion of */
/* this document for the legitimate purposes of the 3GPP2. Copying */
/* this document for monetary gain or other non-3GPP2 purpose is */
/* prohibited. Motorola Inc. may hold one or more patents of */
/* copyrights that cover information contained in this contribution, */
/* and agrees that a license under those rights will be made */
/* available on reasonable and non-discriminatory terms and */
/* conditions, subject to receiving a reciprocal license in return. */
/* Nothing contained herein shall be construed as conferring by */
/* implication, estoppel, or otherwise any license or right under */
/* any patent, whether or not the use of information herein */
/* necessarily employs an invention of any existing or later issued */
/* patent, or copyright. */
/* */
/* Notice */
/* Permission is granted to 3GPP2 participants to copy any portion of*/
/* this contribution for the legitimate purpose of the 3GPP2. */
/* Copying this contribution for monetary gain or other non-3GPP2 */
/* purpose is prohibited. */
/* */
/*-------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
//#include <math.h>
#include "typedef.h"
#include "tty.h"
#include "basic_op.h"
#ifndef PI
#define PI 3.14159265358979323846
#endif
#ifndef MIN
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
#endif
Word16 prev_freq = MARK_FREQ;
/*------------------------------------------------------------------
* cos(wk) = 2*cos(w)*cos(w(k-1)) - cos(w(k-2))
*
* Which is of the form:
*
* y[k] = x*y[k-1] - y[k-2]
*
* Initial conditions:
* param[0] = 32767*cos(2*PI*freq/8000) (2*cos(w) in Q14)
* param[1] = y[-2] = 16384; (cos(0) in Q14)
* param[2] = y[-1] = 16384*cos(2*PI*freq/8000) (cos(w) in Q14)
*------------------------------------------------------------------*/
void tone_gen(
Word16 outbuf[], /* (o) output in Q14 */
Word16 freq, /* (i) desired freq in Hz */
Word16 volume, /* (i) gain in Q14 */
Word16 len, /* (i) # of samples to generate */
Word16 param[]) /* (i) param[0] = 2*cos(2*PI*freq/8000) */
/* (i) param[1] = y[k-2] */
/* (i) param[2] = y[k-1] */
{
Word16 i;
Word32 accA;
/*** Generate first 2 samples using previous history ***/
for( i=0 ; i < MIN(len,2) ; i++ )
{
/* outbuf[i] = (((param[0]*param[2])>>14) - param[1]);*/
accA = L_mult(param[0],param[2]);
accA = L_shr(accA,15); /* shr 15 because L_mult adds shl */
outbuf[i] = extract_l(accA);
outbuf[i] = sub(outbuf[i],param[1]);
param[1] = param[2];
param[2] = outbuf[i];
}
/*** Generate the rest of the samples ***/
for( i=2 ; i < len ; i++ )
{
/* outbuf[i] = (((param[0]*outbuf[i-1])>>14) - outbuf[i-2]);*/
accA = L_mult(param[0],outbuf[i-1]);
accA = L_shr(accA,15); /* shr 15 because L_mult adds shl */
outbuf[i] = extract_l(accA);
outbuf[i] = sub(outbuf[i],outbuf[i-2]);
}
/*** Update parameters ***/
if( sub(len,2) >= 0 )
{
param[1] = outbuf[len-2];
param[2] = outbuf[len-1];
}
/*** Volume Control ***/
for( i=0 ; i < len ; i++ )
{
accA = L_mult(outbuf[i],volume); /* Q14*Q14 */
accA = L_shl(accA,1);
outbuf[i] = extract_h(accA);
}
}
/*-------------------------------------------------------------------
* init_tone_gen()
*--------------------------------------------------------------------*/
void init_tone_gen(
Word16 param[], /* (o) tone generation parameters */
Word16 freq ) /* (i) Desired freq as 2*cos(w) */
{
param[0] = freq;
param[1] = 16384;
param[2] = shr(param[0],1);
} /* end init_tone_gen() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -