v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
audio.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/asset.h>
32 #include <sifteo/abi.h>
33 
34 namespace Sifteo {
35 
50 struct AudioChannel {
51  _SYSAudioChannelID sys;
52  typedef _SYSAudioChannelID AudioChannelID;
53 
55  static const AudioChannelID UNDEFINED = _SYS_AUDIO_INVALID_CHANNEL_ID;
56 
58  static const AudioChannelID NUM_CHANNELS = _SYS_AUDIO_MAX_CHANNELS;
59 
61  static const int32_t MAX_VOLUME = _SYS_AUDIO_MAX_VOLUME;
62 
64  enum LoopMode {
65  UNDEF_LOOP = _SYS_LOOP_UNDEF,
66  ONCE = _SYS_LOOP_ONCE,
67  REPEAT = _SYS_LOOP_REPEAT,
68  };
69 
76  AudioChannel() : sys(UNDEFINED)
77  {}
78 
85  AudioChannel(AudioChannelID id) : sys(id)
86  {}
87 
92  void init(AudioChannelID id)
93  {
94  ASSERT(id < NUM_CHANNELS && "AudioChannel ID is invalid");
95  sys = id;
96  }
97 
103  bool play(const AssetAudio &asset, LoopMode loopMode = UNDEF_LOOP) const
104  {
105  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
106  return _SYS_audio_play(&asset.sys, sys, (_SYSAudioLoopType) loopMode);
107  }
108 
112  bool isPlaying() const
113  {
114  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
115  return _SYS_audio_isPlaying(sys);
116  }
117 
123  void stop() const
124  {
125  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
126  _SYS_audio_stop(sys);
127  }
128 
134  void pause() const
135  {
136  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
137  _SYS_audio_pause(sys);
138  }
139 
145  void resume() const
146  {
147  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
148  _SYS_audio_resume(sys);
149  }
150 
156  void setVolume(int volume) const
157  {
158  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
159  _SYS_audio_setVolume(sys, volume);
160  }
161 
168  void setSpeed(unsigned hz) const
169  {
170  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
171  _SYS_audio_setSpeed(sys, hz);
172  }
173 
177  int volume() const
178  {
179  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
180  return _SYS_audio_volume(sys);
181  }
182 
183  uint32_t pos() const
184  {
185  ASSERT(sys < NUM_CHANNELS && "AudioChannel has invalid ID");
186  return _SYS_audio_pos(sys);
187  }
188 };
189 
197 struct AudioTracker {
202  static bool play(const AssetTracker &asset) {
203  return _SYS_tracker_play(&asset.song);
204  }
205 
209  static bool resume() {
210  return _SYS_tracker_play(0);
211  }
212 
216  static void pause() {
217  _SYS_tracker_pause();
218  }
219 
225  static void stop() {
226  _SYS_tracker_stop();
227  }
228 
232  static bool isStopped() {
233  return _SYS_tracker_isStopped();
234  }
235 
239  static bool isPaused() {
240  return _SYS_tracker_isPaused();
241  }
242 
250  static void setVolume(int volume, _SYSAudioChannelID ch = -1)
251  {
252  ASSERT(volume >= 0 && volume <= _SYS_AUDIO_MAX_VOLUME);
253  _SYS_tracker_setVolume(volume, ch);
254  }
255 
262  static void setTempoModifier(int modifier)
263  {
264  ASSERT(modifier > -100);
265  _SYS_tracker_setTempoModifier(modifier);
266  }
267 
275  static void setPosition(uint16_t phrase, uint16_t row)
276  {
277  _SYS_tracker_setPosition(phrase, row);
278  }
279 };
280 
285 } // namespace Sifteo
Definition: audio.h:67
LoopMode
Loop modes available for use in play()
Definition: audio.h:64
AudioChannel(AudioChannelID id)
Initialize an AudioChannel with a concrete value.
Definition: audio.h:85
static bool play(const AssetTracker &asset)
Begin playback of a module.
Definition: audio.h:202
static void pause()
Pause playback of a module.
Definition: audio.h:216
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
AudioChannel()
Default constructor.
Definition: audio.h:76
static const AudioChannelID NUM_CHANNELS
The maximum number of supported AudioChannels in the system.
Definition: audio.h:58
static void stop()
Stop playback of the current module.
Definition: audio.h:225
void pause() const
Pause the currently playing sample in this channel.
Definition: audio.h:134
Playback interface for Tracker modules.
Definition: audio.h:197
bool isPlaying() const
Is this channel currently playing a sample?
Definition: audio.h:112
void setSpeed(unsigned hz) const
Sets the speed of this channel, in samples per second.
Definition: audio.h:168
void setVolume(int volume) const
Definition: audio.h:156
static void setPosition(uint16_t phrase, uint16_t row)
Set play position for current song.
Definition: audio.h:275
static bool isStopped()
Is the tracker currently playing a module?
Definition: audio.h:232
static bool resume()
Resume playback of a paused module.
Definition: audio.h:209
void resume() const
Resume playback on this channel.
Definition: audio.h:145
static const AudioChannelID UNDEFINED
A reserved ID, used to mark undefined AudioChannels.
Definition: audio.h:55
static void setVolume(int volume, _SYSAudioChannelID ch=-1)
Scale volume for current channel.
Definition: audio.h:250
Definition: audio.h:65
A Tracker module, converted from XM format by stir
Definition: asset/audio.h:112
An audio asset, using any supported compression codec.
Definition: asset/audio.h:50
Definition: audio.h:66
static bool isPaused()
Is the tracker currently playing a module?
Definition: audio.h:239
void stop() const
Stop playback of the current sample.
Definition: audio.h:123
bool play(const AssetAudio &asset, LoopMode loopMode=UNDEF_LOOP) const
Begin playback of a sample.
Definition: audio.h:103
Definition: array.h:34
static const int32_t MAX_VOLUME
The maximum volume for an AudioChannel.
Definition: audio.h:61
static void setTempoModifier(int modifier)
Scale tempo for current song.
Definition: audio.h:262
void init(AudioChannelID id)
Initialize a channel by assignging it an ID.
Definition: audio.h:92
One mixer channel, capable of playing AudioAssets.
Definition: audio.h:50
int volume() const
Get the current volume for this channel.
Definition: audio.h:177