Skip to content

Commit

Permalink
fixed a bug in the mutex handling in midiIn mex file, recompiled on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoostenveld committed May 5, 2015
1 parent bd6c53d commit 4da735a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Converting real-time EEG into sounds and music

## Platform specific notes for Microsoft Windows
Development is done on a VirtualBox with Windows 7 and MATLAB 2012b.

Precompiled mex file are provided, these are linked to a static version of portmidi. To
use these mex files in MATLAB, you might have to install the Microsoft Visual C++ 2010
Redistributable Package, which is available from [1]. This package contains some
Expand All @@ -17,6 +19,8 @@ Software Development Kit version 7.1 (see [3]).
[3] http://stackoverflow.com/questions/1865069/how-to-compile-a-64-bit-application-using-visual-c-2010-express

## Platform specific notes for Mac OS X
Development is done on a MacBook Pro with OS X 10.9.5 and MATLAB 2012b.

Compilation of the mex files requires that portmidi is present. This can be downloaded
from http://portmedia.sourceforge.net/portmidi. Compilation of portmidi requires cmake.
It is also possible to install a compiled version of portmidi using MacPorts ("sudo port
Expand Down
3 changes: 2 additions & 1 deletion launchcontrolXL.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ function creategui(h)
% all controls have the same callback function
ft_uilayout(h, 'tag', '^.*$', 'callback', @cb_interface);

ft_uilayout(h, 'style', 'popupmenu', 'value', 64); % set the default to '0'
ft_uilayout(h, 'style', 'popupmenu', 'value', 64); % set the default value to the middle
ft_uilayout(h, 'style', 'slider', 'value', 0.5); % set the default value to the middle

ft_uilayout(h, 'tag', 'control$', 'position', [0 0 060 030]);
ft_uilayout(h, 'tag', 'focus$', 'position', [0 0 060 030]);
Expand Down
16 changes: 10 additions & 6 deletions midiIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <platform.h>
#include <compiler.h>


#if defined(PLATFORM_WINDOWS)
/* only needed on Windows */
#define usleep(x) (Sleep((x)/1000))
Expand Down Expand Up @@ -85,7 +84,7 @@ unsigned int numReceived = 0;
int32_t *channel = NULL, *note = NULL, *velocity = NULL, *timestamp = NULL;

char locked = 0;
#define MUTEX_LOCK {while(locked) {usleep(100);} locked=1;}
#define MUTEX_LOCK {while (locked) {usleep(100);} locked=1;}
#define MUTEX_UNLOCK {locked=0;}
#define MUTEX_ISLOCKED (locked)

Expand Down Expand Up @@ -150,13 +149,15 @@ void exitFunction() {
inStream = NULL;
}
Pm_Terminate();
MUTEX_LOCK;
FREE(channel);
FREE(note);
FREE(velocity);
FREE(timestamp);
isInit = 0;
deviceOpen = -1;
numReceived = 0;
MUTEX_UNLOCK;
deviceOpen = -1;
isInit = 0;
}
}

Expand All @@ -168,6 +169,7 @@ void init() {

mexPrintf("Initialising PortMidi\n");

MUTEX_LOCK;
note = malloc(INPUT_BUFFER_SIZE*sizeof(int32_t));
channel = malloc(INPUT_BUFFER_SIZE*sizeof(int32_t));
velocity = malloc(INPUT_BUFFER_SIZE*sizeof(int32_t));
Expand All @@ -177,8 +179,12 @@ void init() {
FREE(note);
FREE(velocity);
FREE(timestamp);
MUTEX_UNLOCK;
mexErrMsgTxt("Could not allocate memory");
}
else {
MUTEX_UNLOCK;
}

err1 = Pm_Initialize();
reportPmError(err1);
Expand Down Expand Up @@ -277,7 +283,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
PmError err1;
PtError err2;

MUTEX_LOCK;
init(); /* this returns immediately if already done */

if (nrhs > 0) {
Expand Down Expand Up @@ -358,5 +363,4 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
default:
mexErrMsgTxt("Bad call\n");
}
MUTEX_UNLOCK;
}
Binary file modified midiIn.mexmaci64
Binary file not shown.

0 comments on commit 4da735a

Please sign in to comment.