Esempio n. 1
0
void
createPorts(jack_client_t *client, std::vector<jack_port_t *> &ports,
            bool playback, std::vector<jack_ringbuffer_t *> &ringbuffers)
{

    const char **physical_ports = jack_get_ports(client, NULL, NULL,
            playback ? JackPortIsInput : JackPortIsOutput | JackPortIsPhysical);
    for (unsigned i = 0; physical_ports[i]; ++i) {
        char port_name[32] = {0};
        if (playback)
            snprintf(port_name, sizeof(port_name), "out_%d", i + 1);
        else
            snprintf(port_name, sizeof(port_name), "in_%d", i + 1);
        port_name[sizeof(port_name) - 1] = '\0';
        jack_port_t *port = jack_port_register(client,
                port_name, JACK_DEFAULT_AUDIO_TYPE, playback ? JackPortIsOutput : JackPortIsInput, 0);
        if (port == nullptr)
            throw std::runtime_error("Could not register JACK output port");
        ports.push_back(port);

        static const unsigned RB_SIZE = 16384;
        jack_ringbuffer_t *rb = jack_ringbuffer_create(RB_SIZE);
        if (rb == nullptr)
            throw std::runtime_error("Could not create JACK ringbuffer");
        if (jack_ringbuffer_mlock(rb))
            throw std::runtime_error("Could not lock JACK ringbuffer in memory");
        ringbuffers.push_back(rb);
    }
    free(physical_ports);
}
Esempio n. 2
0
			/**
			  @brief The Constructor
			  \param size the number of items that the ring buffer should be able to hold
			  \param mlock a boolean indicating whether or not the ring buffer should be locked in memory
			  */
			RingBuffer(size_t size, bool mlock = false){
				mLength = size;
				mRingBufferPtr = jack_ringbuffer_create(mLength * sizeof(Type));

				//should we lock the memory for the ring buffer?
				if(mlock)
					jack_ringbuffer_mlock(mRingBufferPtr);
			}
Esempio n. 3
0
jack_client_t* init_jack(void)
{
	jack_client_t *client;
	const char *client_name = "simple_talker";
	const char *server_name = NULL;
	jack_options_t options = JackNullOption;
	jack_status_t status;

	client = jack_client_open (client_name, options, &status, server_name);

	if (NULL == client) {
		fprintf (stderr, "jack_client_open() failed\n ");
		exit (1);
	}
	if (status & JackServerStarted) {
		fprintf (stderr, "JACK server started\n");
	}
	if (status & JackNameNotUnique) {
		client_name = jack_get_client_name(client);
		fprintf (stderr, "unique name `%s' assigned\n", client_name);
	}

	jack_set_process_callback(client, process, 0);
	jack_on_shutdown(client, jack_shutdown, 0);

	if (jack_activate (client))
		fprintf (stderr, "cannot activate client");

	inputports = (jack_port_t**) malloc (CHANNELS * sizeof (jack_port_t*));
	in = (jack_default_audio_sample_t**) malloc (CHANNELS * sizeof (jack_default_audio_sample_t*));
	ringbuffer = jack_ringbuffer_create (SAMPLE_SIZE * DEFAULT_RINGBUFFER_SIZE * CHANNELS);
	jack_ringbuffer_mlock(ringbuffer);

	memset(in, 0, sizeof (jack_default_audio_sample_t*)*CHANNELS);
	memset(ringbuffer->buf, 0, ringbuffer->size);

	for(int i = 0; i < CHANNELS; i++)
	{
		char* portName;
		if (asprintf(&portName, "input%d", i) < 0) 
		{
			fprintf(stderr, "Could not create portname for port %d", i);
			exit(1);
		}	
		
		inputports[i] = jack_port_register (client, portName,
				JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
		if (NULL == inputports[i]) 
		{
			fprintf (stderr, "cannot register input port \"%d\"!\n", i);
			jack_client_close (client);
			exit (1);
		}
	}

	return client;
}
Esempio n. 4
0
jack_ringbuffer_t* ringbuffer_new(size_t sz, int mlock)
/* Create a ringbuffer. mlock!=0: lock the buffer in memory. */
	{
	jack_ringbuffer_t *rbuf;
	if((rbuf = jack_ringbuffer_create(sz)) == NULL)
		return NULL;
	if(mlock)
		jack_ringbuffer_mlock(rbuf);
	return rbuf;
	}
Esempio n. 5
0
void start_synth(void)
{
    //Setup Ringbuffers
    gui_ring = jack_ringbuffer_create(2048*8);
    dsp_ring = jack_ringbuffer_create(2048*32);
    jack_ringbuffer_mlock(gui_ring);
    jack_ringbuffer_mlock(dsp_ring);
    
    jclient = jack_client_open("rtosc-demo", JackNullOption, NULL, NULL);
    if(!jclient)
        errx(1, "jack_client_open() failure");

    jack_set_process_callback(jclient, process, NULL);

    jport = jack_port_register(jclient, "output",
            JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);

    if(!jport)
        errx(1, "jack_port_register() failure");

    if(jack_activate(jclient))
        errx(1, "jack_activate() failure");
}
Esempio n. 6
0
static int init_ringbuffers()
{
  size_t ringbuffer_size = 0;
  int b,c;

  ringbuffer_size = jack_get_sample_rate( client ) * rb_duration * sizeof(jack_default_audio_sample_t);
  rotter_debug("Size of the ring buffers is %2.2f seconds (%d bytes).", rb_duration, (int)ringbuffer_size );

  for(b=0; b<2; b++) {
    char label = ('A' + b);
    ringbuffers[b] = malloc(sizeof(rotter_ringbuffer_t));
    if (!ringbuffers[b]) {
      rotter_fatal("Cannot allocate memory for ringbuffer %c structure.", label);
      return -1;
    }

    if (mlock(ringbuffers[b], sizeof(rotter_ringbuffer_t))) {
      rotter_error("Failed to lock data structure for ringbuffer %c into physical memory.", label);
    }

    ringbuffers[b]->label = label;
    ringbuffers[b]->period_start = 0;
    ringbuffers[b]->file_handle = NULL;
    ringbuffers[b]->overflow = 0;
    ringbuffers[b]->xrun_usecs = 0;
    ringbuffers[b]->close_file = 0;
    ringbuffers[b]->buffer[0] = NULL;
    ringbuffers[b]->buffer[1] = NULL;

    for(c=0; c<channels; c++) {
      ringbuffers[b]->buffer[c] = jack_ringbuffer_create( ringbuffer_size );
      if (!ringbuffers[b]->buffer[c]) {
        rotter_fatal("Cannot create ringbuffer buffer %c%d.", label, c);
        return -1;
      }

      // Lock into physical memory to avoid delays during the realtime callback
      if (jack_ringbuffer_mlock(ringbuffers[b]->buffer[c])) {
        rotter_error("Failed to lock JACK ringbuffer %c%d into physical memory.", label, c);
      }
    }
  }

  return 0;
}
Esempio n. 7
0
JACKAudioSource::JACKAudioSource(UINT inputSamplesPerSec)
{
	bool  bFloat = true;
	UINT  inputChannels = 2;
	UINT  inputBitsPerSample = sizeof(float)*8;
	UINT  inputBlockSize = sizeof(float) * inputChannels;

	sampleFrameCount   = inputSamplesPerSec/100;
	sampleSegmentSize  = inputBlockSize*sampleFrameCount;
	outputBuffer.SetSize(sampleSegmentSize);

	sampleBuffer = jack_ringbuffer_create(sampleSegmentSize * 5);

	assert(!jack_ringbuffer_mlock(sampleBuffer));

	InitAudioData(bFloat, inputChannels, inputSamplesPerSec, inputBitsPerSample, inputBlockSize, 0);

	API->AddAudioSource(this);
}
Esempio n. 8
0
jack_client_t* init_jack(void)
{
	const char* client_name = "simple_listener";
	const char* server_name = NULL;
	jack_options_t options = JackNullOption;
	jack_status_t status;

	client = jack_client_open (client_name, options, &status, server_name);

	if (NULL == client) {
		fprintf (stderr, "jack_client_open() failed\n ");
		shutdown_all(0);
		exit (1);
	}

	if (status & JackServerStarted) {
		fprintf (stderr, "JACK server started\n");
	}

	if (status & JackNameNotUnique) {
		client_name = jack_get_client_name(client);
		fprintf (stderr, "unique name `%s' assigned\n", client_name);
	}

	jack_set_process_callback(client, process_jack, 0);
	jack_on_shutdown(client, jack_shutdown, 0);

	outputports = (jack_port_t**) malloc (CHANNELS * sizeof (jack_port_t*));
	out = (jack_default_audio_sample_t**) malloc (CHANNELS * sizeof (jack_default_audio_sample_t*));
	ringbuffer = jack_ringbuffer_create (SAMPLE_SIZE * DEFAULT_RINGBUFFER_SIZE * CHANNELS);
	jack_ringbuffer_mlock(ringbuffer);

	memset(out, 0, sizeof (jack_default_audio_sample_t*)*CHANNELS);
	memset(ringbuffer->buf, 0, ringbuffer->size);

	for(int i = 0; i < CHANNELS; i++) {
		
		char* portName;
		if (asprintf(&portName, "output%d", i) < 0) {
			fprintf(stderr, "could not create portname for port %d\n", i);
			shutdown_all(0);
			exit(1);
		}	
		
		outputports[i] = jack_port_register (client, portName, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
		if (NULL == outputports[i]) {
			fprintf (stderr, "cannot register output port \"%d\"!\n", i);
			shutdown_all(0);
			exit (1);
		}
	}

	const char** ports;
	if (jack_activate (client)) {
		fprintf (stderr, "cannot activate client\n");		
		shutdown_all(0);
		exit(1);
	}

	ports = jack_get_ports(client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
	if(NULL == ports) { 
		fprintf (stderr, "no physical playback ports\n");		
		shutdown_all(0);
		exit(1);
	}

	int i = 0;
	while(i < CHANNELS && NULL != ports[i]) {
		if (jack_connect(client, jack_port_name(outputports[i]), ports[i]))
			fprintf (stderr, "cannot connect output ports\n");
		i++;
	}

	free(ports);
}
Esempio n. 9
0
/* JACK player control */
void
JACKaudiooutputinit(WfDecoder* d)
{
	int i;

	if(!j_client) JACKconnect(NULL);
	if(!j_client) return;

	if(thread_run || rb) {
		dbg(0, "already playing.");
		return;
	}

	myplayer = d;
	int channels = d->info.channels;
	int samplerate = d->info.sample_rate;
	m_frames = d->info.frames;
	m_samplerate = samplerate;
	playpause = silent = 0;
	play_position = 0;

	j_output_port = (jack_port_t**) calloc(channels, sizeof(jack_port_t*));

	for(i=0;i<channels;i++) {
		char channelid[16];
		snprintf(channelid, 16, "output_%i", i);
		j_output_port[i] = jack_port_register(j_client, channelid, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
		if(!j_output_port[i]) {
			dbg(0, "no more jack ports availabe.");
			JACKclose();
			return;
		}
	}

	myplugin = malloc(channels*sizeof(LadSpa*));
	for(i=0;i<channels;i++) {
		myplugin[i] = ladspah_alloc();
	}

	j_out = (jack_default_audio_sample_t**) calloc(channels, sizeof(jack_default_audio_sample_t*));
	const size_t rbsize = DEFAULT_RB_SIZE * channels * sizeof(jack_default_audio_sample_t);
	rb = jack_ringbuffer_create(rbsize);
	jack_ringbuffer_mlock(rb);
	memset(rb->buf, 0, rbsize);

	jack_nframes_t jsr = jack_get_sample_rate(j_client);

#ifdef ENABLE_RESAMPLING
	m_fResampleRatio = 1.0;
#endif
	if(jsr != samplerate) {
#ifdef ENABLE_RESAMPLING
		m_fResampleRatio = (float) jsr / (float) samplerate;
		dbg(2, "resampling %d -> %d (f:%.2f).", samplerate, jsr, m_fResampleRatio);
#else
		dbg(0, "audio samplerate does not match JACK's samplerate.");
#endif
	}

#if (defined ENABLE_RESAMPLING) && !(defined VARISPEED)
	m_fResampleRatio *= app->playback_speed; ///< fixed speed change. <1.0: speed-up, > 1.0: slow-down
#endif

	thread_run = 1;
	pthread_create(&player_thread_id, NULL, jack_player_thread, NULL);
	sched_yield();

#if 1
	char *jack_autoconnect = play->config.jack_autoconnect;
	if(!jack_autoconnect || strlen(jack_autoconnect)<1) {
		jack_autoconnect = (char*) "system:playback_";
	} else if(!strncmp(jack_autoconnect,"DISABLE", 7)) {
		jack_autoconnect = NULL;
	}
	if(jack_autoconnect) {
		int myc=0;
		dbg(1, "JACK connect to '%s'", jack_autoconnect);
		const char **found_ports = jack_get_ports(j_client, jack_autoconnect, NULL, JackPortIsInput);
		for(i = 0; found_ports && found_ports[i]; i++) {
			if(jack_connect(j_client, jack_port_name(j_output_port[myc]), found_ports[i])) {
				dbg(0, "cannot connect to jack output");
			}
			if(myc >= channels) break;
		}
	}
#endif
}