~stomato463/+junk/nvdajp

« back to all changes in this revision

Viewing changes to nvdaHelper/local/beeps.cpp

  • Committer: Masataka Shinke
  • Date: 2011-10-25 12:35:26 UTC
  • mfrom: (4185 jpmain)
  • mto: This revision was merged to the branch mainline in revision 4211.
  • Revision ID: mshinke@users.sourceforge.jp-20111025123526-ze527a2rl3z0g2ky
lp:~nishimotz/nvdajp/main : 4185 をマージ

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This file is a part of the NVDA project.
 
3
URL: http://www.nvda-project.org/
 
4
Copyright 2006-2010 NVDA contributers.
 
5
    This program is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License version 2.0, as published by
 
7
    the Free Software Foundation.
 
8
    This program is distributed in the hope that it will be useful,
 
9
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
11
This license can be found at:
 
12
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
13
*/
 
14
 
1
15
#define _USE_MATH_DEFINES
2
16
#include <cmath>
3
17
#include "beeps.h"
4
18
using namespace std;
5
19
 
6
 
const float PITWO=M_PI*2;
 
20
const double PITWO=M_PI*2;
7
21
const unsigned sampleRate=44100;
8
22
const int amplitude=14000;
9
23
 
10
24
unsigned generateBeep(short* buf, const float hz, const unsigned length, const unsigned char left, const unsigned char right) {
11
 
        const unsigned samplesPerCycle=(sampleRate/hz);
12
 
        unsigned totalSamples=(length/1000.0)/(1.0/sampleRate);
 
25
        const unsigned samplesPerCycle=static_cast<unsigned int>(sampleRate/hz);
 
26
        unsigned totalSamples=static_cast<unsigned int>((length/1000.0)/(1.0/sampleRate));
13
27
        totalSamples+=(samplesPerCycle-(totalSamples%samplesPerCycle));
14
28
        if (!buf) { //just return buffer length
15
29
                return totalSamples*4;
16
30
        }
17
 
        const float sinFreq=PITWO/samplesPerCycle;
 
31
        const double sinFreq=PITWO/samplesPerCycle;
18
32
        for (unsigned sampleNum=0; sampleNum<totalSamples; ++sampleNum) {
19
 
                const short sample=min(max(sin((sampleNum%sampleRate)*sinFreq)*2,-1),1)*amplitude;
20
 
                const short leftSample=sample*(left/100.0);
21
 
                const short rightSample=sample*(right/100.0);
 
33
                const short sample=static_cast<short>(min(max(sin((sampleNum%sampleRate)*sinFreq)*2,-1),1)*amplitude);
 
34
                const short leftSample=static_cast<short>(sample*(left/100.0));
 
35
                const short rightSample=static_cast<short>(sample*(right/100.0));
22
36
                buf[sampleNum*2]=leftSample;
23
37
                buf[sampleNum*2+1]=rightSample;
24
38
        }