mci_device.h

Go to the documentation of this file.
00001 #ifndef MCI_DEVICE_H
00002 #define MCI_DEVICE_H
00003 
00004 
00005 #include <windows.h>
00006 #include <mmsystem.h>
00007 #include "debug.h"
00008 
00009 
00010 namespace audiere {
00011 
00016   class MCIDevice {
00017   public:
00018     MCIDevice(const std::string& device) {
00019       m_device = device;
00020 
00021       const char* windowClassName = "AudiereMCINotification";
00022 
00023       // Register window class for MCI notifications.  Don't worry about
00024       // failure.  If this fails, the window creation will fail.
00025       WNDCLASS wc;
00026       wc.style          = 0;
00027       wc.lpfnWndProc    = notifyWindowProc;
00028       wc.cbClsExtra     = 0;
00029       wc.cbWndExtra     = 0;
00030       wc.hInstance      = GetModuleHandle(NULL);
00031       wc.hIcon          = NULL;
00032       wc.hCursor        = NULL;
00033       wc.hbrBackground  = NULL;
00034       wc.lpszMenuName   = NULL;
00035       wc.lpszClassName  = windowClassName;
00036       RegisterClass(&wc);
00037 
00038       m_window = CreateWindow(
00039         windowClassName, "",
00040         WS_POPUP,
00041         0, 0, 0, 0,
00042         NULL, NULL, GetModuleHandle(NULL), NULL);
00043       if (m_window) {
00044         SetWindowLong(m_window, GWL_USERDATA, reinterpret_cast<LONG>(this));
00045       } else {
00046         ADR_LOG("MCI notification window creation failed");
00047       }
00048     }
00049 
00050     ~MCIDevice() {
00051       sendCommand("close");
00052 
00053       if (m_window) {
00054         DestroyWindow(m_window);
00055       }
00056     }
00057 
00058   protected:
00059     std::string sendCommand(const std::string& request, const std::string& parameters = "", int flags = 0) {
00060       bool notify = (flags & MCI_NOTIFY) != 0;
00061       bool wait   = (flags & MCI_WAIT)   != 0;
00062       return sendString(
00063         request + " " + m_device + " " + parameters
00064                 + (wait   ? " wait" : "")
00065                 + (notify ? " notify" : ""),
00066         0,
00067         (notify ? m_window : NULL));
00068     }
00069 
00070     static std::string sendString(const std::string& string, bool* error = 0, HWND window = NULL) {
00071       ADR_LOG("Sending MCI Command: " + string);
00072 
00073       const int bufferLength = 1000;
00074       char buffer[bufferLength + 1] = {0};
00075       MCIERROR e = mciSendString(string.c_str(), buffer, 1000, window);
00076       if (error) {
00077         *error = (e != 0);
00078       }
00079 
00080       char errorString[bufferLength + 1] = {0};
00081       mciGetErrorString(e, errorString, bufferLength);
00082 
00083       if (e) {
00084         ADR_LOG("Error: " + std::string(errorString));
00085       }
00086       if (buffer[0]) {
00087         ADR_LOG("Result: " + std::string(buffer));
00088       }
00089       return buffer;
00090     }
00091 
00093     virtual void notify(WPARAM flags) { }
00094 
00095   private:
00096     static LRESULT CALLBACK notifyWindowProc(HWND window, UINT msg, WPARAM wparam, LPARAM lparam) {
00097       switch (msg) {
00098         case MM_MCINOTIFY: {
00099           MCIDevice* This = reinterpret_cast<MCIDevice*>(GetWindowLong(window, GWL_USERDATA));
00100           if (This) {
00101             This->notify(wparam);
00102           }
00103           return 0;
00104         }
00105 
00106         default:
00107           return DefWindowProc(window, msg, wparam, lparam);
00108       }
00109     }
00110 
00111     std::string m_device;
00112     
00113     HWND m_window;  
00114   };
00115 
00116 }
00117 
00118 
00119 #endif

Generated on Mon Feb 13 23:07:47 2006 for audiere by  doxygen 1.4.6