ScummVM API documentation
pcspk.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_SOFTSYNTH_PCSPK_H
23 #define AUDIO_SOFTSYNTH_PCSPK_H
24 
25 #include "audio/audiostream.h"
26 #include "common/mutex.h"
27 #include "common/queue.h"
28 
29 namespace Audio {
30 
31 class PCSpeaker : public AudioStream {
32 public:
33  enum WaveForm {
34  kWaveFormSquare = 0,
35  kWaveFormSine,
36  kWaveFormSaw,
37  kWaveFormTriangle,
38  kWaveFormSilence
39  };
40 
41 protected:
42  // PC speaker instruction: play this waveform at frequency x for y microseconds.
43  struct Command {
44  WaveForm waveForm;
45  float frequency;
46  uint32 length;
47 
48  Command(WaveForm waveForm, float frequency, uint32 length);
49  };
50 
51 public:
52  PCSpeaker(int rate = 44100);
53  ~PCSpeaker();
54 
59  void play(WaveForm wave, int freq, int32 length);
80  void playQueue(WaveForm wave, float freq, uint32 lengthus);
82  void stop(int32 delay = 0);
84  void setVolume(byte volume);
85 
86  bool isPlaying() const;
87 
88  int readBuffer(int16 *buffer, const int numSamples);
89 
90  bool isStereo() const { return false; }
91  bool endOfData() const { return false; }
92  bool endOfStream() const { return false; }
93  int getRate() const { return _rate; }
94 
95 protected:
96  Common::Mutex _mutex;
97 
98  int _rate;
99  WaveForm _wave;
100  bool _playForever;
101  uint32 _oscLength;
102  uint32 _oscSamples;
103  uint32 _remainingSamples;
104  uint32 _mixedSamples;
105  byte _volume;
106 
107  // The queue of playback instructions.
108  Common::Queue<Command> *_commandQueue;
109  // True if a playback instruction is currently being executed. False if
110  // current playback was started by the play method (or if there is no
111  // playback at all).
112  bool _commandActive;
113 
114  typedef int8 (*generatorFunc)(uint32, uint32);
115  static const generatorFunc generateWave[];
116 
117  static int8 generateSquare(uint32 x, uint32 oscLength);
118  static int8 generateSine(uint32 x, uint32 oscLength);
119  static int8 generateSaw(uint32 x, uint32 oscLength);
120  static int8 generateTriangle(uint32 x, uint32 oscLength);
121  static int8 generateSilence(uint32 x, uint32 oscLength);
122 };
123 
124 } // End of namespace Audio
125 
126 #endif // AUDIO_SOFTSYNTH_PCSPEAKER_H
void setVolume(byte volume)
void playQueue(WaveForm wave, float freq, uint32 lengthus)
void stop(int32 delay=0)
bool endOfData() const
Definition: pcspk.h:91
Definition: queue.h:42
Definition: pcspk.h:43
void play(WaveForm wave, int freq, int32 length)
bool endOfStream() const
Definition: pcspk.h:92
Definition: mutex.h:67
Definition: audiostream.h:50
bool isStereo() const
Definition: pcspk.h:90
int getRate() const
Definition: pcspk.h:93
Definition: pcspk.h:31
Definition: system.h:37
int readBuffer(int16 *buffer, const int numSamples)