OSDN Git Service

f219e19b444f618e5f4c8a42b1d898aef278c9c5
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / sound / SoundBuffer.cpp
1 #include "SoundBuffer.hpp"
2 #include "SoundFile.hpp"
3 #include <mof/ConsoleIO.hpp>
4
5 mof::SoundBuffer::SoundBuffer
6 (
7         std::shared_ptr<mof::sound::DirectSoundDevice> pDevice,
8         LPDIRECTSOUNDBUFFER8 pBuffer,
9         SoundFile* pResouce
10 )
11         : m_pBuffer(pBuffer), m_pResource(pResouce), pDevice_(pDevice)
12 {
13         m_pBuffer->AddRef();
14         pBuffer->GetFrequency(&default_frequency_);
15 }
16
17 mof::SoundBuffer::~SoundBuffer(void){
18                 m_pBuffer->Release();
19                 delete m_pResource;
20 }
21
22 //{{{ update
23 void mof::SoundBuffer::update()
24 {
25         volume_stream_.update();
26         frequency_stream_.update();
27
28         mof::real volume = volume_stream_.value();
29         if (volume > 1) volume = 1;
30         else if (volume < 0) volume = 0;
31         int converted_volume = (1.0f - volume) * DSBVOLUME_MIN;
32         converted_volume = converted_volume < DSBVOLUME_MIN
33                 ? DSBVOLUME_MIN
34                 : converted_volume > DSBVOLUME_MAX
35                         ? DSBVOLUME_MAX
36                         : converted_volume;
37         LONG prev_volume;
38         getSoundBuffer()->GetVolume(&prev_volume);
39         if (converted_volume != prev_volume) {
40                 getSoundBuffer()->SetVolume(converted_volume);// 必要ない場合は変更を行わない
41         }
42         
43         mof::real frequency = frequency_stream_.value();
44         int converted_frequency = default_frequency_ * frequency;
45         converted_frequency = converted_frequency < DSBFREQUENCY_MIN 
46                         ? DSBFREQUENCY_MIN  
47                         : converted_frequency > DSBFREQUENCY_MAX 
48                                 ? DSBFREQUENCY_MAX
49                                 : converted_frequency;
50         DWORD prev_frequency;
51         getSoundBuffer()->GetFrequency(&prev_frequency);
52         if (converted_frequency != prev_frequency) {
53                 getSoundBuffer()->SetFrequency(converted_frequency);// 必要ない場合は変更を行わない
54         }
55         
56 }
57 //}}}