Esempio n. 1
0
static
int alsa_seqmidi_attach(alsa_midi_t *m)
{
	alsa_seqmidi_t *self = (alsa_seqmidi_t*) m;
	int err;

	debug_log("midi: attach");

	if (self->seq)
		return -EALREADY;

	if ((err = snd_seq_open(&self->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
		error_log("failed to open alsa seq");
		return err;
	}
	snd_seq_set_client_name(self->seq, self->alsa_name);
	self->port_id = snd_seq_create_simple_port(self->seq, "port",
		SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_WRITE
#ifndef JACK_MIDI_DEBUG
		|SND_SEQ_PORT_CAP_NO_EXPORT
#endif
		,SND_SEQ_PORT_TYPE_APPLICATION);
	self->client_id = snd_seq_client_id(self->seq);

  	self->queue = snd_seq_alloc_queue(self->seq);
  	snd_seq_start_queue(self->seq, self->queue, 0);

	stream_attach(self, PORT_INPUT);
	stream_attach(self, PORT_OUTPUT);

	snd_seq_nonblock(self->seq, 1);

	return 0;
}
Esempio n. 2
0
void ALSADrv_MIDI_SetTempo(int tempo, int division)
{
    double tps;
    snd_seq_queue_tempo_t * t;
    int result;

    if (queueRunning) {
        snd_seq_stop_queue(seq, seq_queue, NULL);
        while ((result = snd_seq_drain_output(seq)) > 0) ;
        if (result < 0) {
            fprintf(stderr, "ALSA could not drain output: err %d\n", result);
        }
    }

    snd_seq_queue_tempo_alloca(&t);
    snd_seq_queue_tempo_set_tempo(t, 60000000 / tempo);
    snd_seq_queue_tempo_set_ppq(t, division);
    snd_seq_set_queue_tempo(seq, seq_queue, t);

    tps = ( (double) tempo * (double) division ) / 60.0;
    threadQueueTicks = (int) ceil(tps / (double) THREAD_QUEUE_INTERVAL);

    if (queueRunning) {
        snd_seq_start_queue(seq, seq_queue, NULL);
        while ((result = snd_seq_drain_output(seq)) > 0) ;
        if (result < 0) {
            fprintf(stderr, "ALSA could not drain output: err %d\n", result);
        }
    }
}
Esempio n. 3
0
void MidiQueue::start()
{
	snd_seq_start_queue(_sequencer, _id, nullptr);
	int result = snd_seq_drain_output(_sequencer);
	if (result < 0)
	{
		std::cerr << "MidiQueue::start error:" << snd_strerror(result) << std::endl;
	}
}
Esempio n. 4
0
void
mastermidibus::continue_from (midipulse tick)
{
#ifdef SEQ64_HAVE_LIBASOUND
    automutex locker(m_mutex);
    snd_seq_start_queue(m_alsa_seq, m_queue, NULL);     /* start timer */
    for (int i = 0; i < m_num_out_buses; ++i)
        m_buses_out[i]->continue_from(tick);
#endif
}
Esempio n. 5
0
void
mastermidibus::start ()
{
#ifdef HAVE_LIBASOUND
    automutex locker(m_mutex);
    snd_seq_start_queue(m_alsa_seq, m_queue, NULL);     /* start timer */
    for (int i = 0; i < m_num_out_buses; i++)
        m_buses_out[i]->start();
#endif
}
Esempio n. 6
0
void event_decoder_start_timer(snd_seq_t *handle, int queue,
			       int client ATTRIBUTE_UNUSED,
			       int port ATTRIBUTE_UNUSED)
{
	int err;

	if ((err = snd_seq_start_queue(handle, queue, NULL))<0)
		fprintf(stderr, "Timer event output error: %s\n", snd_strerror(err));
	while (snd_seq_drain_output(handle)>0)
		sleep(1);
}
Esempio n. 7
0
static PyObject *
alsaseq_start(PyObject *self /* Not used */, PyObject *args)
{
	if (!PyArg_ParseTuple(args, "" ))
		return NULL;

        snd_seq_start_queue(seq_handle, queue_id, NULL);
        snd_seq_drain_output(seq_handle);

	Py_INCREF(Py_None);
	return Py_None;
}
Esempio n. 8
0
/* gets it a runnin */
    void
mastermidibus::continue_from( long a_tick)
{
    lock();

    /* start timer */
    snd_seq_start_queue( m_alsa_seq, m_queue, NULL );

    for ( int i=0; i < m_num_out_buses; i++ )
        m_buses_out[i]->continue_from( a_tick );

    unlock();
}
Esempio n. 9
0
/*
 * Start the queue
 */
void SeqContext::seq_start_queue()
{
#ifdef USE_DRAIN
	snd_seq_drain_output(handle);
#else
	snd_seq_flush_output(handle);
#endif

	snd_seq_start_queue(handle, queue, 0);

	if( verbose ) fprintf(stderr, "Queue started\n");

}
Esempio n. 10
0
/* gets it a runnin */
void 
mastermidibus::start()
{
    lock();
         
    /* start timer */
    snd_seq_start_queue( m_alsa_seq, m_queue, NULL );
    
    for ( int i=0; i < m_num_out_buses; i++ )
	m_buses_out[i]->start();

     unlock();
}
Esempio n. 11
0
MidiAlsaSeq::MidiAlsaSeq() :
	MidiClient(),
	m_seqMutex(),
	m_seqHandle( NULL ),
	m_queueID( -1 ),
	m_quit( false ),
	m_portListUpdateTimer( this )
{
	int err;
	if( ( err = snd_seq_open( &m_seqHandle,
					probeDevice().toLatin1().constData(),
						SND_SEQ_OPEN_DUPLEX, 0 ) ) < 0 )
	{
		fprintf( stderr, "cannot open sequencer: %s\n",
							snd_strerror( err ) );
		return;
	}
	snd_seq_set_client_name( m_seqHandle, "LMMS" );


	m_queueID = snd_seq_alloc_queue( m_seqHandle );
	snd_seq_queue_tempo_t * tempo;
	snd_seq_queue_tempo_malloc( &tempo );
	snd_seq_queue_tempo_set_tempo( tempo, 6000000 /
					Engine::getSong()->getTempo() );
	snd_seq_queue_tempo_set_ppq( tempo, 16 );
	snd_seq_set_queue_tempo( m_seqHandle, m_queueID, tempo );
	snd_seq_queue_tempo_free( tempo );

	snd_seq_start_queue( m_seqHandle, m_queueID, NULL );
	changeQueueTempo( Engine::getSong()->getTempo() );
	connect( Engine::getSong(), SIGNAL( tempoChanged( bpm_t ) ),
			this, SLOT( changeQueueTempo( bpm_t ) ) );

	// initial list-update
	updatePortList();

	connect( &m_portListUpdateTimer, SIGNAL( timeout() ),
					this, SLOT( updatePortList() ) );
	// we check for port-changes every second
	m_portListUpdateTimer.start( 1000 );

	// use a pipe to detect shutdown
	if( pipe( m_pipe ) == -1 )
	{
		perror( __FILE__ ": pipe" );
	}

	start( QThread::IdlePriority );
}
Esempio n. 12
0
void JVlibForm::on_System_PlayMidi_button_toggled(bool checked) {
    if (checked) {
	seqTimer = new QTimer(this);
        System_PauseMidi_button->setEnabled(true);
        System_OpenMidi_button->setEnabled(false);
        System_PlayMidi_button->setText("Stop");
	System_MIDI_progressBar->setEnabled(true);
        connect_port();
        // queue won't actually start until it is drained
        int err = snd_seq_start_queue(seq, queue, NULL);
        check_snd("start queue", err);
	System_PlayMidi_status->on();
        connect(JVlibForm::seqTimer, SIGNAL(timeout()), this, SLOT(tickDisplay()));
        seqTimer->start(100);
	startPlayer(0);
    }
    else {
        if (seqTimer->isActive()) {
            disconnect(JVlibForm::seqTimer, SIGNAL(timeout()), this, SLOT(tickDisplay()));
            seqTimer->stop();
	    delete seqTimer;
        }
        snd_seq_stop_queue(seq,queue,NULL);
        snd_seq_drain_output(seq);
	stopPlayer();
	stop_sound();
        disconnect_port();
	System_PlayMidi_status->off();
	System_MIDI_progressBar->blockSignals(true);
        System_MIDI_progressBar->setValue(0);
	System_MIDI_progressBar->blockSignals(false);
        MIDI_time_display->setText("00:00");
        if (System_PauseMidi_button->isChecked()) {
            System_PauseMidi_button->blockSignals(true);
            System_PauseMidi_button->setChecked(false);
            System_PauseMidi_button->blockSignals(false);
            System_PauseMidi_button->setText("Pause");
        }
        System_PauseMidi_button->setEnabled(false);
        System_PlayMidi_button->setText("Play");
        System_OpenMidi_button->setEnabled(true);
	System_MIDI_Transpose->setEnabled(true);
	System_MIDI_progressBar->setEnabled(false);
	event_num=0;
    }
}   // end on_System_PlayMidi_button_toggled
Esempio n. 13
0
static PyObject *
alsaseq_start(PyObject *self /* Not used */, PyObject *args)
{
	if (!PyArg_ParseTuple(args, "" ))
		return NULL;

        if (!seq_handle) {
                PyErr_SetString(PyExc_RuntimeError, "Must initialize module with alsaseq.client() before using it");
                return NULL;
        }

        snd_seq_start_queue(seq_handle, queue_id, NULL);
        snd_seq_drain_output(seq_handle);

	Py_INCREF(Py_None);
	return Py_None;
}
Esempio n. 14
0
struct midi_handle *midi_open_alsa(unsigned char *name, int nports)
{
	int rc;
	int i;
	struct midi_handle_alsa *mh;
	unsigned char clientname[255], portname[255];

	sprintf(clientname, "Gneutronica (%d)", getpid());

	mh = (struct midi_handle_alsa *) malloc(sizeof(*mh));
	if (mh == NULL)
		return NULL;

	if (nports > MAX_PORTS)
		nports = MAX_PORTS;

	rc = snd_seq_open(&mh->seqp, name, SND_SEQ_OPEN_OUTPUT, 0666);
	if (rc < 0) {
		printf("snd_seq_open returns %d\n", rc);
		free(mh);
		return NULL;
	}
	rc = snd_seq_set_client_name(mh->seqp, clientname);
	if (rc < 0)
		printf("snd_seq_set_client_name failed \n");
	for (i=0;i<nports;i++) {
		sprintf(portname, "Gneutronica (%d) Track:%d", getpid(), i);
		mh->outputport[i] = snd_seq_create_simple_port(mh->seqp, portname,
				SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
				SND_SEQ_PORT_TYPE_MIDI_GENERIC);
		if (mh->outputport < 0)
			printf("snd_seq_create_simple_port %d failed\n", i);
	}

	mh->queue = snd_seq_alloc_queue(mh->seqp);
	if (mh->queue < 0)
		printf("snd_seq_alloc_queue failed.\n");

	snd_seq_start_queue(mh->seqp, mh->queue, 0);

	return (struct midi_handle *) mh;
}
Esempio n. 15
0
int ALSADrv_MIDI_StartPlayback(void (*service)(void))
{
    int result;
    
    ALSADrv_MIDI_HaltPlayback();

    threadService = service;
    threadQuit = 0;

    if (!queueRunning) {
        result = snd_seq_start_queue(seq, seq_queue, NULL);
        if (result < 0) {
            fprintf(stderr, "ALSA snd_seq_start_queue err %d\n", result);
            return ALSAErr_StartQueue;
        }

        while ((result = snd_seq_drain_output(seq)) > 0) ;
        if (result < 0) {
            fprintf(stderr, "ALSA could not drain output: err %d\n", result);
        }
    }

    queueRunning = 1;

    if (pthread_create(&thread, NULL, threadProc, NULL)) {
        fprintf(stderr, "ALSA pthread_create returned error\n");

        snd_seq_stop_queue(seq, seq_queue, NULL);
        while ((result = snd_seq_drain_output(seq)) > 0) ;
        if (result < 0) {
            fprintf(stderr, "ALSA could not drain output: err %d\n", result);
        }
        queueRunning = 0;
        
        return ALSAErr_PlayThread;
    }

    threadRunning = 1;

    return 0;
}
Esempio n. 16
0
/* queue is shared by both input and output, reference counted */
static PmError alsa_use_queue(void)
{
    if (queue_used == 0) {
        snd_seq_queue_tempo_t *tempo;

        queue = snd_seq_alloc_queue(seq);
        if (queue < 0) {
            pm_hosterror = queue;
            return pmHostError;
        }
        snd_seq_queue_tempo_alloca(&tempo);
        snd_seq_queue_tempo_set_tempo(tempo, 480000);
        snd_seq_queue_tempo_set_ppq(tempo, 480);
        pm_hosterror = snd_seq_set_queue_tempo(seq, queue, tempo);
        if (pm_hosterror < 0)
            return pmHostError;

        snd_seq_start_queue(seq, queue, NULL);
        snd_seq_drain_output(seq);
    }
    ++queue_used;
    return pmNoError;
}
Esempio n. 17
0
int
connect_to_alsa (struct a2j* self)
{
	int error;
	void * thread_status;

	self->port_add = jack_ringbuffer_create(2 * MAX_PORTS * sizeof(snd_seq_addr_t));
	if (self->port_add == NULL) {
		goto free_self;
	}

	self->port_del = jack_ringbuffer_create(2 * MAX_PORTS * sizeof(struct a2j_port *));
	if (self->port_del == NULL) {
		goto free_ringbuffer_add;
	}

	if (!a2j_stream_init(self)) {
		goto free_ringbuffer_outbound;
	}

	if ((error = snd_seq_open(&self->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
		a2j_error("failed to open alsa seq");
		goto close_stream;
	}

	if ((error = snd_seq_set_client_name(self->seq, "midi_in")) < 0) {
		a2j_error("snd_seq_set_client_name() failed");
		goto close_seq_client;
	}

	if ((self->port_id = snd_seq_create_simple_port(
		self->seq,
		"port",
		SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_WRITE
#ifndef DEBUG
		|SND_SEQ_PORT_CAP_NO_EXPORT
#endif
		,SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {

		a2j_error("snd_seq_create_simple_port() failed");
		goto close_seq_client;
	}

	if ((self->client_id = snd_seq_client_id(self->seq)) < 0) {
		a2j_error("snd_seq_client_id() failed");
		goto close_seq_client;
	}
	
	if ((self->queue = snd_seq_alloc_queue(self->seq)) < 0) {
		a2j_error("snd_seq_alloc_queue() failed");
		goto close_seq_client;
	}

	snd_seq_start_queue (self->seq, self->queue, 0); 

	a2j_stream_attach (&self->stream);

	if ((error = snd_seq_nonblock(self->seq, 1)) < 0) {
		a2j_error("snd_seq_nonblock() failed");
		goto close_seq_client;
	}

	snd_seq_drop_input (self->seq);

	a2j_add_ports(&self->stream);

	if (sem_init(&self->io_semaphore, 0, 0) < 0) {
		a2j_error("can't create IO semaphore");
		goto close_jack_client;
	}

	g_keep_alsa_walking = true;

	if (pthread_create(&self->alsa_io_thread, NULL, alsa_input_thread, self) < 0)
	{
		a2j_error("cannot start ALSA input thread");
		goto sem_destroy;
	}

	/* wake the poll loop in the alsa input thread so initial ports are fetched */
	if ((error = snd_seq_connect_from (self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE)) < 0) {
		a2j_error("snd_seq_connect_from() failed");
		goto join_io_thread;
	}

	return 0;

	g_keep_alsa_walking = false;  /* tell alsa threads to stop */
	snd_seq_disconnect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  join_io_thread:
	pthread_join(self->alsa_io_thread, &thread_status);
  sem_destroy:
	sem_destroy(&self->io_semaphore);
  close_jack_client:
	if ((error = jack_client_close(self->jack_client)) < 0) {
		a2j_error("Cannot close jack client");
	}
  close_seq_client:
	snd_seq_close(self->seq);
  close_stream:
	a2j_stream_close(self);
  free_ringbuffer_outbound:
	jack_ringbuffer_free(self->outbound_events);
	jack_ringbuffer_free(self->port_del);
  free_ringbuffer_add:
	jack_ringbuffer_free(self->port_add);
  free_self:
	free(self);
	return -1;
}