OSDN Git Service

fix CMakeList.txt for windows
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / sound / StreamSoundBuffer.cpp
1 #include "mof/sound/StreamSoundBuffer.hpp"
2 #include "mof/ConsoleIO.hpp"
3 #include "mof/sound/StreamingSoundLoader.hpp"
4 #include "mof/sound/DirectSoundDevice.hpp"
5
6
7 mof::StreamSoundBuffer::StreamSoundBuffer
8 (
9         std::shared_ptr<mof::sound::DirectSoundDevice> pDevice,
10         LPDIRECTSOUNDBUFFER8 pBuffer,
11         LPDIRECTSOUNDNOTIFY8 pSoundNotify,
12         SoundFile* pResource
13 )
14 : mof::SoundBuffer(pDevice, pBuffer , pResource)
15 {
16         m_pSoundNotify = pSoundNotify;
17         m_pSoundNotify->AddRef();
18         m_pLoadingThread = NULL;
19         m_pLoadingFunction = NULL;
20
21         
22 }
23
24
25
26 mof::StreamSoundBuffer::~StreamSoundBuffer(void)
27 {
28         //boost::mutex::scoped_lock lock(m_pLoadingFunction->getMutex()); TODO これ付けてるとエラーになる
29         getSoundBuffer()->Stop();
30         m_pLoadingThread->join();
31         delete m_pLoadingThread;
32         delete m_pLoadingFunction;
33         m_pSoundNotify->Release();
34 }
35
36
37 bool mof::StreamSoundBuffer::initialize(){
38         
39         m_pLoadingFunction = new StreamingSoundLoader( getSoundBuffer() , m_pSoundNotify , getResource() );
40         m_pLoadingFunction->initialize();
41         //m_pLoadingFunction->CopyBlock(0);
42         m_pLoadingThread = new boost::thread( boost::ref<StreamingSoundLoader>(*m_pLoadingFunction) );
43         return true;
44 }
45
46
47 bool mof::StreamSoundBuffer::play(bool looping){
48         boost::mutex::scoped_lock lock(m_pLoadingFunction->getMutex());
49         
50         HRESULT hr = getSoundBuffer()->Play( 0 , 0 , (looping)?DSBPLAY_LOOPING : 0);
51         if(hr == DSERR_BUFFERLOST){
52                 *ConsoleOut::getInstance() << _T("Failed --- PlaySound") << std::endl;
53                 return false;
54         }
55         return true;
56 }
57
58 void mof::StreamSoundBuffer::stop(){
59         //boost::mutex::scoped_lock lock(m_pLoadingFunction->GetMutex());
60         getSoundBuffer()->Stop();
61         getSoundBuffer()->SetCurrentPosition(0);
62         getResource()->resetFile();
63         //try{
64         m_pLoadingThread->join();
65         //}catch(exception e){DEBUG_PRINT(_T("dd"));}
66         delete m_pLoadingThread;
67         delete m_pLoadingFunction;
68         m_pLoadingFunction = new StreamingSoundLoader( getSoundBuffer() , m_pSoundNotify , getResource() );
69         m_pLoadingFunction->initialize();
70         //m_pLoadingFunction->CopyBlock(0);
71         m_pLoadingThread = new boost::thread( boost::ref<StreamingSoundLoader>(*m_pLoadingFunction) );
72         
73         
74 }