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