~ubuntu-branches/ubuntu/oneiric/denemo/oneiric

« back to all changes in this revision

Viewing changes to src/jackmidi.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-10-27 08:00:18 UTC
  • mfrom: (1.3.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20101027080018-tuekd0869v8ptnqv
Tags: upstream-0.8.16
ImportĀ upstreamĀ versionĀ 0.8.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include "jackmidi.h"
12
12
#include "pitchentry.h"
13
13
#include "smf.h"
 
14
#include "device_manager.h"
 
15
#include "moveviewport.h"
14
16
#define NOTE_OFF                0x80
15
17
#define NOTE_ON                 0x90
16
18
#define SYS_EXCLUSIVE_MESSAGE1  0xF0
17
19
#define MIDI_CONTROLLER         0xB0
18
20
#define MIDI_ALL_SOUND_OFF      0x78 
19
21
#define MIDI_ALL_NOTE_OFF       0x7b
20
 
#define MAX_NUMBER_OF_TRACKS    128
21
 
#define BUFFER_MAX_INDEX        100
 
22
 
 
23
#define DENEMO_MAX_PORTS (128)
 
24
 
 
25
#define MAX_NUMBER_OF_TRACKS   DENEMO_MAX_PORTS 
 
26
 
 
27
//   #define BUFFER_MAX_INDEX   100  rename DENEMO_BUFFER_MAX_INDEX
22
28
#define INPUT_PORT_NAME         "midi_in"
23
 
#define PROGRAM_NAME            "denemo"
24
 
jack_client_t   *jack_client = NULL;
 
29
#define OUTPUT_PORT_NAME         "midi_out"
 
30
#define CLIENT_NAME            "denemo"
 
31
#define MD Denemo.prefs.midi_device
 
32
 
25
33
jack_port_t     *input_port;
26
 
jack_port_t     *output_ports[MAX_NUMBER_OF_TRACKS];
27
 
 
28
 
static volatile gint BufferIndex;
29
 
static gint BufferFillIndex;
30
 
static volatile gboolean BufferEmpty = TRUE;
31
 
static volatile gboolean IMMEDIATE = TRUE;
32
 
 
33
 
struct midi_buffer
34
 
{
35
 
  unsigned char buffer[3];
36
 
  gint channel;
37
 
  gint jack_port;
38
 
};
39
 
 
40
 
struct midi_buffer global_midi_buffer[BUFFER_MAX_INDEX]; 
 
34
 
 
35
//maybe use this instead of looking through ports
 
36
//static gint jack_client_index = 0; 
 
37
 
 
38
//struct midi_output_device
 
39
//{
 
40
//  jack_client_t *jack_client; 
 
41
//  jack_port_t *output_ports[MAX_NUMBER_OF_TRACKS];
 
42
//  void *port_buffers[MAX_NUMBER_OF_TRACKS];//this is not needed, the value stored in it is looked up always
 
43
//};
 
44
 
 
45
//struct midi_output_device midi_device[DENEMO_MAX_DEVICES];
 
46
 
 
47
static double start_player = 0.0;
 
48
//static volatile gint BufferIndex;
 
49
//static gint BufferFillIndex;
 
50
//static volatile gboolean BufferEmpty = TRUE;
 
51
static gboolean playing_piece = FALSE;
 
52
//struct midi_buffer
 
53
//{
 
54
//  unsigned char buffer[3];
 
55
  //gint device_number;
 
56
  //gint port_number;
 
57
//  gint channel;
 
58
//};
 
59
 
 
60
//struct midi_buffer global_midi_buffer[BUFFER_MAX_INDEX]; 
41
61
 
42
62
static gint timeout_id = 0, kill_id=0;
43
63
static gdouble playback_duration = 0.0;
44
64
 
45
 
static smf_t            *smf = NULL;
46
65
static double           rate_limit = 0;
47
 
static gboolean         just_one_output = FALSE;
48
66
static gboolean         start_stopped = FALSE;
49
67
static gboolean         use_transport = FALSE;
50
 
 
51
 
static int              playback_started = -1, song_position = 0;
52
 
static gboolean         stop_midi_output = FALSE;
 
68
static gboolean         jack_server_running = TRUE;
53
69
static double           start_time = 0.0;//time in seconds to start at (from start of the smf)
54
70
static double           end_time = 0.0;//time in seconds to end at (from start of the smf)
55
71
 
56
 
double 
57
 
get_time(void)
58
 
{
59
 
        double          seconds;
60
 
        int             ret;
61
 
        struct timeval  tv;
62
 
 
63
 
        ret = gettimeofday(&tv, NULL);
64
 
 
65
 
        if (ret) {
66
 
                perror("gettimeofday");
67
 
        }
68
 
 
69
 
        seconds = tv.tv_sec + tv.tv_usec / 1000000.0;
70
 
 
71
 
        return seconds;
72
 
}
73
 
 
74
 
double
75
 
get_delta_time(void)
76
 
{
77
 
        static double   previously = -1.0;
78
 
        double          now;
79
 
        double          delta;
80
 
 
81
 
        now = get_time();
82
 
 
83
 
        if (previously == -1.0) {
84
 
                previously = now;
85
 
 
86
 
                return 0;
87
 
        }
88
 
 
89
 
        delta = now - previously;
90
 
        previously = now;
91
 
 
92
 
        assert(delta >= 0.0);
93
 
 
94
 
        return delta;
95
 
}
 
72
gint
 
73
maxnumber_of_clients(){
 
74
  return DENEMO_MAX_DEVICES;
 
75
}
 
76
 
 
77
gint
 
78
maxnumber_of_ports(){
 
79
  return MAX_NUMBER_OF_TRACKS;
 
80
}
 
81
 
 
82
gchar *
 
83
jackmidi_default_client_name(){
 
84
  return CLIENT_NAME;
 
85
}
 
86
 
 
87
gchar *
 
88
jackmidi_default_port_name(){
 
89
  return OUTPUT_PORT_NAME;
 
90
}
 
91
 
 
92
gboolean
 
93
jackmidi_server_running(){
 
94
  return jack_server_running;
 
95
}
 
96
 
96
97
 
97
98
static gboolean
98
99
warning_async(gpointer s)
99
100
{
100
 
        gchar *str = (gchar *)s;
101
 
 
102
 
        g_warning("%s", str);
103
 
 
104
 
        return FALSE;
 
101
  gchar *str = (gchar *)s;
 
102
  g_warning("%s", str);
 
103
  return FALSE;
105
104
}
106
105
 
107
106
static void
108
107
warn_from_jack_thread_context(const char *str)
109
108
{
110
109
#ifdef DEBUG
111
 
        g_idle_add(warning_async, (gpointer)str);
112
 
#endif
113
 
}
114
 
 
115
 
static double
116
 
nframes_to_ms(jack_nframes_t nframes)
117
 
{
118
 
        jack_nframes_t sr;
119
 
 
120
 
        sr = jack_get_sample_rate(jack_client);
121
 
 
122
 
        assert(sr > 0);
123
 
 
124
 
        return (nframes * 1000.0) / (double)sr;
125
 
}
126
 
 
127
 
static double
128
 
nframes_to_seconds(jack_nframes_t nframes)
129
 
{
130
 
        return nframes_to_ms(nframes) / 1000.0;
131
 
}
132
 
 
133
 
static jack_nframes_t
134
 
ms_to_nframes(double ms)
135
 
{
136
 
        jack_nframes_t sr;
137
 
 
138
 
        sr = jack_get_sample_rate(jack_client);
139
 
 
140
 
        assert(sr > 0);
141
 
 
142
 
        return ((double)sr * ms) / 1000.0;
143
 
}
144
 
 
145
 
static jack_nframes_t
146
 
seconds_to_nframes(double seconds)
147
 
{
148
 
        return ms_to_nframes(seconds * 1000.0);
149
 
}
150
 
 
151
 
static void
152
 
send_all_sound_off(void *port_buffers[MAX_NUMBER_OF_TRACKS], jack_nframes_t nframes)
153
 
{
154
 
        int i, channel;
155
 
        unsigned char *buffer;
156
 
        warn_from_jack_thread_context("\nSending all sound off!!!\n");
157
 
        for (i = 0; i < smf->number_of_tracks; i++) {
158
 
                for (channel = 0; channel < 16; channel++) {
159
 
#ifdef JACK_MIDI_NEEDS_NFRAMES
160
 
                        buffer = jack_midi_event_reserve(port_buffers[i], 0, 3, nframes);
161
 
#else
162
 
                        buffer = jack_midi_event_reserve(port_buffers[i], 0, 3);
163
 
#endif
164
 
                        if (buffer == NULL) {
165
 
                                warn_from_jack_thread_context("jack_midi_event_reserve failed, cannot send All Sound Off.");
166
 
                                break;
167
 
                        }
168
 
 
169
 
                        buffer[0] = MIDI_CONTROLLER | channel;
170
 
                        buffer[1] = MIDI_ALL_SOUND_OFF;
171
 
                        buffer[2] = 0;
172
 
                }
173
 
 
174
 
                if (just_one_output)
175
 
                        break;
176
 
        }
177
 
}
178
 
 
179
 
static gint move_on(DenemoGUI *gui){
180
 
  if(timeout_id==0)
181
 
    return FALSE;
182
 
  set_currentmeasurenum (gui, gui->si->currentmeasurenum+1);
183
 
  return TRUE;
184
 
}
185
 
 
186
 
gint jack_kill_timer(void){
 
110
  g_idle_add(warning_async, (gpointer)str);
 
111
#endif
 
112
}
 
113
 
 
114
 
 
115
gint 
 
116
jack_kill_timer(void){
187
117
  g_debug("Jack kill timer %d\n", timeout_id);
188
118
  if(timeout_id>0)
189
119
    g_source_remove(timeout_id);
196
126
 
197
127
static gboolean
198
128
timer_callback(gpointer bufferindex){
199
 
       gint index = (gint) bufferindex;
200
 
       global_midi_buffer[BufferFillIndex].buffer[0] = NOTE_OFF | global_midi_buffer[index].channel;
201
 
       global_midi_buffer[BufferFillIndex].buffer[1] = global_midi_buffer[index].buffer[1];
202
 
       global_midi_buffer[BufferFillIndex].buffer[2] = global_midi_buffer[index].buffer[2];
203
 
       global_midi_buffer[BufferFillIndex].jack_port = global_midi_buffer[index].jack_port;
204
 
       BufferFillIndex = BufferFillIndex+1 > BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
205
 
       BufferEmpty=FALSE;
206
 
       return FALSE;
 
129
#if 0
 
130
  gint index = (gint) bufferindex;
 
131
  global_midi_buffer[BufferFillIndex].buffer[0] = NOTE_OFF | global_midi_buffer[index].channel;
 
132
  global_midi_buffer[BufferFillIndex].buffer[1] = global_midi_buffer[index].buffer[1];
 
133
  global_midi_buffer[BufferFillIndex].buffer[2] = global_midi_buffer[index].buffer[2];
 
134
  global_midi_buffer[BufferFillIndex].device_number = global_midi_buffer[index].device_number;
 
135
  global_midi_buffer[BufferFillIndex].port_number = global_midi_buffer[index].port_number;
 
136
  BufferFillIndex = BufferFillIndex+1 > DENEMO_BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
 
137
  BufferEmpty=FALSE;
 
138
#endif
 
139
  return FALSE;
207
140
}
208
141
 
209
142
void 
210
143
jack_playpitch(gint key, gint duration){
211
 
 if (BufferEmpty==TRUE){ 
 
144
#if 0
 
145
  if (!jack_server_running)
 
146
    return;
 
147
  if (BufferEmpty==TRUE){ 
212
148
    DenemoStaff *curstaffstruct = (DenemoStaff *) Denemo.gui->si->currentstaff->data;
213
 
    global_midi_buffer[BufferFillIndex].buffer[0] = NOTE_ON | curstaffstruct->midi_channel;
 
149
    global_midi_buffer[BufferFillIndex].buffer[0] = NOTE_ON | get_midi_channel();
214
150
    global_midi_buffer[BufferFillIndex].buffer[1] = key; 
215
151
    global_midi_buffer[BufferFillIndex].buffer[2] = curstaffstruct->volume;
216
 
    global_midi_buffer[BufferFillIndex].channel = curstaffstruct->midi_channel;
217
 
    global_midi_buffer[BufferFillIndex].jack_port = curstaffstruct->jack_midi_out_port;
 
152
    global_midi_buffer[BufferFillIndex].channel = get_midi_channel();
 
153
    //global_midi_buffer[BufferFillIndex].device_number = //TODO
 
154
    global_midi_buffer[BufferFillIndex].port_number = get_midi_port();
218
155
    g_timeout_add(duration, timer_callback, (gpointer) BufferFillIndex);
219
 
    BufferFillIndex = BufferFillIndex+1 > BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
 
156
    BufferFillIndex = BufferFillIndex+1 > DENEMO_BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
220
157
    BufferEmpty=FALSE;
221
158
  }
 
159
#endif
222
160
}
223
161
 
224
 
void jack_output_midi_event(unsigned char *buffer){
225
 
 if (BufferEmpty==TRUE){ 
226
 
   global_midi_buffer[BufferFillIndex].buffer[0] = buffer[0];
227
 
   global_midi_buffer[BufferFillIndex].buffer[1] = buffer[1];
228
 
   global_midi_buffer[BufferFillIndex].buffer[2] = buffer[2];
229
 
   BufferFillIndex = BufferFillIndex+1 > BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
230
 
   if (BufferFillIndex == BufferIndex)
231
 
     g_debug("\nBuffer Overrun\n");
232
 
   BufferEmpty=FALSE;
233
 
 }
234
 
 else {
235
 
   global_midi_buffer[BufferFillIndex].buffer[0] = buffer[0];
236
 
   global_midi_buffer[BufferFillIndex].buffer[1] = buffer[1];
237
 
   global_midi_buffer[BufferFillIndex].buffer[2] = buffer[2];
238
 
   BufferFillIndex = BufferFillIndex+1 > BUFFER_MAX_INDEX ? 0 : BufferFillIndex+1;
239
 
 }
 
162
#define MDC MD[client_number].ports[port_number]
 
163
void 
 
164
jack_output_midi_event(unsigned char *buffer, gint client_number, gint port_number){
 
165
  DenemoScore *si = Denemo.gui->si;
 
166
 
 
167
  if (!jack_server_running)
 
168
    return; 
 
169
  gint velocity = ((gint)(si->master_volume * buffer[2]));
 
170
  if(velocity>0x7F) velocity = 0x7F;
 
171
  MDC.midi_buffer[ MDC.FillIndex].buffer[0] = buffer[0];
 
172
  MDC.midi_buffer[ MDC.FillIndex].buffer[1] = buffer[1];
 
173
  MDC.midi_buffer[ MDC.FillIndex].buffer[2] = velocity;
 
174
  MDC.FillIndex++;
 
175
  if(MDC.FillIndex > DENEMO_BUFFER_MAX_INDEX) 
 
176
    MDC.FillIndex = 0;
 
177
  if ( MDC.FillIndex ==  MDC.Index)
 
178
    g_debug("\nBuffer Overrun\n");
 
179
  MDC.BufferEmpty=FALSE;
 
180
  g_debug("output to client %d port %d\n", client_number, port_number);
240
181
}
241
182
 
242
183
static void
243
 
send_midi_event(jack_nframes_t nframes){
 
184
send_midi_event(jack_nframes_t nframes, gint client_number){
244
185
  unsigned char *buffer;
245
 
  gint i=global_midi_buffer[BufferIndex].jack_port;
246
 
  void *port_buffers[MAX_NUMBER_OF_TRACKS];
247
 
 
248
 
  if (output_ports[i]){
249
 
   port_buffers[i] = jack_port_get_buffer(output_ports[i], nframes);
250
 
   jack_midi_clear_buffer(port_buffers[i]);
251
 
   if (BufferEmpty==FALSE)
252
 
     while (BufferIndex != BufferFillIndex){
253
 
       buffer = jack_midi_event_reserve(port_buffers[i], 0, 3);
254
 
       if (buffer == NULL){
255
 
         warn_from_jack_thread_context("jack_midi_event_reserve_failed, NOTE_LOST.");
256
 
         return;
257
 
       }
258
 
       buffer[0] = global_midi_buffer[BufferIndex].buffer[0];
259
 
       buffer[1] = global_midi_buffer[BufferIndex].buffer[1];
260
 
       buffer[2] = global_midi_buffer[BufferIndex].buffer[2];
261
 
       BufferIndex = BufferIndex+1 > BUFFER_MAX_INDEX ? 0 : BufferIndex+1; 
 
186
  gint port_number;
 
187
  if(MD[client_number].jack_client==NULL)
 
188
    return;
 
189
  if(MD[client_number].ports==NULL)
 
190
    return;
 
191
  for(port_number = 0;MDC.output_port;port_number++) {
 
192
  gchar *outbuffer  = jack_port_get_buffer(MDC.output_port, nframes);
 
193
  jack_midi_clear_buffer(outbuffer);
 
194
  if (MDC.BufferEmpty==FALSE)
 
195
    while (MDC.Index != MDC.FillIndex){
 
196
      buffer = jack_midi_event_reserve(outbuffer, 0, 3);
 
197
      if (buffer == NULL){
 
198
        warn_from_jack_thread_context("jack_midi_event_reserve_failed, NOTE_LOST.");
 
199
        return;
 
200
      }
 
201
      buffer[0] = MDC.midi_buffer[MDC.Index].buffer[0];
 
202
      buffer[1] = MDC.midi_buffer[MDC.Index].buffer[1];
 
203
      buffer[2] = MDC.midi_buffer[MDC.Index].buffer[2];
 
204
      MDC.Index++;
 
205
      if(MDC.Index > DENEMO_BUFFER_MAX_INDEX)
 
206
        MDC.Index=0; 
 
207
    }
 
208
  MDC.BufferEmpty=TRUE;
 
209
  }
 
210
}
 
211
#undef MDC
 
212
 
 
213
static gboolean finish_play(gchar *callback) {
 
214
  if(callback && *callback)
 
215
    call_out_to_guile (callback);
 
216
  return FALSE;
 
217
}
 
218
 
 
219
static gdouble          last_draw_time;
 
220
static gdouble          pause_time = -1.0;
 
221
static gboolean jackmidi_play_smf_event(gchar *callback)
 
222
{
 
223
  DevicePort *DP;
 
224
  DenemoScore *si = Denemo.gui->si;
 
225
  smf_event_t *event = si->smf?smf_peek_next_event(si->smf):NULL;
 
226
  
 
227
  if (!jack_server_running)
 
228
    return FALSE;
 
229
  if (!MD[0].jack_client)
 
230
    return FALSE;
 
231
  if (!playing_piece)
 
232
    return finish_play(callback);
 
233
 
 
234
  if (event == NULL || (event->time_seconds > si->end_time)){  
 
235
    si->playingnow = NULL;
 
236
    playing_piece = FALSE;
 
237
    pause_time = -1.0;
 
238
    toggle_playbutton();
 
239
    return  finish_play(callback);
 
240
  }
 
241
  else 
 
242
    playing_piece = TRUE;
 
243
  
 
244
   /* Skip over metadata events. */
 
245
  if (smf_event_is_metadata(event)) {
 
246
    event = smf_get_next_event(si->smf);
 
247
    return TRUE; 
 
248
  } 
 
249
  // g_print("rightmost %f event %f\n", si->rightmost_time, event->time_seconds);
 
250
  if(si->rightmost_time>0.0 && event->time_seconds>si->rightmost_time)
 
251
    page_viewport();
 
252
  gdouble thetime = get_time() - si->start_player;
 
253
  pause_time = thetime;
 
254
  //g_print("thetime %f\n", thetime);
 
255
  thetime -= si->tempo_change_time - si->start_player;
 
256
  thetime *= si->master_tempo;
 
257
  thetime +=  si->tempo_change_time - si->start_player;
 
258
  //g_print("transformed to %f\n", thetime);
 
259
  if (thetime > event->time_seconds){
 
260
     event = smf_get_next_event(si->smf);
 
261
     si->playingnow = event->user_pointer;
 
262
     si->playhead = event->time_seconds;//the time of the playhead used in draw.c
 
263
     DP = event->track->user_pointer;
 
264
     if (!DP)
 
265
       return FALSE;
 
266
     //g_print("current object %p %x\n", event->user_pointer,((event->midi_buffer[0] & SYS_EXCLUSIVE_MESSAGE1)) );
 
267
     if(((event->midi_buffer[0] & SYS_EXCLUSIVE_MESSAGE1)==NOTE_ON) &&
 
268
        event->time_seconds - last_draw_time>Denemo.prefs.display_refresh) {
 
269
       //       g_print("drawing because %f %f\n", event->time_seconds, last_draw_time);
 
270
       last_draw_time = event->time_seconds;
 
271
       
 
272
       gtk_widget_queue_draw (Denemo.gui->scorearea);
262
273
     }
263
 
     BufferEmpty=TRUE;
264
 
   
 
274
    gint chan = (event->midi_buffer[0] & 0x0f);
 
275
    //g_print("message %x %x\n", event->midi_buffer[0] & SYS_EXCLUSIVE_MESSAGE1, PROGRAM_CHANGE);
 
276
    int success;
 
277
    switch((event->midi_buffer[0] & SYS_EXCLUSIVE_MESSAGE1))
 
278
    {
 
279
      case NOTE_ON: {
 
280
        if (si->end_time - event->time_seconds > 0.01) //Do not turn notes on too close to the end
 
281
        jack_output_midi_event(event->midi_buffer, DP->device_number, DP->port_number);
 
282
      }
 
283
      si->playhead += 0.001;//Make sure playhead is inside duration of note
 
284
      break;
 
285
      case NOTE_OFF:
 
286
        jack_output_midi_event(event->midi_buffer, DP->device_number, DP->port_number);
 
287
        si->playhead -= 0.001;//Make sure playhead is inside duration of note
 
288
        break;
 
289
      default:
 
290
        jack_output_midi_event(event->midi_buffer, DP->device_number, DP->port_number);
 
291
        break;
 
292
    }
 
293
 
265
294
  }
 
295
  return TRUE;
266
296
}
267
297
 
268
298
static void
269
 
process_midi_output(jack_nframes_t nframes)
270
 
{
271
 
        int             i, t, bytes_remaining, track_number;
272
 
        unsigned char  *buffer, tmp_status;
273
 
        void            *port_buffers[MAX_NUMBER_OF_TRACKS];
274
 
        jack_nframes_t  last_frame_time;
275
 
        smf_event_t *n;
276
 
        for (i = 0; i < smf->number_of_tracks; i++) {
277
 
          if(output_ports[i])
278
 
             port_buffers[i] = jack_port_get_buffer(output_ports[i], nframes);
279
 
             
280
 
             if (port_buffers[i] == NULL) {
281
 
               warn_from_jack_thread_context("jack_port_get_buffer failed, cannot send anything.");
282
 
               return;
283
 
             }
284
 
 
285
 
#ifdef JACK_MIDI_NEEDS_NFRAMES
286
 
                jack_midi_clear_buffer(port_buffers[i], nframes);
287
 
#else
288
 
                jack_midi_clear_buffer(port_buffers[i]);
289
 
#endif
290
 
 
291
 
                if (just_one_output)
292
 
                        break;
293
 
        }
294
 
 
295
 
        if (use_transport) 
296
 
          if (JackTransportStopped == jack_transport_query(jack_client, NULL))
297
 
                  return;
298
 
                
299
 
        last_frame_time = jack_last_frame_time(jack_client);
300
 
        /* End of song already? */
301
 
        if (playback_started < 0){
302
 
                warn_from_jack_thread_context/*g_debug*/("playback_started < 0");
303
 
                return;
304
 
        }
305
 
        /* We may push at most one byte per 0.32ms to stay below 31.25 Kbaud limit. */
306
 
        bytes_remaining = nframes_to_ms(nframes) * rate_limit;
307
 
 
308
 
        for (;;) {
309
 
                smf_event_t *event = smf_peek_next_event(smf);
310
 
 
311
 
                if (event == NULL || (event->time_seconds>end_time)) {
312
 
                        warn_from_jack_thread_context/*g_debug*/("End of song.");
313
 
                        jack_midi_playback_stop();
314
 
                        send_all_sound_off(port_buffers, nframes);
315
 
                        break;
316
 
                }
317
 
                
318
 
                /* Skip over metadata events. */
319
 
                if (smf_event_is_metadata(event)) {
320
 
                        n = smf_get_next_event(smf);
321
 
                        continue;
322
 
                }
323
 
 
324
 
                bytes_remaining -= event->midi_buffer_length;
325
 
                if (rate_limit > 0.0 && bytes_remaining <= 0) {
326
 
                        warn_from_jack_thread_context("Rate limiting in effect.");
327
 
                        break;
328
 
                }
329
 
 
330
 
                t = seconds_to_nframes(event->time_seconds - start_time) + playback_started - song_position + nframes - last_frame_time;
331
 
                /* If computed time is too much into the future, we'll need
332
 
                   to send it later. */
333
 
                if (t >= (int)nframes)
334
 
                        break;
335
 
 
336
 
                /* If computed time is < 0, we missed a cycle because of xrun. */
337
 
                if (t < 0)
338
 
                        t = 0;
339
 
 
340
 
                /* We will send this event; remove it from the queue. */
341
 
                n = smf_get_next_event(smf);
342
 
 
343
 
                /* Send it via proper output port. */
344
 
                track_number = event->track->track_number -1;
345
 
 
346
 
#ifdef JACK_MIDI_NEEDS_NFRAMES
347
 
                buffer = jack_midi_event_reserve(port_buffers[track_number], t, event->midi_buffer_length, nframes);
348
 
#else
349
 
                buffer = jack_midi_event_reserve(port_buffers[track_number], t, event->midi_buffer_length);
350
 
#endif
351
 
 
352
 
                if (buffer == NULL) {
353
 
                        warn_from_jack_thread_context("jack_midi_event_reserve failed, NOTE LOST.");
354
 
                        break;
355
 
                }
356
 
                memcpy(buffer, event->midi_buffer, event->midi_buffer_length);
357
 
        }
358
 
}
359
 
 
360
 
void
361
 
static process_midi_input(jack_nframes_t nframes)
 
299
process_midi_input(jack_nframes_t nframes)
362
300
{
363
301
  int read, events, i, channel;
364
302
  void           *port_buffer;
365
303
  jack_midi_event_t event;
366
304
  int             last_frame_time;
367
305
  static int      time_of_first_event = -1;
368
 
 
369
 
  last_frame_time = jack_last_frame_time(jack_client);
 
306
  
 
307
  //last_frame_time = jack_last_frame_time(midi_device[0].jack_client);
370
308
  port_buffer = jack_port_get_buffer(input_port, nframes);
371
309
  events = jack_midi_get_event_count(port_buffer);
372
310
  for (i = 0; i < events; i++) {
376
314
}
377
315
 
378
316
static int 
379
 
process_callback(jack_nframes_t nframes, void *notused)
 
317
process_callback(jack_nframes_t nframes, gint client_number)
380
318
{
381
319
  if (nframes <= 0) {
382
320
    warn_from_jack_thread_context("Process callback called with nframes = 0; bug in JACK?");
384
322
  }
385
323
  if(Denemo.gui->input_source==INPUTMIDI && input_port)
386
324
    process_midi_input(nframes);
387
 
  if (IMMEDIATE && Denemo.gui->si && output_ports)
388
 
      send_midi_event(nframes);
389
 
  if (!IMMEDIATE && Denemo.gui->si && smf && output_ports)
390
 
      process_midi_output(nframes);
 
325
  
 
326
  send_midi_event(nframes, client_number);
391
327
  return 0;
392
328
}
393
329
 
 
330
static void
 
331
register_jack_midi_port(gint client_number, gint port_number, gchar *port_name){
 
332
#if 0
 
333
  if (!midi_device[client_number].jack_client)
 
334
    return;
 
335
  if (!jack_server_running)
 
336
    return;
 
337
 
 
338
  midi_device[client_number].output_ports[port_number] = jack_port_register(midi_device[client_number].jack_client,
 
339
                          port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
 
340
  jack_nframes_t nframes = jack_get_buffer_size(midi_device[client_number].jack_client);
 
341
  jack_midi_clear_buffer(jack_port_get_buffer(midi_device[client_number].output_ports[port_number], nframes));
 
342
#endif
 
343
}
394
344
/* returns the jack midi port number that
395
345
 *  has been assigned
396
346
 */
397
347
int 
398
 
create_jack_midi_port(char* port_name){
399
 
  if (jack_client != NULL){
400
 
        gint i;
401
 
        jack_nframes_t nframes = jack_get_buffer_size(jack_client);
402
 
        /* only assign it if the port has not been assigned already */  
403
 
        for (i=0;i <= MAX_NUMBER_OF_TRACKS;i++){
404
 
 
405
 
          if (output_ports[i] == NULL){
406
 
            output_ports[i] = jack_port_register(jack_client, 
407
 
                                        port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
408
 
 
409
 
                if (output_ports[i] == NULL) {
410
 
                        g_critical("Could not register JACKMIDI output_port[%d] '%s'.",i, port_name);
411
 
                }
412
 
  
413
 
                /* clear buffer */
414
 
                if (output_ports[i]){   
415
 
                  jack_midi_clear_buffer(jack_port_get_buffer(output_ports[i], nframes));
416
 
                }
417
 
        
418
 
                if (output_ports[i] != NULL)    
419
 
                  g_debug("\nassigning jackmidi port output_port[%d]\n", i);
420
 
                return i;
421
 
          }
422
 
        }
423
 
    return 0;
424
 
  }
425
 
  else 
426
 
    return -1;
427
 
}
428
 
 
429
 
void
430
 
create_jack_midi_ports_from_score(){
431
 
  staffnode *curstaff;
432
 
  DenemoStaff *curstaffstruct;
433
 
  curstaff = Denemo.gui->si->thescore;
434
 
  
435
 
  while (curstaff)
436
 
  {
437
 
    curstaffstruct = (DenemoStaff *) curstaff->data;
438
 
    g_debug("\nStaff name = %s\n", curstaffstruct->denemo_name->str);
439
 
    /* Create port and assign jack port number */
440
 
    curstaffstruct->jack_midi_out_port = create_jack_midi_port(curstaffstruct->denemo_name->str);
441
 
    curstaff = curstaff->next;
442
 
  }
443
 
}
444
 
 
445
 
int 
446
 
remove_jack_midi_port(int port_number){
447
 
        int err;
448
 
        err = 0;
449
 
        if (output_ports[port_number] != NULL){
450
 
                err = jack_port_unregister(jack_client, output_ports[port_number]);
451
 
                output_ports[port_number] = NULL;
452
 
#ifdef DEBUG
453
 
                g_debug("\nremove jackmidi port number = %d\n", port_number);
454
 
#endif
455
 
        }
456
 
        return err;
457
 
}
458
 
 
459
 
void
460
 
remove_all_jack_midi_ports(){
461
 
        int err,i;
462
 
        err = 0;
463
 
 
464
 
        
465
 
        for (i=0;i < MAX_NUMBER_OF_TRACKS;i++){
466
 
 
467
 
          if (output_ports[i] != NULL){
468
 
 
469
 
                err = jack_port_unregister(jack_client, output_ports[i]);
470
 
                output_ports[i] = NULL;
471
 
#ifdef DEBUG
472
 
                g_debug("\nremoving jackmidi port number = %d\n", i);
473
 
#endif
474
 
          }
475
 
        }
476
 
        /*return err;*/
477
 
}
478
 
 
479
 
int
480
 
rename_jack_midi_port(int port_number, char *port_name){
481
 
        int err = 0;
482
 
        if (output_ports[port_number] != NULL)
483
 
          err = jack_port_set_name (output_ports[port_number], port_name);
484
 
        
485
 
        g_debug("Trying to rename JACK port output_ports[%d] to %s\n",port_number, port_name);
486
 
 
487
 
        if (err)
488
 
          g_critical("Could not rename JACK port output_ports[%d] to %s",port_number, port_name);
489
 
        
490
 
        return err;     
491
 
}
 
348
create_jack_midi_port(gint client_number){
 
349
#if 0
 
350
  if (!midi_device[client_number].jack_client)
 
351
    return -1;
 
352
 
 
353
  if (!jack_server_running)
 
354
    return -1;
 
355
 
 
356
  char port_name[12];  
 
357
  gint i;
 
358
  
 
359
  /* only assign it if the port has not been assigned already */        
 
360
  for (i=0;i <= MAX_NUMBER_OF_TRACKS;i++)
 
361
    if (!midi_device[client_number].output_ports[i]){ 
 
362
      sprintf(port_name, "%s:%d",OUTPUT_PORT_NAME, i);
 
363
      register_jack_midi_port(client_number,i, port_name);      
 
364
      if (midi_device[client_number].output_ports[i]){
 
365
        /* clear buffer */
 
366
        g_debug("\nassigning jackmidi port output_port[%d]\n", i);
 
367
        MD[client_number].port_names = g_list_append(MD[client_number].port_names, g_string_new(port_name));    
 
368
        return i;
 
369
      } else {
 
370
         g_critical("Could not register JACKMIDI %s:[%d]",OUTPUT_PORT_NAME,i);
 
371
         return -1;
 
372
        }
 
373
    }
 
374
 
 
375
#endif 
 
376
  return -1;
 
377
}
 
378
 
 
379
int
 
380
create_jack_midi_client(){
 
381
#if 0
 
382
  gint i;
 
383
  gint err;
 
384
  char client_name[12];
 
385
  
 
386
  if (!jack_server_running)
 
387
    return -1;
 
388
  
 
389
  for (i=0;i <= DENEMO_MAX_DEVICES;i++)
 
390
    if (!midi_device[i].jack_client){
 
391
      sprintf(client_name, "%s:%d",CLIENT_NAME, i); 
 
392
      midi_device[i].jack_client = jack_client_open(client_name, JackNoStartServer, NULL);
 
393
      
 
394
      if (!midi_device[i].jack_client){
 
395
        jack_server_running = FALSE;
 
396
        return -1;
 
397
      }
 
398
      
 
399
      //  if (i==0)
 
400
        if (jack_set_process_callback(midi_device[i].jack_client, process_callback, i)){
 
401
          jack_server_running = FALSE;
 
402
          g_critical("Could not register JACK process callback.");
 
403
          return -1;
 
404
        }
 
405
         
 
406
      /* activate client */
 
407
      if (midi_device[i].jack_client){
 
408
        jack_activate(midi_device[i].jack_client);
 
409
        g_debug("\nassigning jackmidi client %s\n", client_name);
 
410
        MD[i].client_name = g_string_new(client_name);
 
411
        MD[i].port_names = NULL;
 
412
        jack_client_index++;
 
413
        return i;
 
414
      } else {
 
415
        jack_server_running = FALSE;
 
416
        g_critical("Could not connect to the JACK server; run jackd first?");
 
417
      }
 
418
    }
 
419
#endif
 
420
  return -1;
 
421
}
 
422
 
 
423
 
 
424
 
 
425
int 
 
426
remove_jack_midi_port(int client_number, int port_number){
 
427
#if 0
 
428
  int err,i;
 
429
  err = 0;
 
430
  
 
431
    if (midi_device[client_number].output_ports[port_number]){
 
432
      err = jack_port_unregister(midi_device[client_number].jack_client, midi_device[client_number].output_ports[port_number]);
 
433
      midi_device[client_number].output_ports[port_number] = NULL;
 
434
      GList *n = g_list_nth(MD[client_number].port_names, port_number);
 
435
      MD[client_number].port_names = g_list_remove(MD[client_number].port_names, 
 
436
                      n->data);
 
437
      g_debug("\nremove jackmidi device number %d, port number = %d\n", client_number, port_number);
 
438
      return err;
 
439
    }
 
440
  return err;
 
441
#endif
 
442
  return -1;
 
443
}
 
444
 
 
445
int 
 
446
remove_last_jack_midi_port(int client_number){
 
447
#if 0
 
448
  int err,i;
 
449
  err = 0;
 
450
  
 
451
  for (i=MAX_NUMBER_OF_TRACKS;i>=0;i--)
 
452
    if (midi_device[client_number].output_ports[i]){
 
453
      err = jack_port_unregister(midi_device[client_number].jack_client, midi_device[client_number].output_ports[i]);
 
454
      midi_device[client_number].output_ports[i] = NULL;
 
455
      GList *n = g_list_nth(MD[client_number].port_names, (int) i);
 
456
      MD[client_number].port_names = g_list_remove(MD[client_number].port_names, 
 
457
                      n->data);
 
458
      g_debug("\nremove jackmidi port number = %d\n", i);
 
459
      return err;
 
460
    }
 
461
  return err;
 
462
#endif
 
463
  return -1;
 
464
}
 
465
 
 
466
void
 
467
remove_all_jack_midi_ports(int client_number){
 
468
#if 0
 
469
  int err,i;
 
470
  err = 0;
 
471
 
 
472
  for (i=MAX_NUMBER_OF_TRACKS;i>=0;i--)
 
473
    if (midi_device[client_number].output_ports[i]){
 
474
      jack_port_unregister(midi_device[client_number].jack_client, midi_device[client_number].output_ports[i]);
 
475
      midi_device[client_number].output_ports[i] = NULL;
 
476
      g_debug("\nremoving jackmidi port number = %d\n", i);
 
477
    }
 
478
#endif
 
479
}
 
480
 
 
481
int
 
482
remove_jack_midi_client(gint i){
 
483
#if 0
 
484
  remove_all_jack_midi_ports(i);
 
485
  jack_deactivate(midi_device[i].jack_client);
 
486
  jack_client_close(midi_device[i].jack_client);
 
487
  midi_device[i].jack_client = NULL;
 
488
  g_string_free(MD[i].client_name, TRUE);
 
489
  MD[i].client_name = NULL;
 
490
  jack_client_index--;
 
491
#endif
 
492
  return i;
 
493
}
 
494
 
 
495
void
 
496
remove_all_jack_midi_clients(){
 
497
#if 0
 
498
  while (jack_client_index)
 
499
    remove_jack_midi_client(jack_client_index-1);
 
500
#endif
 
501
}
 
502
 
 
503
#define MDC MD[client_number].ports[port_number]
 
504
int
 
505
rename_jack_midi_port(int client_number, int port_number, char *port_name){
 
506
  int err = -1;
 
507
 
 
508
  if (MD[client_number].ports[port_number].port_name) 
 
509
    err = jack_port_set_name (MDC.output_port, port_name);
 
510
  if (!err)
 
511
    MD[client_number].ports[port_number].port_name = g_string_new(port_name);
 
512
  else    
 
513
    g_critical("Could not rename JACK device %d output_ports[%d] to %s",client_number, port_number, port_name);
 
514
  
 
515
  return err;
 
516
}
 
517
#undef MDC
492
518
 
493
519
void 
494
520
stop_jack(void){
495
 
  if(!jack_client)
496
 
    return;
497
 
  remove_all_jack_midi_ports();
498
 
  int err = jack_port_unregister(jack_client, input_port);
499
 
  jack_deactivate(jack_client);
500
 
  jack_client_close(jack_client);
501
 
  jack_client = NULL;
 
521
#if 0
 
522
  if (midi_device[0].jack_client)
 
523
    jack_port_unregister(midi_device[0].jack_client, input_port);
 
524
  remove_all_jack_midi_clients();
 
525
#endif
502
526
}
503
527
 
504
528
int
505
529
init_jack(void){
506
 
  int err = 0;
507
 
  
508
 
  jack_client = jack_client_open(PROGRAM_NAME, JackNullOption, NULL);
509
 
 
510
 
  if (jack_client == NULL) {
511
 
    g_critical("Could not connect to the JACK server; run jackd first?");
512
 
  }
513
 
  err = jack_set_process_callback(jack_client, process_callback, 0);
514
 
  if (err) {
515
 
    g_critical("Could not register JACK process callback.");
516
 
  }
517
 
  
518
 
  input_port = jack_port_register(jack_client, INPUT_PORT_NAME, JACK_DEFAULT_MIDI_TYPE,
519
 
                          JackPortIsInput, 0);
520
 
   
521
 
  if (input_port == NULL) {
522
 
    g_critical("Could not register JACK input port.");
523
 
  }
524
 
 
525
 
  if (jack_activate(jack_client)) {
526
 
    g_critical("Cannot activate JACK client.");
527
 
  }
 
530
  int i, err, port_number;
 
531
  i = err = port_number = 0;
 
532
  //DeviceManager();
 
533
  if(MD==NULL) {
 
534
    jack_server_running = FALSE;
 
535
    if(Denemo.prefs.midi_audio_output==Jack)
 
536
      g_warning("No devices in preferences, edit->preferences->MIDI add devices and re-start");
 
537
    return -1;}
 
538
 
 
539
  for (i=0;MD[i].client_name;i++){
 
540
    g_debug("\njack init *** client name == %s \n",MD[i].client_name->str);
 
541
    //create_jack_midi_client_from_load(MD[i].client_name->str);
 
542
    MD[i].jack_client = (gpointer)jack_client_open(MD[i].client_name->str, JackNoStartServer, NULL);
 
543
    if(MD[i].jack_client == NULL) {
 
544
      if(Denemo.prefs.midi_audio_output==Jack)
 
545
        g_warning("Could not open JACK client %s, no jack server running?", MD[i].client_name->str );
 
546
      return -1;
 
547
    }
 
548
    if (jack_set_process_callback(MD[i].jack_client, (JackProcessCallback)process_callback, (void *)i)){
 
549
      jack_server_running = FALSE;
 
550
      g_warning("Could not register JACK process callback.");
 
551
      return -1;
 
552
    }
 
553
 
 
554
    if(MD[i].jack_client) {
 
555
      gint j;
 
556
      for(j=0; MD[i].ports && j<DENEMO_MAX_PORTS && MD[i].ports[j].port_name; j++) {
 
557
        MD[i].ports[j].output_port = 
 
558
          (gpointer) jack_port_register(MD[i].jack_client,
 
559
                                        MD[i].ports[j].port_name->str, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
 
560
        MD[i].ports[j].Index = MD[i].ports[j].FillIndex = 0;
 
561
        MD[i].ports[j].BufferEmpty = TRUE;   
 
562
      }
 
563
      if (i==0 && MD[0].jack_client && input_port==NULL)
 
564
        input_port = jack_port_register(MD[0].jack_client, INPUT_PORT_NAME, JACK_DEFAULT_MIDI_TYPE,
 
565
                                      JackPortIsInput, 0);
 
566
  
 
567
      /* activate client */
 
568
      jack_activate(MD[i].jack_client);
 
569
    }
 
570
  }  
528
571
  return err;
529
572
}
530
573
 
531
 
void
532
 
jack_start_restart (void){
533
 
  g_debug("\nJack Start/Restart button pressed\n");
534
 
  if (jack_client == NULL){
535
 
    g_debug("\nStarting Jack\n");
536
 
    init_jack();
537
 
    create_jack_midi_ports_from_score();
538
 
  }
539
 
  if (jack_client != NULL){
540
 
    g_debug("\nRestarting Jack\n");
541
 
    stop_jack();
542
 
    init_jack();
543
 
    create_jack_midi_ports_from_score();
544
 
  }
545
 
}
546
 
 
547
 
static void
548
 
jack_midi_player (void) {
549
 
 
550
 
  if (smf->number_of_tracks > MAX_NUMBER_OF_TRACKS) { 
551
 
     g_warning("Number of tracks (%d) exceeds maximum for per-track output; implying '-s' option.", smf->number_of_tracks);
552
 
     just_one_output = TRUE;
553
 
  }
554
 
  g_debug("\nNumber of tracks = %d\n", smf->number_of_tracks);
555
 
 
556
 
 if (use_transport && !start_stopped) {
557
 
    jack_transport_locate(jack_client, 0);
558
 
    jack_transport_start(jack_client);
559
 
  }
560
 
 
561
 
  playback_started = jack_frame_time(jack_client);
562
 
  IMMEDIATE=FALSE;
563
 
}
564
 
 
565
 
void
566
 
jack_midi_playback_start()
 
574
 
 
575
 
 
576
void jack_midi_play(gchar *callback)
567
577
{
568
578
  DenemoGUI *gui = Denemo.gui;
569
 
  playback_started = -1, song_position = 0, stop_midi_output = FALSE;
570
 
  
571
 
  /* set tranport on/off */
572
 
  use_transport = (gboolean)Denemo.prefs.jacktransport; 
573
 
  /* set transport start_stopped */
574
 
  start_stopped = (gboolean) Denemo.prefs.jacktransport_start_stopped; 
575
 
  if (jack_client != NULL){
576
 
     if((gui->si->smf==NULL) || (gui->si->smfsync!=gui->si->changecount))
577
 
       exportmidi (NULL, gui->si, 1, 0/* means to end */);
578
 
 
579
 
 
580
 
 
581
 
 
582
 
          smf = Denemo.gui->si->smf;
583
 
          if (smf == NULL) {
584
 
            g_critical("Loading SMF failed.");
585
 
            return;
586
 
          }
587
 
          smf_rewind(smf);
588
 
          playback_duration = smf_get_length_seconds(smf);
589
 
          g_debug("%s.\n", smf_decode(smf));
590
 
 
591
 
 
592
 
 
593
 
          DenemoObject *curobj;
594
 
          start_time = 0.0;
595
 
          curobj = get_mark_object();
596
 
          if(curobj==NULL && gui->si->currentobject)
597
 
            curobj = gui->si->currentobject->data;
598
 
          if(curobj && curobj->midi_events) {
599
 
            smf_event_t *event = curobj->midi_events->data;
600
 
            start_time = event->time_seconds;
601
 
            g_debug("\nsetting start %f\n", start_time);
602
 
          }
603
 
          end_time = playback_duration;
604
 
          curobj = NULL;
605
 
          curobj =  get_point_object();
606
 
          if(curobj && curobj->midi_events)/*is this ever true?*/ { 
607
 
            smf_event_t *event = g_list_last(curobj->midi_events)->data;
608
 
            end_time = event->time_seconds;
609
 
            g_debug("\nsetting end %f\n", end_time);       
610
 
            //could investigate to see if event is NoteOn and g_warning("Setting stop time to a NoteOn event!");
611
 
          } 
612
 
 
613
 
 
614
 
          if(start_time>end_time) {
615
 
            gdouble temp = start_time;
616
 
            start_time = end_time;
617
 
            end_time = temp;
618
 
          }
619
 
          playback_duration = end_time - start_time;
620
 
          g_debug("\nstart %f for %f seconds\n",start_time, playback_duration);
621
 
          /* execute jackmidi player function */ 
622
 
          jack_midi_player();
623
 
 
624
 
 
625
 
            if(gui->si->end==0) {//0 means not set, we move the cursor on unless the specific range was specified
626
 
              DenemoStaff *staff = (DenemoStaff *) gui->si->currentstaff->data;
627
 
              //FIXME add a delay before starting the timer.
628
 
              timeout_id = g_timeout_add ( 4*((double)staff->timesig.time1/(double)staff->timesig.time2)/(gui->si->tempo/(60.0*1000.0)), (GSourceFunc)move_on, gui);
629
 
              // g_print("Setting end time to %f %u\n", duration*1000, (guint)(duration*1000));
630
 
              kill_id = g_timeout_add ((guint)(playback_duration*1000), (GSourceFunc)jack_kill_timer, NULL);
631
 
            }
632
 
  }//if jack_client  
633
 
  return;
 
579
  if (!jack_server_running)
 
580
    return;
 
581
  if((gui->si->smf==NULL) || (gui->si->smfsync!=gui->si->changecount))
 
582
    generate_midi();
 
583
  if (Denemo.gui->si->smf == NULL) {
 
584
    g_critical("Loading SMF failed.");
 
585
    return;
 
586
  }
 
587
  static GString *callback_string;
 
588
 
 
589
  if(callback_string==NULL)
 
590
    callback_string=g_string_new("");
 
591
  if(callback)
 
592
    g_string_assign(callback_string, callback);
 
593
  else
 
594
    g_string_assign(callback_string,"(display \"Stopped Playing\")");
 
595
  
 
596
 
 
597
  if(playing_piece) {
 
598
    // pause
 
599
    toggle_playbutton();
 
600
    playing_piece = FALSE;
 
601
    (void)finish_play(callback);
 
602
    return;
 
603
  }
 
604
  if(gui->si->end_time<0.0)
 
605
    gui->si->end_time = smf_get_length_seconds(Denemo.gui->si->smf);
 
606
  
 
607
  if(gui->si->start_time>gui->si->end_time)
 
608
    gui->si->start_time =  0.0;
 
609
  if(gui->si->start_time<0.0)
 
610
    gui->si->start_time = 0.0;
 
611
  if( (pause_time>0.0) && (pause_time<gui->si->end_time))
 
612
    gui->si->start_player = get_time() - pause_time;
 
613
  else
 
614
    pause_time = -1.0, gui->si->start_player = get_time() - gui->si->start_time;
 
615
  playing_piece = TRUE;
 
616
  toggle_playbutton();
 
617
  gui->si->tempo_change_time = gui->si->start_player;
 
618
  
 
619
  smf_rewind(Denemo.gui->si->smf);
 
620
  last_draw_time = 0.0;
 
621
  g_idle_add(jackmidi_play_smf_event, callback_string->str);
 
622
  smf_seek_to_seconds(gui->si->smf, pause_time>0.0? pause_time:gui->si->start_time);
634
623
}
635
624
 
636
625
void
637
626
jack_midi_playback_stop ()
638
627
{
639
 
   stop_midi_output = TRUE;
640
 
   if(jack_client)
641
 
     jack_transport_stop(jack_client);
642
 
   g_debug("\nStopping Transport and midi playback\n");
643
 
   playback_started = -1;
644
 
   IMMEDIATE=TRUE;
645
 
}
646
 
 
647
 
 
648
 
#endif // _HAVE_JACK_
 
628
   //if(jack_client)
 
629
     //jack_transport_stop(jack_client);
 
630
  if(playing_piece)
 
631
    toggle_playbutton();
 
632
  Denemo.gui->si->playingnow = NULL;
 
633
  playing_piece = FALSE;
 
634
  pause_time = -1.0;
 
635
}
 
636
 
 
637
void
 
638
jack_midi_panic()
 
639
{
 
640
  gint i,j,c;
 
641
  gint client_number, port_number;
 
642
  jack_midi_playback_stop ();
 
643
  unsigned char buffer[3];
 
644
 
 
645
  //flush buffers, stop callback?
 
646
#define MD Denemo.prefs.midi_device
 
647
  for (i=0;MD[i].client_name;i++)
 
648
    for(j=0;MD[i].ports[j].port_name;j++) 
 
649
      for (c=0;c<16;c++){
 
650
        buffer[0] = MIDI_CONTROLLER | c;
 
651
        buffer[1] = MIDI_ALL_SOUND_OFF;
 
652
        buffer[2] = 0x00;  
 
653
        jack_output_midi_event(buffer, i, j);
 
654
      }
 
655
  
 
656
  Denemo.gui->si->end_time = pause_time = -1.0;
 
657
  Denemo.gui->si->start_time =  0.0;
 
658
  displayhelper(Denemo.gui);
 
659
}
 
660
 
 
661
#else //If not _HAVE_JACK_
 
662
void jack_playpitch(){}
 
663
void jack_output_midi_event(){}
 
664
int jack_kill_timer(){}
 
665
void jack_midi_playback_stop (){}
 
666
void jack_midi_playback_start (){}
 
667
void jack_midi_play(){}
 
668
void remove_last_jack_midi_port (){}
 
669
void create_jack_midi_port (){}
 
670
void remove_jack_midi_client (){}
 
671
void remove_jack_midi_port (){}
 
672
void rename_jack_midi_port (){}
 
673
 
 
674
void create_jack_midi_client (){}
 
675
jackmidi_default_client_name(){}
 
676
jackmidi_default_port_name(){}
 
677
int maxnumber_of_ports(){ return 0;}
 
678
int maxnumber_of_clients(){ return 0;}
 
679
int jackmidi_server_running(){ return 0;}
 
680
void stop_jack(){}
 
681
void jack_midi_panic(){}
 
682
#endif 
649
683