OSDN Git Service

1bedc9474d80ac97cff3bb4156df3751c05e62c7
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / sound / DirectSoundDevice.cpp
1 #pragma once
2 #include <mof/sound/DirectSoundDevice.hpp>
3 #include <dsound.h>
4 #include <mof/ConsoleIO.hpp>
5
6
7 namespace mof
8 {
9 namespace sound
10 {
11 //{{{ constructor
12                 DirectSoundDevice::DirectSoundDevice(HWND hWnd)
13                         : pSound_(NULL), primary_buffer_(NULL)
14                 {
15                         HRESULT hr;
16
17                         hr = DirectSoundCreate8( NULL, &pSound_, NULL );
18                         if( FAILED( hr ) ){
19                                 *ConsoleOut::getInstance() << _T("Failed --- CreateDirectSound"); 
20                                 return; 
21                         }
22                 
23                         if( FAILED(hr = pSound_->SetCooperativeLevel( hWnd, DSSCL_PRIORITY ))) {
24                                 throw std::runtime_error("failed to set cooperative level");
25                         }
26
27                         DSBUFFERDESC desc;
28                         ZeroMemory(&desc, sizeof(desc));
29                         desc.dwSize = sizeof(desc);
30                         desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
31                         desc.dwBufferBytes = 0;// バッファサイズは自動設定される
32                         desc.lpwfxFormat = NULL;
33                         if (FAILED(hr = pSound_->CreateSoundBuffer(&desc, &primary_buffer_, NULL))) {
34                                 throw std::runtime_error("failed to create a primary buffer");
35                         }
36                         
37                         // バッファサイズを得る
38                         DSBCAPS caps;
39                         caps.dwSize = sizeof(DSBCAPS);
40                         primary_buffer_->GetCaps(&caps);
41                         DEBUG_PRINT("primary size" << caps.dwBufferBytes);
42
43                         primary_buffer_->Play(0, 0xffffffff, DSBPLAY_LOOPING);
44                 }
45 //}}}
46 //{{{ destructor
47                 DirectSoundDevice::~DirectSoundDevice()
48                 {
49                         primary_buffer_->Release();
50                         pSound_->Release();
51                 }
52 //}}}   
53 //{{{ raw
54                 LPDIRECTSOUND8 DirectSoundDevice::raw() const {return pSound_;}
55 //}}}
56 //{{{ primary_buffer_
57                 LPDIRECTSOUNDBUFFER DirectSoundDevice::primary_buffer() const {return primary_buffer_;}
58 //}}}
59 }// sound
60 }// mof