Beispiel #1
0
/**************************************************************************
 * 			midiCloseSeq				[internal]
 */
static int midiCloseSeq(void)
{
    if (--numOpenMidiSeq == 0) {
	snd_seq_delete_simple_port(midiSeq, port_out);
	snd_seq_delete_simple_port(midiSeq, port_in);
	snd_seq_close(midiSeq);
	midiSeq = NULL;
    }
    return 0;
}
void MidiAlsaSeq::removePort( MidiPort * _port )
{
	if( m_portIDs.contains( _port ) )
	{
		m_seqMutex.lock();
		snd_seq_delete_simple_port( m_seqHandle, m_portIDs[_port][0] );
		snd_seq_delete_simple_port( m_seqHandle, m_portIDs[_port][1] );
		m_seqMutex.unlock();

		m_portIDs.remove( _port );
	}
	MidiClient::removePort( _port );
}
Beispiel #3
0
/** del_aubio_alsa_seq_driver */
int del_aubio_alsa_seq_driver(aubio_midi_driver_t* p)
{
    aubio_alsa_seq_driver_t* dev;

    dev = (aubio_alsa_seq_driver_t*) p;
    if (dev == NULL) {
        return AUBIO_OK;
    }

    dev->status = AUBIO_MIDI_DONE;

    /* cancel the thread and wait for it before cleaning up */
    if (dev->thread) {
        if (pthread_cancel(dev->thread)) {
            AUBIO_ERR( "Failed to cancel the midi thread");
            return AUBIO_FAIL;
        }
        if (pthread_join(dev->thread, NULL)) {
            AUBIO_ERR( "Failed to join the midi thread");
            return AUBIO_FAIL;
        }
    }
    if (dev->seq_port >= 0) {
        snd_seq_delete_simple_port (dev->seq_handle, dev->seq_port);
    }
    if (dev->seq_handle) {
        snd_seq_drain_output(dev->seq_handle);
        snd_seq_close(dev->seq_handle);
    }
    AUBIO_FREE(dev);
    return AUBIO_OK;
}
Beispiel #4
0
qxgeditMidiDevice::~qxgeditMidiDevice (void)
{
	// Reset pseudo-singleton reference.
	g_pMidiDevice = NULL;

	// Last but not least, delete input thread...
	if (m_pInputThread) {
		// Try to terminate executive thread,
		// but give it a bit of time to cleanup...
		if (m_pInputThread->isRunning()) {
			m_pInputThread->setRunState(false);
		//	m_pInputThread->terminate();
			m_pInputThread->wait();
		}
		delete m_pInputThread;
		m_pInputThread = NULL;
	}

	if (m_pAlsaSeq) {
		snd_seq_delete_simple_port(m_pAlsaSeq, m_iAlsaPort);
		m_iAlsaPort   = -1;
		snd_seq_close(m_pAlsaSeq);
		m_iAlsaClient = -1;
		m_pAlsaSeq    = NULL;
	}
}
Beispiel #5
0
void ALSADrv_MIDI_Shutdown(void)
{
    ALSADrv_MIDI_HaltPlayback();
    
    if (seq_queue >= 0) {
        snd_seq_free_queue(seq, seq_queue);
    }
    if (seq_port >= 0) {
        snd_seq_delete_simple_port(seq, seq_port);
    }
    if (seq) {
        snd_seq_close(seq);
    }
    seq_queue = -1;
    seq_port = -1;
    seq = 0;
}
 MidiInputDeviceAlsa::MidiInputPortAlsa::~MidiInputPortAlsa() {
     UnsubscribeAll();
     snd_seq_delete_simple_port(pDevice->hAlsaSeq, portNumber);
 }
Beispiel #7
0
void samplv1_jack::close (void)
{
#ifdef CONFIG_ALSA_MIDI
    // close alsa sequencer client...
    if (m_alsa_seq) {
        if (m_alsa_thread) {
            delete m_alsa_thread;
            m_alsa_thread = NULL;
        }
        if (m_alsa_buffer) {
            ::jack_ringbuffer_free(m_alsa_buffer);
            m_alsa_buffer = NULL;
        }
        if (m_alsa_decoder) {
            snd_midi_event_free(m_alsa_decoder);
            m_alsa_decoder = NULL;
        }
        if (m_alsa_port >= 0) {
            snd_seq_delete_simple_port(m_alsa_seq, m_alsa_port);
            m_alsa_port = -1;
        }
        snd_seq_close(m_alsa_seq);
        //	m_alsa_client = -1;
        m_alsa_seq = NULL;
    }
#endif

    if (m_client == NULL)
        return;

#ifdef CONFIG_JACK_MIDI
    // unregister midi port
    if (m_midi_in) {
        ::jack_port_unregister(m_client, m_midi_in);
        m_midi_in = NULL;
    }
#endif

    // unregister audio ports
    const uint16_t nchannels = samplv1::channels();

    for (uint16_t k = 0; k < nchannels; ++k) {
        if (m_audio_outs && m_audio_outs[k]) {
            ::jack_port_unregister(m_client, m_audio_outs[k]);
            m_audio_outs[k] = NULL;
        }
        if (m_outs && m_outs[k])
            m_outs[k] = NULL;
        if (m_audio_ins && m_audio_ins[k]) {
            ::jack_port_unregister(m_client, m_audio_ins[k]);
            m_audio_ins[k] = NULL;
        }
        if (m_ins && m_ins[k])
            m_ins[k] = NULL;
    }

    if (m_outs) {
        delete [] m_outs;
        m_outs = NULL;
    }
    if (m_ins) {
        delete [] m_ins;
        m_ins = NULL;
    }

    if (m_audio_outs) {
        delete [] m_audio_outs;
        m_audio_outs = NULL;
    }
    if (m_audio_ins) {
        delete [] m_audio_ins;
        m_audio_ins = NULL;
    }

    // close client
    ::jack_client_close(m_client);
    m_client = NULL;
}
int main(int argc, char **argv)
{
	int i;
	int listen_port = -1;
	char *client_name = "net2alsamidi";
	char *connect_client = NULL;
	int connect_port = -1;

	for (i = 1; i < argc; i++)
	{
		if (strcmp(argv[i], "--port") == 0)
		{
			if (++i == argc) usage(argv[0]);
			listen_port = atoi(argv[i]);
		}
		else if (strcmp(argv[i], "--name") == 0)
		{
			if (++i == argc) usage(argv[0]);
			client_name = argv[i];
		}
		else if (strcmp(argv[i], "--connect") == 0)
		{
			if (++i == argc) usage(argv[0]);
			connect_client = argv[i];
			if (++i == argc) usage(argv[0]);
			connect_port = atoi(argv[i]);
		}
		else
		{
			usage(argv[0]);
		}
	}

	if (listen_port > 0)
	{
		snd_seq_t *seq;
		int port;

		if (snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0)
		{
			fprintf(stderr, "Cannot open the ALSA sequencer.\n");
			exit(1);
		}

		snd_seq_set_client_name(seq, client_name);
		port = snd_seq_create_simple_port(seq, "from NetMIDI client", SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);

		if ((connect_client != NULL) && (connect_port >= 0))
		{
			int connect_client_id = -1;

			{
				snd_seq_client_info_t *client_info;
				snd_seq_client_info_malloc(&client_info);

				while (snd_seq_query_next_client(seq, client_info) == 0)
				{
					if (strcmp(snd_seq_client_info_get_name(client_info), connect_client) == 0)
					{
						connect_client_id = snd_seq_client_info_get_client(client_info);
						break;
					}
				}

				snd_seq_client_info_free(client_info);
			}

			if (connect_client_id < 0) connect_client_id = atoi(connect_client);
			snd_seq_connect_to(seq, port, connect_client_id, connect_port);
		}

		{
			int server_socket;
			struct sockaddr_in server_address;

			if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
			{
				fprintf(stderr, "Cannot start a NetMIDI server on port %d.\n", listen_port);
				exit(1);
			}

			server_address.sin_family = AF_INET;
			server_address.sin_port = htons(listen_port);
			server_address.sin_addr.s_addr = INADDR_ANY;

			if (bind(server_socket, (struct sockaddr *)(&server_address), sizeof(server_address)) < 0)
			{
				fprintf(stderr, "Cannot start a NetMIDI server on port %d.\n", listen_port);
				exit(1);
			}

			if (listen(server_socket, 1) < 0)
			{
				fprintf(stderr, "Cannot start a NetMIDI server on port %d.\n", listen_port);
				exit(1);
			}

			while (1)
			{
				int socket_to_client;

				if ((socket_to_client = accept(server_socket, NULL, NULL)) >= 0)
				{
					snd_midi_event_t *midi_event_parser;
					snd_seq_event_t *event;
					unsigned char buffer[BUFFER_SIZE];
					int bytes_read;

					{
						char one = 1;
						setsockopt(socket_to_client, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
					}

					snd_midi_event_new(BUFFER_SIZE, &midi_event_parser);

					while ((bytes_read = recv(socket_to_client, buffer, BUFFER_SIZE, 0)) > 0)
					{
						for (i = 0; i < bytes_read; i++)
						{
							if (snd_midi_event_encode_byte(midi_event_parser, buffer[i], event) == 1)
							{
								snd_seq_event_output_direct(seq, event);
							}
						}
					}

					snd_midi_event_free(midi_event_parser);
					close(socket_to_client);
				}
			}

			close(server_socket);
		}

		snd_seq_delete_simple_port(seq, port);
		snd_seq_close(seq);
	}
	else
	{
		usage(argv[0]);
	}

	return 0;
}
void MidiAlsaSeq::applyPortMode( MidiPort * _port )
{
	m_seqMutex.lock();

	// determine port-capabilities
	unsigned int caps[2] = { 0, 0 };

	switch( _port->mode() )
	{
		case MidiPort::Duplex:
			caps[1] |= SND_SEQ_PORT_CAP_READ |
						SND_SEQ_PORT_CAP_SUBS_READ;

		case MidiPort::Input:
			caps[0] |= SND_SEQ_PORT_CAP_WRITE |
						SND_SEQ_PORT_CAP_SUBS_WRITE;
			break;

		case MidiPort::Output:
			caps[1] |= SND_SEQ_PORT_CAP_READ |
						SND_SEQ_PORT_CAP_SUBS_READ;
			break;

		default:
			break;
	}

	for( int i = 0; i < 2; ++i )
	{
		if( caps[i] != 0 )
		{
			// no port there yet?
			if( m_portIDs[_port][i] == -1 )
			{
				// then create one;
				m_portIDs[_port][i] =
						snd_seq_create_simple_port(
							m_seqHandle,
				_port->displayName().toUtf8().constData(),
							caps[i],
						SND_SEQ_PORT_TYPE_MIDI_GENERIC |
						SND_SEQ_PORT_TYPE_APPLICATION );
				continue;
			}
			snd_seq_port_info_t * port_info;
			snd_seq_port_info_malloc( &port_info );
			snd_seq_get_port_info( m_seqHandle, m_portIDs[_port][i],
							port_info );
			snd_seq_port_info_set_capability( port_info, caps[i] );
			snd_seq_set_port_info( m_seqHandle, m_portIDs[_port][i],
							port_info );
			snd_seq_port_info_free( port_info );
		}
		// still a port there although no caps? ( = dummy port)
		else if( m_portIDs[_port][i] != -1 )
		{
			// then remove this port
			snd_seq_delete_simple_port( m_seqHandle,
							m_portIDs[_port][i] );
			m_portIDs[_port][i] = -1;
		}
	}

	m_seqMutex.unlock();
}
Beispiel #10
0
void midi_deregister()
{
    snd_seq_delete_simple_port(midi_client, midi_out);
    snd_seq_delete_simple_port(midi_client, midi_in);
    snd_seq_close(midi_client);
}
Beispiel #11
0
static GstStateChangeReturn
gst_amidisrc_change_state (GstElement * element, GstStateChange transition )
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstaMIDISrc *src = GST_AMIDISRC (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
		 // TODO: Open our listen alsa ports
		//     1. Open sequencer
		//     2. If we have a client and port, connect to them
		printf("***Here: %s:%d\n",__FILE__,__LINE__);

		if ((snd_seq_open(&src->a_seq, src->device, SND_SEQ_OPEN_INPUT, 0)) < 0)
			return GST_STATE_CHANGE_FAILURE;
		if( src->a_seq == NULL )
			return GST_STATE_CHANGE_FAILURE;
		snd_seq_set_client_name(src->a_seq, "gstreamer");
		if ((src->a_port = snd_seq_create_simple_port(src->a_seq, "In",
						SND_SEQ_PORT_CAP_WRITE |
						SND_SEQ_PORT_CAP_SUBS_WRITE,
						SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0)
		{
			return GST_STATE_CHANGE_FAILURE;
		}
		src->a_queue = snd_seq_alloc_queue(src->a_seq);
		if( src->a_queue < 0 )
			return GST_STATE_CHANGE_FAILURE;
		// Only connect if we have positive client/port
		if( src->client >= 0 && src->port >= 0 )
		{
			if( snd_seq_connect_to(src->a_seq, src->a_port, src->client, src->port) < 0 )
				return GST_STATE_CHANGE_FAILURE;
		}
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
		return GST_STATE_CHANGE_NO_PREROLL;
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
		 // TODO: close our listening alsa ports
		 // disconnect from midi port
		printf("***Here: %s:%d\n",__FILE__,__LINE__);
		if( src->a_queue >= 0 ){
			if( snd_seq_free_queue( src->a_seq, src->a_queue ) < 0 )
				return GST_STATE_CHANGE_FAILURE;
		}
		if( src->a_port >= 0 && src->a_seq != NULL){
			if( snd_seq_delete_simple_port(src->a_seq,src->a_port) < 0 ){
				return GST_STATE_CHANGE_FAILURE;
			}
		}
		if( snd_seq_close( src->a_seq ) < 0 )
			return GST_STATE_CHANGE_FAILURE;
      break;
	 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
		return GST_STATE_CHANGE_NO_PREROLL;
		break;
    default:
      break;
  }

  return ret;
}