Logo Search packages:      
Sourcecode: scummvm version File versions  Download package

int Kyra::SoundDigital::playSound ( const char *  filename,
uint8  priority,
Audio::Mixer::SoundType  type,
int  volume = 255,
bool  loop = false,
int  channel = -1 
)

Plays a sound.

Parameters:
filename file to be played
priority priority of the sound
type type
volume channel volume
loop true if the sound should loop (endlessly)
channel tell the sound player to use a specific channel for playback
Returns:
channel playing the sound

Definition at line 409 of file sound_digital.cpp.

References Common::String::c_str(), isPlaying(), Kyra::kDebugLevelSound, Audio::Mixer::playStream(), and stopSound().

                                                                                                                              {
      Sound *use = 0;
      if (channel != -1 && channel < ARRAYSIZE(_sounds)) {
            stopSound(channel);
            use = &_sounds[channel];
      } else {
            for (channel = 0; !use && channel < ARRAYSIZE(_sounds); ++channel) {
                  if (!isPlaying(channel)) {
                        stopSound(channel);
                        use = &_sounds[channel];
                        break;
                  }
            }

            for (channel = 0; !use && channel < ARRAYSIZE(_sounds); ++channel) {
                  if (strcmp(_sounds[channel].filename, filename) == 0) {
                        stopSound(channel);
                        use = &_sounds[channel];
                        break;
                  }
            }

            for (channel = 0; !use && channel < ARRAYSIZE(_sounds); ++channel) {
                  if (_sounds[channel].priority <= priority) {
                        stopSound(channel);
                        use = &_sounds[channel];
                        break;
                  }
            }

            if (!use) {
                  warning("no free sound channel");
                  return -1;
            }
      }

      Common::SeekableReadStream *stream = 0;
      int usedCodec = -1;
      for (int i = 0; _supportedCodecs[i].fileext; ++i) {
            Common::String file = filename;
            file += _supportedCodecs[i].fileext;

            if (!_vm->resource()->exists(file.c_str()))
                  continue;

            stream = _vm->resource()->createReadStream(file);
            usedCodec = i;
      }

      if (!stream) {
            warning("Couldn't find soundfile '%s'", filename);
            return -1;
      }

      Common::strlcpy(use->filename, filename, sizeof(use->filename));
      use->priority = priority;
      debugC(5, kDebugLevelSound, "playSound: \"%s\"", use->filename);
      Audio::SeekableAudioStream *audioStream = _supportedCodecs[usedCodec].streamFunc(stream, DisposeAfterUse::YES);
      if (!audioStream) {
            warning("Couldn't create audio stream for file '%s'", filename);
            return -1;
      }
      use->stream = new KyraAudioStream(audioStream);
      assert(use->stream);
      if (use->stream->endOfData()) {
            delete use->stream;
            use->stream = 0;

            return -1;
      }

      if (volume > 255)
            volume = 255;
      volume = (volume * Audio::Mixer::kMaxChannelVolume) / 255;

      if (type == Audio::Mixer::kSpeechSoundType && _vm->heliumMode())
            use->stream->setRate(32765);

      _mixer->playStream(type, &use->handle, makeLoopingAudioStream(use->stream, loop ? 0 : 1), -1, volume);
      return use - _sounds;
}


Generated by  Doxygen 1.6.0   Back to index