Nsound  0.9.4
Public Member Functions | Private Attributes | List of all members
Nsound::Mixer Class Reference

This class enables easy scheduling and mixing of multiple AudioStreams. More...

#include <Nsound/Mixer.h>

Public Member Functions

 Mixer ()
 Constructor. More...
 
 ~Mixer ()
 Destuctor. More...
 
void add (float64 first_beat_time, float64 beats_per_minute, const AudioStream &audio_stream)
 This method inserts the AudioStream to the Mixer's LinkList. More...
 
AudioStream getStream (float64 end_time)
 This method returns one AudioStream composed of all AudioStreams stored in the Mixer's LinkList. More...
 
AudioStream getStream (float64 start_time, float64 end_time)
 
void clear ()
 This method removes all streams from the mixer. More...
 
uint32 size () const
 This method returns the number of stream stored in it. More...
 

Private Attributes

Nsound::uint32 max_channels_
 Stores the maximum number of channels. More...
 
Nsound::MixerSet mixer_set_
 This stores all the MixerNodes. More...
 

Detailed Description

This class enables easy scheduling and mixing of multiple AudioStreams.

This class can que up AudioStreams using beats per minute and first beat time.

Definition at line 63 of file Mixer.h.

Constructor & Destructor Documentation

Mixer::Mixer ( )

Constructor.

Constructor

Definition at line 47 of file Mixer.cc.

48  : max_channels_(0),
49  mixer_set_()
50 {}
Nsound::uint32 max_channels_
Stores the maximum number of channels.
Definition: Mixer.h:124
Nsound::MixerSet mixer_set_
This stores all the MixerNodes.
Definition: Mixer.h:131
Mixer::~Mixer ( )

Destuctor.

Destuctor

Definition at line 54 of file Mixer.cc.

55 {}

Member Function Documentation

void Mixer::add ( float64  first_beat_time,
float64  beats_per_minute,
const AudioStream audio_stream 
)

This method inserts the AudioStream to the Mixer's LinkList.

This method inserts the AudioStream into the Mixer's LinkList.

Parameters
first_beat_time- The time this AudioStream starts playing in the Mixer, must be >= zero.
beats_per_minute- The number of times this AudioStream will play per minute. If this is <= zero, the AudioStream will play only once.
audio_stream- The AudioStream object to be stored.

Definition at line 59 of file Mixer.cc.

References Nsound::AudioStream::getNChannels(), M_ASSERT_VALUE, max_channels_, and mixer_set_.

Referenced by main().

63 {
64  M_ASSERT_VALUE(first_beat_time, >=, 0.0);
65 
66  if(beats_per_minute < 0.0)
67  {
68  beats_per_minute = 0.0;
69  }
70 
71  MixerNode new_node = MixerNode(
72  first_beat_time, beats_per_minute, audio_stream);
73 
74  mixer_set_.insert(new_node);
75 
76  if(audio_stream.getNChannels() > max_channels_)
77  {
78  max_channels_ = audio_stream.getNChannels();
79  }
80 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
Nsound::uint32 max_channels_
Stores the maximum number of channels.
Definition: Mixer.h:124
Nsound::MixerSet mixer_set_
This stores all the MixerNodes.
Definition: Mixer.h:131
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
This class holds the nodes for use with the Mixer class.
Definition: MixerNode.h:55
AudioStream Mixer::getStream ( float64  end_time)

This method returns one AudioStream composed of all AudioStreams stored in the Mixer's LinkList.

This method returns one AudioStream composed of all AudioStreams stored in the Mixer's LinkList.

Definition at line 91 of file Mixer.cc.

Referenced by main().

92 {
93  return getStream(0,end_time);
94 }
AudioStream getStream(float64 end_time)
This method returns one AudioStream composed of all AudioStreams stored in the Mixer's LinkList...
Definition: Mixer.cc:91
AudioStream Mixer::getStream ( float64  start_time,
float64  end_time 
)

Definition at line 123 of file Mixer.cc.

References Nsound::AudioStream::add(), Nsound::AudioStream::getLength(), M_ASSERT_VALUE, max_channels_, mixer_set_, Nsound::start_time, and Nsound::AudioStream::substream().

124 {
125  M_ASSERT_VALUE(mixer_set_.size(), >, 0);
126  M_ASSERT_VALUE(start_time, <, end_time);
127 
128  if(mixer_set_.size() == 0) return AudioStream(1,1);
129  if(start_time >= end_time) return AudioStream(1,1);
130 
131  MixerSet::const_iterator node = mixer_set_.begin();
132 
133  // Grab the sample rate
134  float64 sample_rate = node->audio_stream_->getSampleRate();
135 
136  // Create the new stream to return. By default it is 2 channels and
137  // has the same sample_rate as the first AudioStream.
138 
139  AudioStream new_stream(sample_rate, max_channels_);
140 
141  // Fill the stream with zeros.
142  uint32 n_samples = static_cast<uint32>(
143  sample_rate * (end_time - start_time));
144 
145  for(uint32 i = 0; i < n_samples; ++i)
146  {
147  new_stream << 0.0;
148  }
149 
150  // Loop through the list of MixerNodes. On each node, loop from start
151  // time to end time and add the node's AudioStream data to new_stream.
152 
153  while(node != mixer_set_.end())
154  {
155  // Beats per minute is a frequency, but we want to calculate the
156  // the time between beats. Remember that f = 1/t, so to get time
157  // we invert the equation, t = 1/f. Here, we also need to multiply
158  // by 60 to get beats per second.
159  //
160  // Therefore, t = 60/f
161 
162  float64 beat_time = 60.0 / node->bpm_;
163 
164  float64 beat_length =
165  static_cast<float64>(node->audio_stream_->getLength())
166  / static_cast<float64>(sample_rate);
167 
168  // We also need to keep track of the next_beat_time and end_time.
169  float64 next_beat_time = node->first_beat_time_;
170  float64 next_end_time = next_beat_time + beat_length;
171 
172  // initialize some indexes
173  uint32 new_stream_offset_index = 0;
174  uint32 node_max_index = node->audio_stream_->getLength();
175 
176  float64 mixer_time = start_time;
177 
178  // Go to the first beat that ends after current time.
179  while(next_end_time < mixer_time)
180  {
181  next_beat_time += beat_time;
182  next_end_time = next_beat_time + beat_length;
183  }
184 
185  float64 new_stream_time = next_beat_time - start_time;
186  float64 new_stream_end_time = end_time - start_time;
187 
188  // Case 1: Start time is before next beat.
189  // ....[________].....[________].....[________]..
190  // ^
191  // start
192 
193  if(mixer_time < next_beat_time)
194  {
195  mixer_time = next_beat_time;
196  }
197 
198  // Case 2: Start time is inside next beat.
199  // ....[________].....[________].....[________]..
200  // ^
201  // start
202 
203  else if(next_beat_time < mixer_time)
204  {
205  // Add the remaing AudioStream to the output.
206 
207  uint32 node_index = static_cast<uint32>((next_end_time - mixer_time) * sample_rate);
208 
209  new_stream_offset_index = static_cast<uint32>(
210  (mixer_time - start_time) * sample_rate);
211 
212  AudioStream temp = node->audio_stream_->substream(node_index);
213 
214  new_stream.add(temp, new_stream_offset_index);
215 
216  next_beat_time += beat_time;
217  next_end_time = next_beat_time + beat_length;
218  mixer_time = next_beat_time;
219  new_stream_time = next_beat_time - start_time;
220  }
221 
222  while(new_stream_time < new_stream_end_time)
223  {
224  new_stream_offset_index = static_cast<uint32>
225  (new_stream_time * sample_rate);
226 
227  // add into new_stream
228  uint32 add_n_samples = node_max_index;
229 
230  if(next_end_time > end_time)
231  {
232  add_n_samples -= static_cast<uint32>((next_end_time - end_time)
233  * static_cast<float64>(sample_rate));
234  }
235 
236  new_stream.add(
237  *node->audio_stream_,
238  new_stream_offset_index,
239  add_n_samples);
240 
241  next_beat_time += beat_time;
242  next_end_time = next_beat_time + beat_length;
243  mixer_time = next_beat_time;
244  new_stream_time += beat_time;
245  }
246 
247  // Move to the next MixerNode in the list.
248  ++node;
249 
250  } // while(node != NULL)
251 
252  return new_stream;
253 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
Nsound::uint32 max_channels_
Stores the maximum number of channels.
Definition: Mixer.h:124
AudioStream substream(uint32 start_index, uint32 n_samples=0) const
Definition: AudioStream.cc:825
double float64
Definition: Nsound.h:146
static double start_time
Definition: TicToc.cc:35
Nsound::MixerSet mixer_set_
This stores all the MixerNodes.
Definition: Mixer.h:131
void add(const AudioStream &as, uint32 offset, uint32 n_samples=0)
This method adds the passed AudioStream to this AudioStream.
Definition: AudioStream.cc:121
void Mixer::clear ( )

This method removes all streams from the mixer.

Definition at line 84 of file Mixer.cc.

References mixer_set_.

Referenced by main().

85 {
86  mixer_set_.erase(mixer_set_.begin(), mixer_set_.end());
87 }
Nsound::MixerSet mixer_set_
This stores all the MixerNodes.
Definition: Mixer.h:131
uint32 Nsound::Mixer::size ( ) const
inline

This method returns the number of stream stored in it.

Definition at line 124 of file Mixer.h.

References mixer_set_.

Referenced by main().

124 { return static_cast<uint32>(mixer_set_.size()); };
unsigned int uint32
Definition: Nsound.h:153
Nsound::MixerSet mixer_set_
This stores all the MixerNodes.
Definition: Mixer.h:131

Member Data Documentation

Nsound::uint32 Nsound::Mixer::max_channels_
private

Stores the maximum number of channels.

Definition at line 124 of file Mixer.h.

Referenced by add(), and getStream().

Nsound::MixerSet Nsound::Mixer::mixer_set_
private

This stores all the MixerNodes.

Definition at line 131 of file Mixer.h.

Referenced by add(), clear(), getStream(), and size().


The documentation for this class was generated from the following files: