|
2 | 2 |
|
3 | 3 | namespace luastg { |
4 | 4 | void ResourceSoundEffectImpl::FlushCommand() { |
5 | | - // 根据最后的命令对音效进行操作 |
| 5 | + // apply |
6 | 6 | switch (m_last_command.type) { |
7 | | - case CommandType::None: |
| 7 | + case CommandType::none: |
8 | 8 | break; |
9 | | - case CommandType::Play: |
10 | | - m_player->resume(); |
11 | | - break; |
12 | | - case CommandType::Stop: |
13 | | - m_player->pause(); |
14 | | - break; |
15 | | - case CommandType::Reset: |
| 9 | + case CommandType::play: |
16 | 10 | m_player->setVolume(m_last_command.vol); |
17 | 11 | m_player->setBalance(m_last_command.pan); |
18 | 12 | m_player->play(0.0); |
19 | 13 | break; |
20 | | - case CommandType::ResetAndStop: |
| 14 | + case CommandType::pause: |
| 15 | + m_player->pause(); |
| 16 | + break; |
| 17 | + case CommandType::resume: |
| 18 | + m_player->resume(); |
| 19 | + break; |
| 20 | + case CommandType::stop: |
21 | 21 | m_player->stop(); |
22 | 22 | break; |
23 | 23 | } |
24 | | - // 重置为无命令 |
25 | | - m_last_command.type = CommandType::None; |
26 | | - m_last_command.vol = 0.0f; |
| 24 | + // clear |
| 25 | + m_last_command.type = CommandType::none; |
| 26 | + m_last_command.vol = 1.0f; |
27 | 27 | m_last_command.pan = 0.0f; |
28 | | - // 刷新状态 |
29 | | - if (m_status == 2) { |
30 | | - if (m_player->getState() != core::AudioPlayerState::playing) { |
31 | | - m_status = 0; // 已结束播放 |
| 28 | + // read state |
| 29 | + if (m_state != core::AudioPlayerState::stopped) { |
| 30 | + if (m_player->getState() == core::AudioPlayerState::stopped) { |
| 31 | + m_state = core::AudioPlayerState::stopped; |
32 | 32 | } |
33 | 33 | } |
34 | 34 | } |
35 | 35 | void ResourceSoundEffectImpl::Play(float const vol, float const pan) { |
36 | | - // 优先级最高的命令,覆盖其他一切命令 |
37 | | - m_last_command.type = CommandType::Reset; |
38 | | - m_last_command.vol = std::max(m_last_command.vol, vol); // 取音量最高 |
| 36 | + m_last_command.type = CommandType::play; |
| 37 | + m_last_command.vol = vol; |
39 | 38 | m_last_command.pan = pan; |
40 | | - m_status = 2; // playing |
| 39 | + m_state = core::AudioPlayerState::playing; |
41 | 40 | } |
42 | 41 | void ResourceSoundEffectImpl::Resume() { |
43 | | - switch (m_last_command.type) { |
44 | | - case CommandType::None: // 初始化命令 |
45 | | - case CommandType::Play: // 就是这个命令 |
46 | | - case CommandType::Stop: // 覆盖暂停/停止命令 |
47 | | - m_last_command.type = CommandType::Play; |
48 | | - break; |
49 | | - case CommandType::Reset: |
50 | | - // 保持当前命令 |
51 | | - break; |
52 | | - case CommandType::ResetAndStop: |
53 | | - // 修正为重新播放命令 |
54 | | - m_last_command.type = CommandType::Reset; |
55 | | - break; |
| 42 | + if (m_state == core::AudioPlayerState::paused) { |
| 43 | + m_last_command.type = CommandType::resume; |
| 44 | + m_state = core::AudioPlayerState::playing; |
56 | 45 | } |
57 | | - m_status = 2; // playing |
58 | 46 | } |
59 | 47 | void ResourceSoundEffectImpl::Pause() { |
60 | | - switch (m_last_command.type) { |
61 | | - case CommandType::None: // 初始化命令 |
62 | | - case CommandType::Play: // 覆盖播放/恢复命令 |
63 | | - case CommandType::Stop: // 就是这个命令 |
64 | | - m_last_command.type = CommandType::Stop; |
65 | | - break; |
66 | | - case CommandType::Reset: |
67 | | - // 修正为回到起始命令 |
68 | | - m_last_command.type = CommandType::ResetAndStop; |
69 | | - break; |
70 | | - case CommandType::ResetAndStop: |
71 | | - // 保持当前命令 |
72 | | - break; |
| 48 | + if (m_state == core::AudioPlayerState::playing) { |
| 49 | + m_last_command.type = CommandType::pause; |
| 50 | + m_state = core::AudioPlayerState::paused; |
73 | 51 | } |
74 | | - m_status = 1; // pause |
75 | 52 | } |
76 | 53 | void ResourceSoundEffectImpl::Stop() { |
77 | | - switch (m_last_command.type) { |
78 | | - case CommandType::None: // 初始化命令 |
79 | | - case CommandType::Play: // 覆盖播放/恢复命令 |
80 | | - case CommandType::Stop: // 就是这个命令 |
81 | | - m_last_command.type = CommandType::Stop; |
82 | | - break; |
83 | | - case CommandType::Reset: |
84 | | - // 修正为回到起始命令 |
85 | | - m_last_command.type = CommandType::ResetAndStop; |
86 | | - break; |
87 | | - case CommandType::ResetAndStop: |
88 | | - // 保持当前命令 |
89 | | - break; |
90 | | - } |
91 | | - m_status = 0; // stop |
| 54 | + m_last_command.type = CommandType::stop; |
| 55 | + m_state = core::AudioPlayerState::stopped; |
92 | 56 | } |
93 | | - bool ResourceSoundEffectImpl::IsPlaying() { return m_player->getState() == core::AudioPlayerState::playing || m_status == 2; } |
94 | | - bool ResourceSoundEffectImpl::IsStopped() { return !IsPlaying() && m_status != 1; } |
| 57 | + bool ResourceSoundEffectImpl::IsPlaying() { return m_state == core::AudioPlayerState::playing; } |
| 58 | + bool ResourceSoundEffectImpl::IsStopped() { return m_state == core::AudioPlayerState::stopped; } |
95 | 59 | bool ResourceSoundEffectImpl::SetSpeed(float const speed) { return m_player->setSpeed(speed); } |
96 | 60 | float ResourceSoundEffectImpl::GetSpeed() { return m_player->getSpeed(); } |
97 | 61 |
|
|
0 commit comments