From eb24bdac93a1871785ca4db191f5817267f0cc46 Mon Sep 17 00:00:00 2001 From: Stephane Letz Date: Thu, 31 Mar 2011 22:03:34 +0200 Subject: [PATCH] New JackWinMMEPort class to factorize some code. --- windows/winmme/JackWinMMEInputPort.cpp | 59 +++++++++------------ windows/winmme/JackWinMMEInputPort.h | 25 ++++----- windows/winmme/JackWinMMEOutputPort.cpp | 67 +++++++----------------- windows/winmme/JackWinMMEOutputPort.h | 31 +++-------- windows/winmme/JackWinMMEPort.cpp | 69 +++++++++++++++++++++++++ windows/winmme/JackWinMMEPort.h | 58 +++++++++++++++++++++ 6 files changed, 185 insertions(+), 124 deletions(-) create mode 100644 windows/winmme/JackWinMMEPort.cpp create mode 100644 windows/winmme/JackWinMMEPort.h diff --git a/windows/winmme/JackWinMMEInputPort.cpp b/windows/winmme/JackWinMMEInputPort.cpp index ae526cbc..9d72cc42 100644 --- a/windows/winmme/JackWinMMEInputPort.cpp +++ b/windows/winmme/JackWinMMEInputPort.cpp @@ -59,7 +59,7 @@ JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name, (DWORD)this, CALLBACK_FUNCTION | MIDI_IO_STATUS); if (result != MMSYSERR_NOERROR) { - GetErrorString(result, error_message); + GetInErrorString(result, error_message); goto delete_sysex_buffer; } sysex_header.dwBufferLength = max_bytes; @@ -70,19 +70,19 @@ JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name, sysex_header.lpNext = 0; result = midiInPrepareHeader(handle, &sysex_header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - GetErrorString(result, error_message); + GetInErrorString(result, error_message); goto close_handle; } result = midiInAddBuffer(handle, &sysex_header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - GetErrorString(result, error_message); + GetInErrorString(result, error_message); goto unprepare_header; } MIDIINCAPS capabilities; char *name_tmp; result = midiInGetDevCaps(index, &capabilities, sizeof(capabilities)); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [constructor]", "midiInGetDevCaps", + WriteInError("JackWinMMEInputPort [constructor]", "midiInGetDevCaps", result); name_tmp = driver_name; } else { @@ -100,13 +100,13 @@ JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name, unprepare_header: result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [constructor]", + WriteInError("JackWinMMEInputPort [constructor]", "midiInUnprepareHeader", result); } close_handle: result = midiInClose(handle); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [constructor]", "midiInClose", result); + WriteInError("JackWinMMEInputPort [constructor]", "midiInClose", result); } delete_sysex_buffer: delete[] sysex_buffer; @@ -118,16 +118,16 @@ JackWinMMEInputPort::~JackWinMMEInputPort() Stop(); MMRESULT result = midiInReset(handle); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [destructor]", "midiInReset", result); + WriteInError("JackWinMMEInputPort [destructor]", "midiInReset", result); } result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [destructor]", "midiInUnprepareHeader", + WriteInError("JackWinMMEInputPort [destructor]", "midiInUnprepareHeader", result); } result = midiInClose(handle); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort [destructor]", "midiInClose", result); + WriteInError("JackWinMMEInputPort [destructor]", "midiInClose", result); } delete[] sysex_buffer; delete thread_queue; @@ -154,27 +154,6 @@ JackWinMMEInputPort::EnqueueMessage(jack_nframes_t time, size_t length, } } -const char * -JackWinMMEInputPort::GetAlias() -{ - return alias; -} - -const char * -JackWinMMEInputPort::GetName() -{ - return name; -} - -void -JackWinMMEInputPort::GetErrorString(MMRESULT error, LPTSTR text) -{ - MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH); - if (result != MMSYSERR_NOERROR) { - snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error); - } -} - void JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer, jack_nframes_t frames) @@ -254,7 +233,7 @@ JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2) MMRESULT result = midiInAddBuffer(handle, &sysex_header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteError("JackWinMMEInputPort::ProcessWinMME", "midiInAddBuffer", + WriteInError("JackWinMMEInputPort::ProcessWinMME", "midiInAddBuffer", result); } break; @@ -274,7 +253,7 @@ JackWinMMEInputPort::Start() MMRESULT result = midiInStart(handle); started = result == MMSYSERR_NOERROR; if (! started) { - WriteError("JackWinMMEInputPort::Start", "midiInStart", result); + WriteInError("JackWinMMEInputPort::Start", "midiInStart", result); } } return started; @@ -287,17 +266,27 @@ JackWinMMEInputPort::Stop() MMRESULT result = midiInStop(handle); started = result != MMSYSERR_NOERROR; if (started) { - WriteError("JackWinMMEInputPort::Stop", "midiInStop", result); + WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result); } } return ! started; } void -JackWinMMEInputPort::WriteError(const char *jack_func, const char *mm_func, +JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text) +{ + MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH); + if (result != MMSYSERR_NOERROR) { + snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error); + } +} + +void +JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func, MMRESULT result) { char error_message[MAXERRORLENGTH]; - GetErrorString(result, error_message); + GetInErrorString(result, error_message); jack_error("%s - %s: %s", jack_func, mm_func, error_message); } + diff --git a/windows/winmme/JackWinMMEInputPort.h b/windows/winmme/JackWinMMEInputPort.h index 568bd9f5..a7305340 100644 --- a/windows/winmme/JackWinMMEInputPort.h +++ b/windows/winmme/JackWinMMEInputPort.h @@ -24,10 +24,11 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #include "JackMidiAsyncQueue.h" #include "JackMidiBufferWriteQueue.h" +#include "JackWinMMEPort.h" namespace Jack { - class JackWinMMEInputPort { + class JackWinMMEInputPort : public JackRunnableInterface { private: @@ -39,19 +40,10 @@ namespace Jack { EnqueueMessage(jack_nframes_t time, size_t length, jack_midi_data_t *data); - void - GetErrorString(MMRESULT error, LPTSTR text); void ProcessWinMME(UINT message, DWORD param1, DWORD param2); - void - WriteError(const char *jack_func, const char *mm_func, - MMRESULT result); - - char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; - char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; - HMIDIIN handle; jack_midi_event_t *jack_event; jack_midi_data_t *sysex_buffer; @@ -61,6 +53,13 @@ namespace Jack { bool started; + void + WriteOSError(const char *jack_func, const char *os_func); + + void + WriteInError(const char *jack_func, const char *mm_func, + MMRESULT result); + public: JackWinMMEInputPort(const char *alias_name, const char *client_name, @@ -69,12 +68,6 @@ namespace Jack { ~JackWinMMEInputPort(); - const char * - GetAlias(); - - const char * - GetName(); - void ProcessJack(JackMidiBuffer *port_buffer, jack_nframes_t frames); diff --git a/windows/winmme/JackWinMMEOutputPort.cpp b/windows/winmme/JackWinMMEOutputPort.cpp index 7af41ca7..dcc1dd79 100644 --- a/windows/winmme/JackWinMMEOutputPort.cpp +++ b/windows/winmme/JackWinMMEOutputPort.cpp @@ -58,7 +58,7 @@ JackWinMMEOutputPort::JackWinMMEOutputPort(const char *alias_name, MMRESULT result = midiOutOpen(&handle, index, (DWORD)HandleMessageEvent, (DWORD)this, CALLBACK_FUNCTION); if (result != MMSYSERR_NOERROR) { - GetMMErrorString(result, error_message); + GetOutErrorString(result, error_message); goto raise_exception; } thread_queue_semaphore = CreateSemaphore(NULL, 0, max_messages, NULL); @@ -75,7 +75,7 @@ JackWinMMEOutputPort::JackWinMMEOutputPort(const char *alias_name, char *name_tmp; result = midiOutGetDevCaps(index, &capabilities, sizeof(capabilities)); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort [constructor]", "midiOutGetDevCaps", + WriteOutError("JackWinMMEOutputPort [constructor]", "midiOutGetDevCaps", result); name_tmp = driver_name; } else { @@ -95,7 +95,7 @@ JackWinMMEOutputPort::JackWinMMEOutputPort(const char *alias_name, close_handle: result = midiOutClose(handle); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort [constructor]", "midiOutClose", + WriteOutError("JackWinMMEOutputPort [constructor]", "midiOutClose", result); } raise_exception: @@ -107,12 +107,12 @@ JackWinMMEOutputPort::~JackWinMMEOutputPort() Stop(); MMRESULT result = midiOutReset(handle); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort [destructor]", "midiOutReset", + WriteOutError("JackWinMMEOutputPort [destructor]", "midiOutReset", result); } result = midiOutClose(handle); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort [destructor]", "midiOutClose", + WriteOutError("JackWinMMEOutputPort [destructor]", "midiOutClose", result); } if (! CloseHandle(sysex_semaphore)) { @@ -179,7 +179,7 @@ JackWinMMEOutputPort::Execute() message |= (DWORD) data[0]; result = midiOutShortMsg(handle, message); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort::Execute", + WriteOutError("JackWinMMEOutputPort::Execute", "midiOutShortMsg", result); } continue; @@ -193,13 +193,13 @@ JackWinMMEOutputPort::Execute() header.lpData = (LPSTR)data; result = midiOutPrepareHeader(handle, &header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort::Execute", + WriteOutError("JackWinMMEOutputPort::Execute", "midiOutPrepareHeader", result); continue; } result = midiOutLongMsg(handle, &header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort::Execute", "midiOutLongMsg", + WriteOutError("JackWinMMEOutputPort::Execute", "midiOutLongMsg", result); continue; } @@ -213,7 +213,7 @@ JackWinMMEOutputPort::Execute() result = midiOutUnprepareHeader(handle, &header, sizeof(MIDIHDR)); if (result != MMSYSERR_NOERROR) { - WriteMMError("JackWinMMEOutputPort::Execute", + WriteOutError("JackWinMMEOutputPort::Execute", "midiOutUnprepareHeader", result); break; } @@ -223,38 +223,6 @@ JackWinMMEOutputPort::Execute() return false; } -const char * -JackWinMMEOutputPort::GetAlias() -{ - return alias; -} - -void -JackWinMMEOutputPort::GetMMErrorString(MMRESULT error, LPTSTR text) -{ - MMRESULT result = midiOutGetErrorText(error, text, MAXERRORLENGTH); - if (result != MMSYSERR_NOERROR) { - snprintf(text, MAXERRORLENGTH, "Unknown MM error code '%d'", error); - } -} - -const char * -JackWinMMEOutputPort::GetName() -{ - return name; -} - -void -JackWinMMEOutputPort::GetOSErrorString(LPTSTR text) -{ - DWORD error = GetLastError(); - if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), text, - MAXERRORLENGTH, NULL)) { - snprintf(text, MAXERRORLENGTH, "Unknown OS error code '%d'", error); - } -} - void JackWinMMEOutputPort::HandleMessage(UINT message, DWORD_PTR param1, DWORD_PTR param2) @@ -388,18 +356,19 @@ JackWinMMEOutputPort::Wait(HANDLE semaphore) } void -JackWinMMEOutputPort::WriteMMError(const char *jack_func, const char *mm_func, - MMRESULT result) +JackWinMMEOutputPort::GetOutErrorString(MMRESULT error, LPTSTR text) { - char error_message[MAXERRORLENGTH]; - GetMMErrorString(result, error_message); - jack_error("%s - %s: %s", jack_func, mm_func, error_message); + MMRESULT result = midiOutGetErrorText(error, text, MAXERRORLENGTH); + if (result != MMSYSERR_NOERROR) { + snprintf(text, MAXERRORLENGTH, "Unknown MM error code '%d'", error); + } } void -JackWinMMEOutputPort::WriteOSError(const char *jack_func, const char *os_func) +JackWinMMEOutputPort::WriteOutError(const char *jack_func, const char *mm_func, + MMRESULT result) { char error_message[MAXERRORLENGTH]; - GetOSErrorString(error_message); - jack_error("%s - %s: %s", jack_func, os_func, error_message); + GetMMErrorString(result, error_message); + jack_error("%s - %s: %s", jack_func, mm_func, error_message); } diff --git a/windows/winmme/JackWinMMEOutputPort.h b/windows/winmme/JackWinMMEOutputPort.h index c343ff55..36fa28b1 100644 --- a/windows/winmme/JackWinMMEOutputPort.h +++ b/windows/winmme/JackWinMMEOutputPort.h @@ -29,7 +29,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. namespace Jack { - class JackWinMMEOutputPort: public JackRunnableInterface { + class JackWinMMEOutputPort : + public JackWinMMEPort, public JackRunnableInterface { private: @@ -37,12 +38,6 @@ namespace Jack { HandleMessageEvent(HMIDIOUT handle, UINT message, DWORD_PTR port, DWORD_PTR param1, DWORD_PTR param2); - void - GetMMErrorString(MMRESULT error, LPTSTR text); - - void - GetOSErrorString(LPTSTR text); - void HandleMessage(UINT message, DWORD_PTR param1, DWORD_PTR param2); @@ -52,16 +47,6 @@ namespace Jack { bool Wait(HANDLE semaphore); - void - WriteMMError(const char *jack_func, const char *mm_func, - MMRESULT result); - - void - WriteOSError(const char *jack_func, const char *os_func); - - char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; - char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; - HMIDIOUT handle; JackMidiBufferReadQueue *read_queue; HANDLE sysex_semaphore; @@ -70,7 +55,11 @@ namespace Jack { HANDLE thread_queue_semaphore; void - GetErrorString(MMRESULT error, LPTSTR text); + GetOutErrorString(MMRESULT error, LPTSTR text); + + void + WriteOutError(const char *jack_func, const char *mm_func, + MMRESULT result); public: @@ -95,12 +84,6 @@ namespace Jack { bool Stop(); - const char * - GetAlias(); - - const char * - GetName(); - }; } diff --git a/windows/winmme/JackWinMMEPort.cpp b/windows/winmme/JackWinMMEPort.cpp new file mode 100644 index 00000000..577cfbeb --- /dev/null +++ b/windows/winmme/JackWinMMEPort.cpp @@ -0,0 +1,69 @@ +/* +Copyright (C) 2011 Devin Anderson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include +#include + +#include "JackWinMMEPort.h" + +using Jack::JackWinMMEPort; + +/////////////////////////////////////////////////////////////////////////////// +// Class +/////////////////////////////////////////////////////////////////////////////// + +JackWinMMEPort::JackWinMMEPort(const char *alias_name, + const char *client_name, + const char *driver_name) +{} + +JackWinMMEPort::~JackWinMMEPort() +{} + +const JackWinMMEPort * +JackWinMMEOutputPort::GetAlias() +{ + return alias; +} + +const char * +JackWinMMEPort::GetName() +{ + return name; +} + +void +JackWinMMEPort::GetOSErrorString(LPTSTR text) +{ + DWORD error = GetLastError(); + if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), text, + MAXERRORLENGTH, NULL)) { + snprintf(text, MAXERRORLENGTH, "Unknown OS error code '%d'", error); + } +} + +void +JackWinMMEPort::WriteOSError(const char *jack_func, const char *os_func) +{ + char error_message[MAXERRORLENGTH]; + GetOSErrorString(error_message); + jack_error("%s - %s: %s", jack_func, os_func, error_message); +} + diff --git a/windows/winmme/JackWinMMEPort.h b/windows/winmme/JackWinMMEPort.h new file mode 100644 index 00000000..3b53fd5f --- /dev/null +++ b/windows/winmme/JackWinMMEPort.h @@ -0,0 +1,58 @@ +/* +Copyright (C) 2011 Devin Anderson + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __JackWinMMEPort__ +#define __JackWinMMEPort__ + +#include +#include + +namespace Jack { + + class JackWinMMEPort { + + protected: + + char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; + char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE]; + + public: + + JackWinMMEPort(const char *alias_name, const char *client_name, + const char *driver_name); + + ~JackWinMMEPort(); + + const char * + GetAlias(); + + const char * + GetName(); + + void + GetOSErrorString(LPTSTR text); + + void + GetInErrorString(MMRESULT error, LPTSTR text); + + }; + +} + +#endif