1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
rtsyn_npipe.c
Copyright (c) 2007 Keishi Suenaga <s_keishi@mutt.freemail.ne.jp>
I referenced following sources.
alsaseq_c.c - ALSA sequencer server interface
Copyright (c) 2000 Takashi Iwai <tiwai@suse.de>
readmidi.c
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "interface.h"
#ifdef __POCC__
#include <sys/types.h>
#endif //for off_t
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#endif
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <math.h>
#include <signal.h>
#include "server_defs.h"
#ifdef __W32__
#include <windows.h>
#endif
#include "timidity.h"
#include "common.h"
#include "controls.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "recache.h"
#include "output.h"
#include "aq.h"
#include "timer.h"
#include "rtsyn.h"
#define PIPE_BUFFER_SIZE (8192)
static HANDLE hPipe=NULL;
static char pipe_name[256];
#define EVBUFF_SIZE 512
typedef struct rtsyn_evbuf_t{
UINT wMsg;
UINT port;
DWORD dwParam1;
DWORD dwParam2;
int exlen;
char *exbuffer;
} RtsynEvBuf;
static RtsynEvBuf evbuf[EVBUFF_SIZE];
static UINT np_evbwpoint=0;
static UINT np_evbrpoint=0;
static UINT evbsysexpoint;
static CRITICAL_SECTION mim_np_section;
int first_ev = 1;
static double mim_start_time;
static char npipe_buffer[2*PIPE_BUFFER_SIZE];
static int npipe_len=0;
void rtsyn_np_set_pipe_name(char* name)
{
strncpy(pipe_name, name, 256-1);
pipe_name[256-1]='\0';
}
static int npipe_input_open(const char *pipe_name)
{
static OVERLAPPED overlapped;
static HANDLE hEvent;
char PipeName[256];
DWORD ret;
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
memset( &overlapped, 0, sizeof(OVERLAPPED));
overlapped.hEvent = hEvent;
sprintf(PipeName, "\\\\.\\pipe\\%s", pipe_name);
hPipe = CreateNamedPipe(PipeName, PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
// PIPE_WAIT|
PIPE_READMODE_BYTE |PIPE_TYPE_BYTE, 2,
0, PIPE_BUFFER_SIZE, 0, NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't create Named Pipe %s : %ld",
pipe_name, GetLastError());
return -1;
}
ret = ConnectNamedPipe(hPipe, &overlapped);
if ( (ret == 0) && (ERROR_IO_PENDING!=GetLastError()) ){
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "CnnectNamePipe(%ld) error %s",
GetLastError(), pipe_name);
CloseHandle(hPipe);
hPipe=NULL;
return -1;
}
// WaitForSingleObject(overlapped.hEvent, 1000);
CloseHandle(hEvent);
return 0;
}
int rtsyn_np_synth_start()
{
if( 0 != npipe_input_open(pipe_name)) return 0;
// npipe_input_open(pipe_name);
np_evbwpoint=0;
np_evbrpoint=0;
InitializeCriticalSection(&mim_np_section);
first_ev = 1;
npipe_len=0;
return ~0;
}
void rtsyn_np_synth_stop()
{
rtsyn_stop_playing();
// play_mode->close_output();
DeleteCriticalSection(&mim_np_section);
CloseHandle(hPipe);
hPipe=NULL;
return;
}
int rtsyn_np_buf_check(void)
{
int retval;
EnterCriticalSection(&mim_np_section);
retval = (np_evbrpoint != np_evbwpoint) ? 0 : -1;
LeaveCriticalSection(&mim_np_section);
return retval;
}
static int read_pipe_data(void);
int rtsyn_np_play_some_data(void)
{
UINT wMsg;
DWORD dwParam1;
DWORD dwParam2;
MidiEvent ev;
MidiEvent evm[260];
UINT evbpoint;
MIDIHDR *IIMidiHdr;
int exlen;
char *sysexbuffer;
int ne,i,j,chk,played;
UINT port;
static DWORD pre_time;
static DWORD timeoffset;
double event_time;
// rtsyn_play_one_data (0,0x007f3c90, get_current_calender_time());
if ( 0 != read_pipe_data()) return 0;
played=0;
if( -1 == rtsyn_np_buf_check() ){
played=~0;
return played;
}
do{
EnterCriticalSection(&mim_np_section);
evbpoint=np_evbrpoint;
if (++np_evbrpoint >= EVBUFF_SIZE)
np_evbrpoint -= EVBUFF_SIZE;
wMsg=evbuf[evbpoint].wMsg;
port=evbuf[evbpoint].port;
dwParam1=evbuf[evbpoint].dwParam1;
dwParam2=evbuf[evbpoint].dwParam2;
exlen = evbuf[evbpoint].exlen;
sysexbuffer = evbuf[evbpoint].exbuffer;
LeaveCriticalSection(&mim_np_section);
if(rtsyn_sample_time_mode !=1){
if((first_ev == 1) || ( pre_time > dwParam2)){
pre_time=dwParam2;
timeoffset=dwParam2;
mim_start_time = get_current_calender_time();
first_ev=0;
}
if(dwParam2 !=0){
event_time= mim_start_time+((double)(dwParam2-timeoffset))*(double)1.0/(double)1000.0;
}else{
event_time = get_current_calender_time();
}
}
switch (wMsg) {
case RTSYN_NP_DATA:
if(rtsyn_sample_time_mode !=1){
rtsyn_play_one_data(port, dwParam1, event_time);
}else{
rtsyn_play_one_data(port, dwParam1, dwParam2);
}
break;
case RTSYN_NP_LONGDATA:
if(rtsyn_sample_time_mode !=1){
rtsyn_play_one_sysex(sysexbuffer,exlen, event_time);
}else{
rtsyn_play_one_sysex(sysexbuffer,exlen, dwParam2);
}
free(sysexbuffer);
break;
}
pre_time =dwParam2;
}while( 0==rtsyn_np_buf_check());
return played;
}
static void parse_ev(char* buffer, int *len){
UINT wMsg;
UINT port;
DWORD dwParam1;
DWORD dwParam2;
int exlen;
char *exbuffer;
char *bp, *sp;
UINT evbpoint;
RtsynNpEvBuf *npevbuf;
/*
printf ("buhihi %d \n", *len);
{
int i,j;
for(j=0; j < 4; j++){
for(i=0; i <16;i++){
printf("%2X:", buffer[j*16+i]);
}
printf ("\n");
}
}
*/
bp=buffer;
sp=buffer;
while(1){
npevbuf=(RtsynNpEvBuf*)sp;
if(*len >= sizeof(RtsynNpEvBuf)){
wMsg=npevbuf->wMsg;
}else{
memmove(buffer, sp, *len);
return;
}
if( ( wMsg != RTSYN_NP_LONGDATA ) && ( wMsg != RTSYN_NP_DATA ) ){
*len = 0;
return;
}
if( wMsg == RTSYN_NP_DATA){
port=npevbuf->port;
dwParam1=npevbuf->dwParam1;
dwParam2=npevbuf->dwParam2;
bp = sp+sizeof(RtsynNpEvBuf);
}
if( wMsg == RTSYN_NP_LONGDATA ){
exlen=npevbuf->exlen;
bp = sp+sizeof(RtsynNpEvBuf);
dwParam2=npevbuf->dwParam2;
if (*len >= sizeof(RtsynNpEvBuf)+exlen){
exbuffer= (char *)malloc( sizeof(char) * exlen);
memmove(exbuffer,sp+sizeof(RtsynNpEvBuf), exlen);
bp += exlen;
}else{
memmove(buffer, sp, *len);
return;
}
}
EnterCriticalSection(&mim_np_section);
evbpoint = np_evbwpoint;
if (++np_evbwpoint >= EVBUFF_SIZE)
np_evbwpoint -= EVBUFF_SIZE;
evbuf[evbpoint].wMsg = wMsg;
evbuf[evbpoint].port = port;
evbuf[evbpoint].dwParam1 = dwParam1;
evbuf[evbpoint].dwParam2 = dwParam2;
evbuf[evbpoint].exlen = exlen;
evbuf[evbpoint].exbuffer = exbuffer;
LeaveCriticalSection(&mim_np_section);
*len -= (bp -sp);
sp=bp;
if(*len <= 0){
len = 0;
return;
}
};
}
static int read_pipe_data()
{
DWORD last_error;
DWORD n;
DWORD length;
OVERLAPPED overlapped;
HANDLE hEvent;
if( hPipe == NULL ) return -1;
// if ( ( 0 == PeekNamedPipe(hPipe,NULL,0,NULL,&length,NULL)) &&
// (GetLastError()==ERROR_BAD_PIPE) ) return -1;
if ( 0 == PeekNamedPipe(hPipe,NULL,0,NULL,&length,NULL)) return -1;
if(length == 0) return -1;
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
memset( &overlapped, 0, sizeof(OVERLAPPED));
overlapped.hEvent = hEvent;
npipe_len=0; // not good fix
memset(npipe_buffer+npipe_len, 0, PIPE_BUFFER_SIZE-npipe_len);
if(length <= PIPE_BUFFER_SIZE - npipe_len){
if( length > 0 ){
ReadFile(hPipe, npipe_buffer+npipe_len,length, &n, &overlapped);
last_error = GetLastError();
if(last_error == ERROR_IO_PENDING){
GetOverlappedResult(hPipe, &overlapped,&n,TRUE) ;
last_error = GetLastError();
}
}
}else{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Named Pipe buffer overlow");
return -1;
}
if(last_error == ERROR_SUCCESS){
npipe_len += n;
parse_ev(npipe_buffer,&npipe_len);
}
CloseHandle(hEvent);
if ( (last_error != ERROR_SUCCESS) &&
(last_error != ERROR_NOACCESS) ){
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Named Pipe Error: %ld",last_error);
return -1;
}
return 0;
}
void rtsyn_np_pipe_close()
{
CloseHandle(hPipe);
}
|