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 "audio/mixer.h"
27 #include "common/mutex.h"
28 #include "common/queue.h"
29 
30 namespace Audio {
31 
32 #define PCSPK_DEFAULT_VOLUME 20
33 
34 class PCSpeakerStream;
35 
36 class PCSpeaker {
37 public:
38  enum WaveForm {
39  kWaveFormSquare = 0,
40  kWaveFormSilence
41  };
42 
43 public:
44  PCSpeaker();
45  ~PCSpeaker();
46 
47  bool init();
48  void quit();
49 
54  void play(WaveForm wave, int freq, int32 length, byte volume = PCSPK_DEFAULT_VOLUME);
55 
76  void playQueue(WaveForm wave, float freq, uint32 lengthus, byte volume = PCSPK_DEFAULT_VOLUME);
78  void stop(int32 delay = 0);
79 
80  bool isPlaying() const;
81 
82 private:
83  PCSpeakerStream *_speakerStream;
84  SoundHandle _speakerHandle;
85 };
86 
87 
88 class PCSpeakerStream : public AudioStream {
89 protected:
90  // PC speaker instruction: play this waveform at frequency x for y microseconds.
91  struct Command {
92  PCSpeaker::WaveForm waveForm;
93  float frequency;
94  uint32 length;
95  byte volume;
96 
97  Command(PCSpeaker::WaveForm waveForm, float frequency, uint32 length, byte volume = PCSPK_DEFAULT_VOLUME);
98  };
99 
100 public:
101  PCSpeakerStream(int rate = 44100);
102  ~PCSpeakerStream();
103 
108  void play(PCSpeaker::WaveForm wave, int freq, int32 length, byte volume = PCSPK_DEFAULT_VOLUME);
129  void playQueue(PCSpeaker::WaveForm wave, float freq, uint32 lengthus, byte volume = PCSPK_DEFAULT_VOLUME);
131  void stop(int32 delay = 0);
132 
133  bool isPlaying() const;
134 
135  int readBuffer(int16 *buffer, const int numSamples) override;
136 
137  bool isStereo() const override { return false; }
138  bool endOfData() const override { return false; }
139  bool endOfStream() const override { return false; }
140  int getRate() const override { return _rate; }
141 
142 protected:
143  Common::Mutex _mutex;
144 
145  int _rate;
146  PCSpeaker::WaveForm _wave;
147  bool _playForever;
148  uint32 _oscLength;
149  uint32 _oscSamples;
150  uint32 _remainingSamples;
151  uint32 _mixedSamples;
152  byte _volume;
153 
154  // The queue of playback instructions.
155  Common::Queue<Command> *_commandQueue;
156  // True if a playback instruction is currently being executed. False if
157  // current playback was started by the play method (or if there is no
158  // playback at all).
159  bool _commandActive;
160 
161  typedef int8 (*generatorFunc)(uint32, uint32);
162  static const generatorFunc generateWave[];
163 
164  static int8 generateSquare(uint32 x, uint32 oscLength);
165  static int8 generateSine(uint32 x, uint32 oscLength);
166  static int8 generateSaw(uint32 x, uint32 oscLength);
167  static int8 generateTriangle(uint32 x, uint32 oscLength);
168  static int8 generateSilence(uint32 x, uint32 oscLength);
169 };
170 
171 } // End of namespace Audio
172 
173 #endif // AUDIO_SOFTSYNTH_PCSPEAKER_H
bool endOfData() const override
Definition: pcspk.h:138
void stop(int32 delay=0)
void play(WaveForm wave, int freq, int32 length, byte volume=20)
Definition: pcspk.h:88
Definition: queue.h:42
void playQueue(WaveForm wave, float freq, uint32 lengthus, byte volume=20)
Definition: mixer.h:49
int getRate() const override
Definition: pcspk.h:140
Definition: pcspk.h:91
Definition: mutex.h:67
Definition: audiostream.h:50
bool endOfStream() const override
Definition: pcspk.h:139
bool isStereo() const override
Definition: pcspk.h:137
Definition: pcspk.h:36
Definition: system.h:39