~ubuntu-branches/ubuntu/natty/plee-the-bear/natty

« back to all changes in this revision

Viewing changes to bear-engine/core/src/audio/code/sdl_sample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
  Bear Engine
3
3
 
4
 
  Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
5
5
 
6
6
  This program is free software; you can redistribute it and/or modify it
7
7
  under the terms of the GNU General Public License as published by the
27
27
 * \author Julien Jorge
28
28
 */
29
29
#include "audio/sdl_sample.hpp"
 
30
 
 
31
#include "audio/sdl_sound.hpp"
30
32
#include "audio/sound_manager.hpp"
31
33
 
32
34
#include <SDL/SDL.h>
33
35
#include <claw/assert.hpp>
34
36
#include <claw/exception.hpp>
35
37
#include <claw/logger.hpp>
 
38
#include <claw/types.hpp>
36
39
#include <cmath>
37
40
#include <limits>
38
41
 
89
92
void bear::audio::sdl_sample::channel_attribute::set_effect
90
93
( const sound_effect& effect )
91
94
{
 
95
 
92
96
  m_effect = effect;
93
97
} // sdl_sample::channel_attribute::set_effect()
94
98
 
132
136
 
133
137
/*----------------------------------------------------------------------------*/
134
138
/**
135
 
 * \brief Default constructor.
136
 
 */
137
 
bear::audio::sdl_sample::sdl_sample()
138
 
  : m_channel(-1), m_sound(NULL)
139
 
{
140
 
 
141
 
} // sdl_sample::sdl_sample()
142
 
 
143
 
/*----------------------------------------------------------------------------*/
144
 
/**
145
139
 * \brief Constructor.
146
140
 * \param s The sound of which we are a sample.
147
141
 * \param owner The instance of sound_manager who manage the sound.
148
142
 */
149
143
bear::audio::sdl_sample::sdl_sample( const sdl_sound& s, sound_manager& owner )
150
 
  : sample(owner), m_channel(-1), m_sound(&s)
 
144
  : sample(s.get_sound_name(), owner), m_channel(-1), m_sound(&s)
151
145
{
152
146
 
153
147
} // sdl_sample::sdl_sample()
167
161
 */
168
162
void bear::audio::sdl_sample::play()
169
163
{
170
 
  inside_play(1);
 
164
  inside_play();
171
165
} // sdl_sample::play()
172
166
 
173
167
/*----------------------------------------------------------------------------*/
177
171
 */
178
172
void bear::audio::sdl_sample::play( const sound_effect& effect )
179
173
{
180
 
  inside_play( effect.get_loops() );
 
174
  m_effect = effect;
181
175
 
182
 
  if ( m_channel != -1)
183
 
    inside_set_effect(effect);
 
176
  inside_play();
184
177
} // sdl_sample::play() [effect]
185
178
 
186
179
/*----------------------------------------------------------------------------*/
244
237
  if ( m_channel != -1 )
245
238
    return s_playing_channels[m_channel]->get_effect();
246
239
  else
247
 
    return sound_effect();
 
240
    return m_effect;
248
241
} // sdl_sample::get_effect()
249
242
 
250
243
/*----------------------------------------------------------------------------*/
254
247
 */
255
248
void bear::audio::sdl_sample::set_effect( const sound_effect& effect )
256
249
{
 
250
  m_effect = effect;
 
251
 
257
252
  if ( m_channel != -1 )
258
253
    {
259
254
      if ( !Mix_UnregisterAllEffects(m_channel) )
260
255
        claw::logger << claw::log_warning << "sdl_sample::set_effect(): "
261
256
                     << Mix_GetError() << std::endl;
262
257
 
263
 
      inside_set_effect( effect );
 
258
      inside_set_effect();
264
259
    }
265
260
} // sdl_sample::change_effect()
266
261
 
305
300
(int channel, void* stream, int length, void* attr)
306
301
{
307
302
  CLAW_PRECOND( attr != NULL );
 
303
  CLAW_PRECOND( length % 2 == 0 );
 
304
  CLAW_PRECOND( sdl_sound::get_audio_format() == AUDIO_S16 );
308
305
 
309
306
  double tone_down = 1.0;
310
 
  char* buffer = static_cast<char*>(stream);
 
307
  claw::int_16* buffer = static_cast<claw::int_16*>(stream);
 
308
  length /= 2;
311
309
  const channel_attribute* attribute = static_cast<channel_attribute*>(attr);
312
310
 
313
311
  const claw::math::coordinate_2d<double> ears =
328
326
    std::fill( buffer, buffer + (unsigned int)length, 0 );
329
327
  else if ( tone_down < 1.0 )
330
328
    for (unsigned int i=0; i!=(unsigned)length; ++i)
331
 
      buffer[i] = (char)((double)buffer[i] * tone_down);
 
329
      buffer[i] = (claw::int_16)((double)buffer[i] * tone_down);
332
330
 
333
331
} // sdl_sample::distance_tone_down()
334
332
 
347
345
(int channel, void* stream, int length, void* attr)
348
346
{
349
347
  CLAW_PRECOND( attr != NULL );
 
348
  CLAW_PRECOND( length % 2 == 0 );
 
349
  CLAW_PRECOND( sdl_sound::get_audio_format() == AUDIO_S16 );
350
350
 
351
 
  char* buffer = static_cast<char*>(stream);
 
351
  claw::int_16* buffer = static_cast<claw::int_16*>(stream);
 
352
  length /= 2;
352
353
  const channel_attribute* attribute = static_cast<channel_attribute*>(attr);
353
354
 
354
355
  const double v = attribute->get_effect().get_volume();
357
358
    std::fill( buffer, buffer + (unsigned int)length, 0 );
358
359
  else
359
360
    for (unsigned int i=0; i!=(unsigned)length; ++i)
360
 
      buffer[i] = (char)((double)buffer[i] * v);
 
361
      buffer[i] = (claw::int_16)((double)buffer[i] * v);
361
362
} // sdl_sample::volume()
362
363
 
363
364
/*----------------------------------------------------------------------------*/
364
365
/**
365
366
 * \brief Start to play the sample.
366
 
 * \param loops Number of loops, 0 for infinite.
367
367
 */
368
 
void bear::audio::sdl_sample::inside_play( unsigned int loops )
 
368
void bear::audio::sdl_sample::inside_play()
369
369
{
370
370
  if ( m_channel != -1 )
371
371
    stop();
372
372
 
373
373
  if ( m_sound != NULL )
374
 
    m_channel = m_sound->play(loops);
 
374
    m_channel = m_sound->play(m_effect.get_loops());
375
375
 
376
376
  if ( m_channel != -1 )
377
377
    {
379
379
      Mix_Volume
380
380
        ( m_channel,
381
381
          (int)(m_sound->get_manager().get_volume(this) * MIX_MAX_VOLUME) );
 
382
 
 
383
      inside_set_effect();
382
384
    }
383
385
} // sdl_sample::inside_play()
384
386
 
385
387
/*----------------------------------------------------------------------------*/
386
388
/**
387
389
 * \brief Set the effect associated to a channel.
388
 
 * \param position The position of this sound.
389
390
 * \pre m_channel >= 0
390
391
 * \pre s_playing_channels[channel]->is_empty() == false.
391
392
 * \post s_playing_channels[channel]->get_effect().get_position() == position.
392
393
 */
393
 
void bear::audio::sdl_sample::inside_set_effect( const sound_effect& effect )
 
394
void bear::audio::sdl_sample::inside_set_effect()
394
395
{
395
396
  CLAW_PRECOND( m_channel >= 0 );
396
397
  CLAW_PRECOND( s_playing_channels[m_channel]->is_empty() == false );
397
398
 
398
 
  s_playing_channels[m_channel]->set_effect( effect );
 
399
  s_playing_channels[m_channel]->set_effect( m_effect );
399
400
 
400
 
  if ( effect.has_a_position() )
 
401
  if ( m_effect.has_a_position() )
401
402
    {
402
403
      int ok;
403
404
      ok = Mix_RegisterEffect
408
409
                     << Mix_GetError() << std::endl;
409
410
    }
410
411
 
411
 
  if ( effect.get_volume() != 1 )
 
412
  if ( m_effect.get_volume() != 1 )
412
413
    {
413
414
      int ok;
414
415
      ok = Mix_RegisterEffect