ScummVM API documentation
sine.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AUDIO_SINE_H
23 #define AUDIO_SINE_H
24 
25 #include "audio/audiostream.h"
26 #include "common/util.h"
27 
28 namespace Audio {
29 
30 class SineStream : public AudioStream {
31 public:
32  SineStream(int freq, int rate = 44100) {
33  _rate = rate;
34  _oscLength = rate / freq;
35  _oscSamples = 0;
36  _volume = 20; // The maximum volume is 255
37  }
38 
39 
40  int readBuffer(int16 *buffer, const int numSamples) override {
41  for (int i = 0; i < numSamples; i++) {
42  buffer[i] = generateSine(_oscSamples, _oscLength) * _volume;
43  if (_oscSamples++ >= _oscLength)
44  _oscSamples = 0;
45  }
46 
47  return numSamples;
48  }
49 
50  bool isStereo() const override { return false; }
51  bool endOfData() const override { return false; }
52  bool endOfStream() const override { return false; }
53  int getRate() const override { return _rate; }
54 
55 protected:
56  int _rate;
57  uint32 _oscLength;
58  uint32 _oscSamples;
59  byte _volume;
60 
61  int8 generateSine(uint32 x, uint32 oscLength) const {
62  if (oscLength == 0)
63  return 0;
64 
65  // TODO: Maybe using a look-up-table would be better?
66  return CLIP<int16>((int16) (128 * sin(2.0 * M_PI * x / oscLength)), -128, 127);
67  }
68 };
69 
70 }
71 
72 #endif
int getRate() const override
Definition: sine.h:53
int readBuffer(int16 *buffer, const int numSamples) override
Definition: sine.h:40
bool endOfStream() const override
Definition: sine.h:52
Definition: audiostream.h:50
bool isStereo() const override
Definition: sine.h:50
Definition: sine.h:30
bool endOfData() const override
Definition: sine.h:51
Definition: system.h:39