File: rtsyn_portmidi.c

package info (click to toggle)
timidity 2.14.0-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,364 kB
  • sloc: ansic: 168,735; sh: 3,730; makefile: 1,405; tcl: 1,048; perl: 285; ruby: 126
file content (247 lines) | stat: -rw-r--r-- 5,869 bytes parent folder | download | duplicates (4)
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
/*
    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_portmidi.c
        Copyright (c) 2003 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"

#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 <portmidi.h>
#include <porttime.h>

#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"

int rtsyn_portnumber=1;
unsigned int portID[MAX_PORT];
char rtsyn_portlist[32][80];
int rtsyn_nportlist;

#define MAX_EXBUF 20
#define BUFF_SIZE 512

double pm_start_time;
PmError pmerr;
static unsigned int InNum;
struct midistream_t{
	PortMidiStream* stream;
};
static struct midistream_t  midistream[MAX_PORT];
//static PmDeviceID portID[MAX_PORT];
#define PMBUFF_SIZE 8192
#define EXBUFF_SIZE 512
static PmEvent pmbuffer[PMBUFF_SIZE];
static char    sysexbuffer[EXBUFF_SIZE];

void rtsyn_get_port_list(){
	int i,j;
	PmDeviceInfo *deviceinfo;
	
	pmerr=Pm_Initialize();
	if( pmerr != pmNoError ) goto pmerror;

	InNum = Pm_CountDevices();
	j=0;
	for (i=1;i <=InNum && i<=32;i++){
		deviceinfo=(PmDeviceInfo *)Pm_GetDeviceInfo(i-1);
		if(TRUE==deviceinfo->input){
			sprintf(rtsyn_portlist[j],"%d:%s",i,deviceinfo->name);
			j++;
		}
	}
	rtsyn_nportlist=j;
	Pm_Terminate();
	
	return;
pmerror:
		Pm_Terminate();
	ctl->cmsg(  CMSG_ERROR, VERB_NORMAL, "PortMIDI error: %s\n", Pm_GetErrorText( pmerr ) );
	return;
}

int rtsyn_synth_start(){
	int i;
	unsigned int port;
	
	port=0;
	Pt_Start(1,NULL,NULL);
	pm_start_time = get_current_calender_time();
	pmerr=Pm_Initialize();
	if( pmerr != pmNoError ) goto pmerror;
	for(port=0;port<rtsyn_portnumber;port++){
				PortMidiStream* stream;
				void* timeinfo;

				pmerr=Pm_OpenInput( &stream,
				portID[port],
				NULL,
				(PMBUFF_SIZE),
				Pt_Time,
				NULL);
		midistream[port].stream=stream;
		if( pmerr != pmNoError ) goto pmerror;
		pmerr=Pm_SetFilter(midistream[port].stream,PM_FILT_CLOCK);
		if( pmerr != pmNoError ) goto pmerror;
	}

	return ~0;

pmerror:
		Pm_Terminate();
	ctl->cmsg(  CMSG_ERROR, VERB_NORMAL, "PortMIDI error: %s\n", Pm_GetErrorText( pmerr ) );
	return 0;

}


void rtsyn_synth_stop(){

	rtsyn_stop_playing();
//	play_mode->close_output();
	rtsyn_midiports_close();
	
	return;
pmerror:
		Pm_Terminate();
	ctl->cmsg(  CMSG_ERROR, VERB_NORMAL, "PortMIDI error: %s\n", Pm_GetErrorText( pmerr ) );
	return ;
}
void rtsyn_midiports_close(void){
	unsigned int port;

	for(port=0;port<rtsyn_portnumber;port++){
		pmerr=Pm_Abort(midistream[port].stream);
//		if( pmerr != pmNoError ) goto pmerror;
	}
	Pm_Terminate();
	Pt_Stop();
}

int rtsyn_play_some_data (void){
	PmMessage pmmsg;
	int played;	
	int j,port,exlen,data,shift;
	long pmlength,pmbpoint;
	double event_time;
	
	played=0;
		sleep(0);
		for(port=0;port<rtsyn_portnumber;port++){
			pmerr=Pm_Read(midistream[port].stream, pmbuffer, PMBUFF_SIZE);
			if(pmerr<0) goto pmerror;
			pmlength=pmerr;
			pmbpoint=0;
			while(pmbpoint<pmlength){
				played=~0;
				pmmsg=pmbuffer[pmbpoint].message;
				event_time=((double)pmbuffer[pmbpoint].timestamp)/1000.0 + pm_start_time;
				pmbpoint++;
				if( 1==rtsyn_play_one_data (port, pmmsg, event_time) ){	

					j=0;
					sysexbuffer[j++] = 0xf0;
					for (shift = 8,data=0; shift < 32 && (data != 0x0f7); shift += 8) {
       		         	data= (pmmsg >> shift) & 0x0FF;
						sysexbuffer[j++]=data;
					}
					if(data!=0x0f7){
						if(pmbpoint>=pmlength){
							{
								pmerr=Pm_Read(midistream[port].stream, pmbuffer, PMBUFF_SIZE);
								if(pmerr<0){goto pmerror; }
								sleep(0);
							}while(pmerr==8);
							pmlength=pmerr;
							pmbpoint=0;
						}
						while(j<EXBUFF_SIZE-4){
							for (shift=0,data=0; shift < 32 && (data != 0x0f7); shift += 8) {
                				data= (pmbuffer[pmbpoint].message >> shift) & 0x0FF;
								sysexbuffer[j++]=data;
							}
							pmbpoint++;
							if(data==0x0f7) break;
							if( pmbpoint>=pmlength ){
								{
									pmerr=Pm_Read(midistream[port].stream, pmbuffer, PMBUFF_SIZE);
									if(pmerr<0){goto pmerror;}
									sleep(0);
								}while(pmerr==0);
								pmlength=pmerr;
								pmbpoint=0;
							}
						}
					}
					exlen=j;
					rtsyn_play_one_sysex (sysexbuffer,exlen, event_time);
				}
			}
		}
	return played;
pmerror:
	Pm_Terminate();
	ctl->cmsg(  CMSG_ERROR, VERB_NORMAL, "PortMIDI error: %s\n", Pm_GetErrorText( pmerr ) );
	return 0;
}