aboutsummaryrefslogtreecommitdiff
path: root/src/aoblipplayer.cpp
blob: 3a13d787925693d8ef8dcd1a3ec6625e70b78f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "aoblipplayer.h"

AOBlipPlayer::AOBlipPlayer(AOApplication *ao_app)
    : ao_app(ao_app)
{}

void AOBlipPlayer::setVolume(int value)
{
  m_volume = value;
  updateInternalVolume();
}

void AOBlipPlayer::setMuted(bool enabled)
{
  m_muted = enabled;
  updateInternalVolume();
}

void AOBlipPlayer::setBlip(QString blip)
{
  QString path = ao_app->get_sfx_suffix(ao_app->get_sounds_path(blip));
  for (int i = 0; i < STREAM_COUNT; ++i)
  {
    BASS_StreamFree(m_stream[i]);

    if (path.endsWith(".opus"))
    {
      m_stream[i] = BASS_OPUS_StreamCreateFile(FALSE, path.utf16(), 0, 0, BASS_UNICODE | BASS_ASYNCFILE);
    }
    else
    {
      m_stream[i] = BASS_StreamCreateFile(FALSE, path.utf16(), 0, 0, BASS_UNICODE | BASS_ASYNCFILE);
    }
  }

  updateInternalVolume();
}

void AOBlipPlayer::playBlip()
{
  HSTREAM stream = m_stream[m_cycle];
  BASS_ChannelSetDevice(stream, BASS_GetDevice());
  BASS_ChannelPlay(stream, false);
  m_cycle = ++m_cycle % STREAM_COUNT;
}

void AOBlipPlayer::updateInternalVolume()
{
  float volume = m_muted ? 0.0f : (m_volume * 0.01);
  for (int i = 0; i < STREAM_COUNT; ++i)
  {
    BASS_ChannelSetAttribute(m_stream[i], BASS_ATTRIB_VOL, volume);
  }
}