📄 beep.c
字号:
/*
** Kenobi2 version 1.3 (Sing unit)
**
** ** This code has been made to check/learn the 1161 functionalities **
** ** Release 25-Feb-2002 **
**
** OKANO, Akifumi
**
** Computing Segment, Semisonductors Div, Philips Japan Ltd.
** akifumi.okano@philips.com
** +81-3-3740-4668
*/
#ifndef NoBEEP
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include "dev_ep.h"
#include "ui.h"
#include "sing.h"
#include "beep.h"
#define PI 3.1415926535
unsigned long g_beep_sample_count;
unsigned long g_duration;
unsigned char g_decay_sw;
double g_freq_factor_L;
double g_freq_factor_R;
unsigned int buffer_fill_beep_sound( unsigned char *buffer_ptr );
void beep( double frequency, double duration, unsigned char decay, unsigned char poly )
{
device_instance *dvi_ptr;
if ( is_audio_active() )
return;
/* */
/* Finding USB audio device */
/* */
if ( NULL == (dvi_ptr = find_class_interface( AUDIO_CLASS_INTERFACE )) )
return ;
g_beep_sample_count = 0;
g_duration = (unsigned long)(duration * 44100);
g_decay_sw = decay;
g_freq_factor_L = frequency * ((2.00 * PI) / 44100.00);
if ( poly == MONO )
g_freq_factor_R = g_freq_factor_L * 1.00;
else
g_freq_factor_R = g_freq_factor_L * 1.3333333333;
/* */
/* Initialize Audio functions */
/* */
if ( 0 != audio_initialize( dvi_ptr, NULL, buffer_fill_beep_sound ) )
return;
/* */
/* Start to play Audio */
/* */
audio_start();
}
unsigned int buffer_fill_beep_sound( unsigned char *buffer_ptr )
{
short *bf;
short sample_L;
short sample_R;
double amp;
unsigned int i = 0;
bf = (short *)buffer_ptr;
if ( g_decay_sw )
{
// amp = 0.10 * (1.00 - log10( 1.00 + g_beep_sample_count / (g_duration * 0.10) )); // log decay
amp = 0.70 * ((double)(g_duration - g_beep_sample_count) / (double)g_duration); // linear decay
}
else
{
amp = 0.70;
}
amp *= (double)SHRT_MAX;
while ( ((g_beep_sample_count + 1) < g_duration) && (i < (SINGLE_BUFFER_SIZE__SHORT >> 1)) )
{
sample_L = (short)( sin( (double)g_beep_sample_count * g_freq_factor_L ) * amp);
sample_R = (short)( sin( (double)g_beep_sample_count * g_freq_factor_R ) * amp);
*bf++ = sample_L;
*bf++ = sample_R;
g_beep_sample_count++;
i++;
#if 0
*bf++ = sample_L;
*bf++ = sample_R;
g_beep_sample_count++;
i++;
#endif
}
return ( i << 2 );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -