Exemplo n.º 1
0
void Lv2Worker::respond()
{
    uint32_t read_space = jack_ringbuffer_read_space(responseBuffer);
    while (read_space) {
        uint32_t size = 0;
        jack_ringbuffer_read(responseBuffer, (char*)&size, sizeof(size));
        jack_ringbuffer_read(responseBuffer, response, size);
        interface->work_response(handle, size, response);
        read_space -= sizeof(size) + size;
    }
}
Exemplo n.º 2
0
int jack_card_read(JackCard *obj,char *buf,int size)
{
  size_t bytes, can_read, i;
  int error;
  float norm, value;

  g_return_val_if_fail((obj->read.buffer!=NULL)&&(obj->read.src_state!=NULL),-1);
  if (jack_init(obj) != 0) return -1;
  size /= 2;
  can_read = MIN(size, obj->read.frames);
  //  can_read = MIN(((long)((double)can_read / obj->read.data.src_ratio))*sizeof(sample_t), jack_ringbuffer_read_space(obj->read.buffer));
  can_read = ((long)((double)can_read / obj->read.data.src_ratio))*sizeof(sample_t);
  obj->read.can_process = FALSE;
  bytes = jack_ringbuffer_read (obj->read.buffer, (void *)obj->read.data.data_in, can_read);
  obj->read.can_process = TRUE;
  obj->read.data.input_frames = bytes / sizeof(sample_t);
  can_read = MIN(size, obj->read.frames);
  obj->read.data.output_frames = can_read;
  if ((error = src_process(obj->read.src_state, &(obj->read.data))) != 0)
    g_warning("error while samplerate conversion. error: %s", src_strerror(error));
  norm = obj->read.level*obj->level*(float)0x8000;
  for (i=0; i < obj->read.data.output_frames_gen; i++) {
    value = obj->read.data.data_out[i]*norm;
    if (value >= 32767.0) 
      ((short*)buf)[i] = 32767;
    else if (value <= -32768.0)
      ((short*)buf)[i] = -32768;
    else
      ((short*)buf)[i] = (short)value;
  }
  bytes = obj->read.data.output_frames_gen * 2;
  return bytes;
}
Exemplo n.º 3
0
void *shout_thread(void *data) {
	STREAM_HDR *stream;
	size_t n;
	int ret;
	struct timespec nap;
	stream = (STREAM_HDR *)data;
	stream->streaming = 1;
	while (stream->streaming) {
		shout_sync(stream->shout);
		n = jack_ringbuffer_read_space(stream->ringbuf);
		if (n < 1) {
			nap.tv_sec = shout_wait.tv_sec;
			nap.tv_nsec = shout_wait.tv_nsec;
			while (nanosleep(&nap, &nap) != 0);
			continue;
			}
		if (n > stream->shoutbuf_size) {
			if (stream->shoutbuf != NULL) free(stream->shoutbuf);
			stream->shoutbuf = (char *)my_plain_malloc(n, "shout_thread");
			stream->shoutbuf_size = n;
			}
		jack_ringbuffer_read(stream->ringbuf, stream->shoutbuf, n);
		ret = shout_send(stream->shout,
				(const unsigned char *)stream->shoutbuf, n);
		if (ret != SHOUTERR_SUCCESS) {
			log_msg("send err: %s\n",
				shout_get_error(stream->shout));
			}
		}
	return NULL;
	}
Exemplo n.º 4
0
void
JackLayer::read(AudioBuffer &buffer)
{
    for (unsigned i = 0; i < in_ringbuffers_.size(); ++i) {

        const size_t incomingSamples = jack_ringbuffer_read_space(in_ringbuffers_[i]) / sizeof(captureFloatBuffer_[0]);
        if (!incomingSamples)
            continue;

        captureFloatBuffer_.resize(incomingSamples);
        buffer.resize(incomingSamples);

        // write to output
        const size_t from_ringbuffer = jack_ringbuffer_read_space(in_ringbuffers_[i]);
        const size_t expected_bytes = std::min(incomingSamples * sizeof(captureFloatBuffer_[0]), from_ringbuffer);
        // FIXME: while we have samples to write AND while we have space to write them
        const size_t read_bytes = jack_ringbuffer_read(in_ringbuffers_[i],
                (char *) captureFloatBuffer_.data(), expected_bytes);
        if (read_bytes < expected_bytes) {
            RING_WARN("Dropped %zu bytes", expected_bytes - read_bytes);
            break;
        }

        /* Write the data one frame at a time.  This is
         * inefficient, but makes things simpler. */
        // FIXME: this is braindead, we should write blocks of samples at a time
        // convert a vector of samples from 1 channel to a float vector
        convertFromFloat(captureFloatBuffer_, *buffer.getChannel(i));
    }
}
Exemplo n.º 5
0
/*
 * The process callback for this JACK application.
 * It is called by JACK at the appropriate times.
 * @nframes : 
 * @arg :
 */
int
process (jack_nframes_t nframes, void *arg)
{
  JackCard* obj = (JackCard*) arg;
  sample_t *out;
  sample_t *in;
  
  if (obj->clear && !obj->write.can_process) {
    out = (sample_t *) jack_port_get_buffer (obj->write.port, nframes);
    memset (out, 0, nframes * sizeof(sample_t));
    obj->clear = FALSE;
  }
  
  if (!obj->can_process)
    return 0;

  if(obj->read.can_process) {
    in = (sample_t *) jack_port_get_buffer (obj->read.port, nframes);
    jack_ringbuffer_write (obj->read.buffer, (void *) in, sizeof(sample_t) * nframes);
  }

  if (obj->write.can_process) {
    out = (sample_t *) jack_port_get_buffer (obj->write.port, nframes);
    memset (out, 0, nframes * sizeof(sample_t));
    if (obj->clear && jack_ringbuffer_read_space(obj->write.buffer) == 0) {
      obj->write.can_process = FALSE;
      if (!obj->read.open)
	obj->can_process = FALSE;
      obj->clear = FALSE;
      return 0;
    }
    jack_ringbuffer_read (obj->write.buffer, (void *) out, sizeof(sample_t) * nframes);
  }
  return 0;      
}
Exemplo n.º 6
0
/*
  mast_fill_input_buffer()
  Make sure input buffer if full of audio  
*/
size_t mast_fill_input_buffer( MastAudioBuffer* buffer )
{
	int frames_wanted = buffer->get_write_space();
	size_t bytes_wanted = frames_wanted * buffer->get_channels() * sizeof( mast_sample_t );
	size_t bytes_read = 0, frames_read = 0;



	// Keep checking that there is enough audio available
	while (jack_ringbuffer_read_space(g_ringbuffer) < bytes_wanted) {
		MAST_WARNING( "Not enough audio available in ringbuffer; waiting" );
		//MAST_DEBUG("Ring buffer is %u%% full", (jack_ringbuffer_read_space(g_ringbuffer)*100) / g_ringbuffer->size);

		// Wait for some more audio to become available
		pthread_mutex_lock(&g_ringbuffer_cond_mutex);
		pthread_cond_wait(&g_ringbuffer_cond, &g_ringbuffer_cond_mutex);
		pthread_mutex_unlock(&g_ringbuffer_cond_mutex);
	}

	// Copy frames from ring buffer to temporary buffer
	bytes_read = jack_ringbuffer_read(g_ringbuffer, (char*)buffer->get_write_ptr(), bytes_wanted);
	if (bytes_read<=0) MAST_FATAL( "Failed to read from ringbuffer" );
	if (bytes_read!=bytes_wanted) MAST_WARNING("Failed to read enough audio for a full packet");
	
	// Mark the space in the buffer as used
	frames_read = bytes_read / (buffer->get_channels() * sizeof( mast_sample_t ));
	buffer->add_frames( frames_read );

	// Return the number 
	return frames_read;
}
Exemplo n.º 7
0
void dsp_ev(void)
{
    while(jack_ringbuffer_read_space(dsp_ring) >= 2048) {
        jack_ringbuffer_read(dsp_ring, dsp_osc_buf, 2048);
        dsp_dispatch(dsp_osc_buf);
    }
}
Exemplo n.º 8
0
static void send_cb(EV_P_ ev_async *w, int revents) {
	jack_ringbuffer_read(ringbuf, framebuf, (buffer_samples * sizeof(jack_default_audio_sample_t)) );
	int rtmp_size = RTMP_MAX_HEADER_SIZE + 1 + buffer_samples * 5 / 4 + 7200;
	unsigned char *rtmp_buffer = malloc(rtmp_size);
	int mp3size = lame_encode_buffer_ieee_float(mp3, (const float *) framebuf, NULL, buffer_samples, rtmp_buffer + RTMP_MAX_HEADER_SIZE + 1, rtmp_size - RTMP_MAX_HEADER_SIZE - 1);
	if ( mp3size < 0 ) {
		fprintf(stderr, "mp3 encoding error %d . buffer_samples %d\n", mp3size, buffer_samples);
	} else {
		if (!start_time) {
			rtmp_w.m_headerType = RTMP_PACKET_SIZE_LARGE;
			rtmp_w.m_nInfoField2 = rtmp_o->m_stream_id;
			rtmp_w.m_nTimeStamp = 0;
			start_time = RTMP_GetTime();
		} else {
			rtmp_w.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
			rtmp_w.m_nTimeStamp = RTMP_GetTime()-start_time;
		}
		rtmp_w.m_nChannel = 0x04; // source channel
		rtmp_w.m_body = (char *) rtmp_buffer + RTMP_MAX_HEADER_SIZE;
		rtmp_w.m_nBodySize = mp3size + 1;
		rtmp_w.m_packetType = RTMP_PACKET_TYPE_AUDIO;
		rtmp_buffer[RTMP_MAX_HEADER_SIZE] = FLV_CODECID_MP3 | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_MONO;
		if ( !RTMP_SendPacket(rtmp_o, &rtmp_w, FALSE) ) { // queue учитывается только для INVOKE
			fprintf(stderr, "RTMP_SendPacket error\n");
		}
		//fprintf(stderr, "sent %d bytes of data. time: %d\n", (buffer_samples * (int)sizeof(jack_default_audio_sample_t)), rtmp_w.m_nTimeStamp );
	}
	free(rtmp_buffer);
}
Exemplo n.º 9
0
			/**
			  @brief Read into dest

			  Read from the buffer into a variable.

			  \param dest an item to be read into
			  */
			void read(Type &dest){
				if(getReadSpace() <= 0){
					//throw error!!!!
					return;
				}
				jack_ringbuffer_read(mRingBufferPtr, (char *)&dest, sizeof(Type));
			}
Exemplo n.º 10
0
static int jackrack_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
{
	// Get the filter service
	mlt_filter filter = mlt_frame_pop_audio( frame );

	// Get the filter properties
	mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );

	int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );

	// Get the producer's audio
	*format = mlt_audio_float;
	mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
	
	// TODO: Deal with sample rate differences
	if ( *frequency != jack_frequency )
		mlt_log_error( MLT_FILTER_SERVICE( filter ), "mismatching frequencies JACK = %d actual = %d\n",
			jack_frequency, *frequency );
	*frequency = jack_frequency;

	// Initialise Jack ports and connections if needed
	if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
		mlt_properties_set_int( filter_properties, "_samples", *samples );
	
	// Get the filter-specific properties
	jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
	jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
//	pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
//	pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
	
	// Process the audio
	float *q = (float*) *buffer;
	size_t size = *samples * sizeof(float);
	int j;
//	struct timespec tm = { 0, 0 };

	// Write into output ringbuffer
	for ( j = 0; j < *channels; j++ )
	{
		if ( jack_ringbuffer_write_space( output_buffers[j] ) >= size )
			jack_ringbuffer_write( output_buffers[j], (char*)( q + j * *samples ), size );
	}

	// Synchronization phase - wait for signal from Jack process
	while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < size ) ;
		//pthread_cond_wait( output_ready, output_lock );
		
	// Read from input ringbuffer
	for ( j = 0; j < *channels; j++, q++ )
	{
		if ( jack_ringbuffer_read_space( input_buffers[j] ) >= size )
			jack_ringbuffer_read( input_buffers[j], (char*)( q + j * *samples ), size );
	}

	// help jack_sync() indicate when we are rolling
	mlt_position pos = mlt_frame_get_position( frame );
	mlt_properties_set_position( filter_properties, "_last_pos", pos );

	return 0;
}
Exemplo n.º 11
0
void gui_ev(void)
{
    while(jack_ringbuffer_read_space(gui_ring) >= 2048) {
        jack_ringbuffer_read(gui_ring, gui_osc_buf, 2048);
        gui_dispatch(gui_osc_buf);
    }
}
Exemplo n.º 12
0
static size_t rotter_read_from_ringbuffer(rotter_ringbuffer_t *ringbuffer, size_t desired_frames)
{
  size_t desired_bytes = desired_frames * sizeof(jack_default_audio_sample_t);
  size_t available_bytes = 0;
  int c, bytes_read = 0;

  // Is there enough in the ring buffers?
  for (c=0; c<channels; c++) {
    available_bytes = jack_ringbuffer_read_space( ringbuffer->buffer[c] );
    if (available_bytes <= 0) {
      // Try again later
      return 0;
    }
  }

  if (available_bytes > desired_bytes)
    available_bytes = desired_bytes;

  // Get the audio out of the ring buffer
  for (c=0; c<channels; c++) {
    // Copy frames from ring buffer to temporary buffer
    bytes_read = jack_ringbuffer_read( ringbuffer->buffer[c], (char*)tmp_buffer[c], available_bytes);
    if (bytes_read != available_bytes) {
      rotter_fatal( "Failed to read from ringbuffer %c channel %d.", ringbuffer->label, c);
      return 0;
    }
  }

  return bytes_read / sizeof(jack_default_audio_sample_t);
}
Exemplo n.º 13
0
void *
detect_note(void *arg)
{
	jack_thread_info_t *info = (jack_thread_info_t *)arg;
	jack_nframes_t samples_per_frame = info->channels;
	size_t bytes_per_frame = samples_per_frame * sample_size, bytes;
	gboolean done = FALSE;
  float sample, *frame;
	int i;

	frame = (float *)alloca(bytes_per_frame);

	pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	pthread_mutex_lock(&detect_lock);
	info->status = 0;

	while (!done) {	
		while (info->can_capture &&
		      jack_ringbuffer_read_space(rb) >= bytes_per_frame) {

			jack_ringbuffer_read(rb, frame, bytes_per_frame);

			fftMeasure(1, bytes_per_frame, frame);
		}
		//wait for data
		pthread_cond_wait (&data_ready, &detect_lock);
	}

  pthread_mutex_unlock(&detect_lock);
}
Exemplo n.º 14
0
/** take a single block from the ringbuffers and send it out the
 *  attached track's ports */
nframes_t
Playback_DS::process ( nframes_t nframes )
{
    THREAD_ASSERT( RT );


    const size_t block_size = nframes * sizeof( sample_t );

//    printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );

    for ( int i = channels(); i--;  )
    {

        void *buf = track()->output[ i ].buffer( nframes );

        if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
        {
            ++_xruns;
            memset( buf, 0, block_size );
            /* FIXME: we need to resync somehow */
        }

        /* TODO: figure out a way to stop IO while muted without losing sync */
        if ( track()->mute() || ( Track::soloing() && ! track()->solo() ) )
            buffer_fill_with_silence( (sample_t*)buf, nframes );
    }

    block_processed();

    /* FIXME: bogus */
    return nframes;
}
Exemplo n.º 15
0
static gavl_source_status_t
read_func_jack(void * p, gavl_audio_frame_t ** frame)
  {
  int i;
  
  int samples_read, result;

  gavl_audio_frame_t * f = *frame;
  jack_t * priv = p;
  
  for(i = 0; i < priv->format.num_channels; i++)
    {
    samples_read = 0;
    while(samples_read < priv->format.samples_per_frame)
      {
      result = jack_ringbuffer_read(priv->ports[i].buffer,
                                    (char*)(f->channels.f[i] + samples_read),
                                    (priv->format.samples_per_frame -
                                     samples_read) * sizeof(float));
      samples_read += result / sizeof(float);
      
      if(samples_read < priv->format.num_channels)
        {
        gavl_time_t delay_time;
        delay_time = gavl_time_unscale(priv->format.samplerate,
                                       priv->format.samples_per_frame -
                                       samples_read);
        gavl_time_delay(&delay_time);
        }
      }
    }

  f->valid_samples = samples_read;
  return GAVL_SOURCE_OK;
  }
Exemplo n.º 16
0
static int process_cb(jack_nframes_t nframes, void *data)
{
   jack_t *jd = (jack_t*)data;
   if (nframes <= 0)
   {
      pthread_cond_signal(&jd->cond);
      return 0;
   }

   jack_nframes_t avail[2];
   avail[0] = jack_ringbuffer_read_space(jd->buffer[0]);
   avail[1] = jack_ringbuffer_read_space(jd->buffer[1]);
   jack_nframes_t min_avail = ((avail[0] < avail[1]) ? avail[0] : avail[1]) / sizeof(jack_default_audio_sample_t);

   if (min_avail > nframes)
      min_avail = nframes;

   for (int i = 0; i < 2; i++)
   {
      jack_default_audio_sample_t *out = (jack_default_audio_sample_t*)jack_port_get_buffer(jd->ports[i], nframes);
      assert(out);
      jack_ringbuffer_read(jd->buffer[i], (char*)out, min_avail * sizeof(jack_default_audio_sample_t));

      for (jack_nframes_t f = min_avail; f < nframes; f++)
      {
         out[f] = 0.0f;
      }
   }
   pthread_cond_signal(&jd->cond);
   return 0;
}
Exemplo n.º 17
0
static int process_jack(jack_nframes_t nframes, void* arg)
{
	int cnt;

	if (!ready) {
		return 0;
	}

	for(int i = 0; i < CHANNELS; i++) {
		out[i] = jack_port_get_buffer(outputports[i], nframes);
	}

	for(size_t i = 0; i < nframes; i++) {
		
		if (jack_ringbuffer_read_space(ringbuffer) >= SAMPLE_SIZE * CHANNELS) {

			for(int j = 0; j < CHANNELS; j++){
				jack_ringbuffer_read (ringbuffer, (char*)(out[j]+i), SAMPLE_SIZE);
			}

		} else {
			printf ("underrun\n");
			ready = 0;

			return 0;
		}
	}

	return 0;
}
Exemplo n.º 18
0
static int process_cb(jack_nframes_t nframes, void *data)
{
   int i;
   jack_nframes_t f, avail[2], min_avail;
   jack_t *jd = (jack_t*)data;

   if (nframes <= 0)
   {
#ifdef HAVE_THREADS
      scond_signal(jd->cond);
#endif
      return 0;
   }

   avail[0] = jack_ringbuffer_read_space(jd->buffer[0]);
   avail[1] = jack_ringbuffer_read_space(jd->buffer[1]);
   min_avail = ((avail[0] < avail[1]) ? avail[0] : avail[1]) / sizeof(jack_default_audio_sample_t);

   if (min_avail > nframes)
      min_avail = nframes;

   for (i = 0; i < 2; i++)
   {
      jack_default_audio_sample_t *out = (jack_default_audio_sample_t*)jack_port_get_buffer(jd->ports[i], nframes);
      assert(out);
      jack_ringbuffer_read(jd->buffer[i], (char*)out, min_avail * sizeof(jack_default_audio_sample_t));

      for (f = min_avail; f < nframes; f++)
         out[f] = 0.0f;
   }
#ifdef HAVE_THREADS
   scond_signal(jd->cond);
#endif
   return 0;
}
Exemplo n.º 19
0
void a2j_add_ports (struct a2j_stream * str)
{
	struct a2j_port * port_ptr;
	while (jack_ringbuffer_read (str->new_ports, (char *)&port_ptr, sizeof(port_ptr))) {
		a2j_debug("jack: inserted port %s", port_ptr->name);
		a2j_port_insert (str->port_hash, port_ptr);
	}
}
Exemplo n.º 20
0
int jack_audio_callback(jack_nframes_t nframes, void *arg) {
	int c,s;
	WfAudioInfo* nfo = &myplayer->info;

#ifdef JACK_MIDI
	int n;
	void *jack_buf = jack_port_get_buffer(jack_midi_port, nframes);
	int nevents = jack_midi_get_event_count(jack_buf);
	for (n=0; n<nevents; n++) {
		jack_midi_event_t ev;
		jack_midi_event_get(&ev, jack_buf, n);

		if (ev.size < 3 || ev.size > 3) continue; // filter note on/off
		else {
			event_queue[queued_events_end].time = ev.time;
			event_queue[queued_events_end].size = ev.size;
			memcpy (event_queue[queued_events_end].buffer, ev.buffer, ev.size);
			queued_events_end = (queued_events_end +1 ) % JACK_MIDI_QUEUE_SIZE;
		}
	}
	if (queued_events_start != queued_events_end) {
		/* Tell the midi thread there is work to do. */
		if(pthread_mutex_trylock(&midi_thread_lock) == 0) {
			pthread_cond_signal(&midi_ready);
			pthread_mutex_unlock(&midi_thread_lock);
		}
	}
#endif

	if (!player_active) return (0);

	for(c=0; c<nfo->channels; c++) {
		j_out[c] = (jack_default_audio_sample_t*) jack_port_get_buffer(j_output_port[c], nframes);
	}

	if(playpause || jack_ringbuffer_read_space(rb) < nfo->channels * nframes * sizeof(jack_default_audio_sample_t)) {
		silent = 1;
		for(c=0; c< nfo->channels; c++) {
			memset(j_out[c], 0, nframes * sizeof(jack_default_audio_sample_t));
		}
	} else {
		silent=0;
		/* de-interleave */
		for(s=0; s<nframes; s++) {
			for(c=0; c< nfo->channels; c++) {
				jack_ringbuffer_read(rb, (char*) &j_out[c][s], sizeof(jack_default_audio_sample_t));
			}
		}
	}

	/* Tell the player thread there is work to do. */
	if(pthread_mutex_trylock(&player_thread_lock) == 0) {
		pthread_cond_signal(&buffer_ready);
		pthread_mutex_unlock(&player_thread_lock);
	}

	return(0);
};
Exemplo n.º 21
0
int ConsumeAudioFromBuffer(audioBuffer_t *buffer, int channel, uint8_t *data_out, int data_size) {
	if(buffer != NULL) {
		jack_ringbuffer_read(buffer->to_audio_buffers[channel], (char*) data_out, data_size);	
		normalbuffer--;
		printf("NORMAL BUFFER: %d\n",normalbuffer);
		return data_size;
	}
	return -1;
}
Exemplo n.º 22
0
static
void add_ports(stream_t *str)
{
	port_t *port;
	while (jack_ringbuffer_read(str->new_ports, (char*)&port, sizeof(port))) {
		debug_log("jack: inserted port %s\n", port->name);
		port_insert(str->ports, port);
	}
}
Exemplo n.º 23
0
int ConsumeSyncFromBuffer(audioBuffer_t *buffer, int channel, uint8_t *data_out, int data_size) {
	if(buffer != NULL) {
		jack_ringbuffer_read(buffer->sync_buffers[channel], (char*) data_out, data_size);
		syncbuffer--;
		printf("SYNC BUFFER: %d\n",syncbuffer);
		return data_size;
	}
	return -1;
}
Exemplo n.º 24
0
static
void free_ports(alsa_seqmidi_t *self, jack_ringbuffer_t *ports)
{
	port_t *port;
	int sz;
	while ((sz = jack_ringbuffer_read(ports, (char*)&port, sizeof(port)))) {
		assert (sz == sizeof(port));
		port_free(self, port);
	}
}
Exemplo n.º 25
0
void
jack_ringbuffer_read_exactly ( jack_ringbuffer_t *r , char *buf , int n )
{
  int err = jack_ringbuffer_read ( r , buf , n ) ;
  if ( err != n ) {
    eprintf ( "%s: error reading ring buffer (%d != %d)\n" , 
	      __func__ , err , n ) ;
    FAILURE ;
  }
}
Exemplo n.º 26
0
int jackprocess(jack_nframes_t nframes,void *arg){
    jack_default_audio_sample_t *outl=(jack_default_audio_sample_t *) jack_port_get_buffer (outport_left, nframes);
    jack_default_audio_sample_t *outr=(jack_default_audio_sample_t *) jack_port_get_buffer (outport_right, nframes);

    int datasize=nframes*sizeof (REALTYPE);
    int incoming_datasize=SOUND_BUFFER_SIZE*sizeof (REALTYPE);
    int data_read=0;


    if (jack_ringbuffer_read_space(rb)>=(2*incoming_datasize)){
	if (datasize>incoming_datasize){
	    data_read=0;
	    while (data_read < datasize){
		jack_ringbuffer_read(rb, (char *) outl+data_read,datasize);
		jack_ringbuffer_read(rb, (char *) outr+data_read,datasize);
		data_read+=incoming_datasize;
	    };
	} else if (datasize==incoming_datasize){
		jack_ringbuffer_read(rb, (char *) outl,datasize);
		jack_ringbuffer_read(rb, (char *) outr,datasize);
	} else {
	};
    } else {//the ringbuffer is empty or there are too small amount of samples in it
	for (int i=0;i<nframes;i++){
	    outl[i]=0.0;outr[i]=0.0;
	}; 
    };
/*    if (jack_ringbuffer_read_space(rb)>=datasize){
	jack_ringbuffer_read(rb, (char *) outl,datasize);
	jack_ringbuffer_read(rb, (char *) outr,datasize);
    } else {//the ringbuffer is empty or there are too small amount of samples in it
	for (int i=0;i<nframes;i++){
	    outl[i]=0.0;outr[i]=0.0;
	};
    };
*/    
    if (pthread_mutex_trylock(&zyn_thread_lock)==0){
	pthread_cond_signal(&more_data);
	pthread_mutex_unlock(&zyn_thread_lock);
    };
    
    return(0);
};
Exemplo n.º 27
0
/*------------------------------------------------------------------------------
 *  Read from the audio source
 *----------------------------------------------------------------------------*/
unsigned int
JackDspSource :: read (   void          * buf,
                          unsigned int    len )     throw ( Exception )
{
    jack_nframes_t samples         = len / 2 / getChannel();
    jack_nframes_t samples_read[2] = {0,0};
    short        * output          = (short*)buf;
    unsigned int c, n;

    if ( !isOpen() ) {
        return 0;
    }


    // Ensure the temporary buffer is big enough
    tmp_buffer = (jack_default_audio_sample_t*)realloc(tmp_buffer,
                             samples * sizeof( jack_default_audio_sample_t ) );
    if (!tmp_buffer) {
        throw Exception( __FILE__, __LINE__, "realloc on tmp_buffer failed");
    }


    for (c=0; c<getChannel(); c++)
    {    
        // Copy frames from ring buffer to temporary buffer
        // and then convert samples to output buffer
        int bytes_read = jack_ringbuffer_read(rb[c],
                                             (char*)tmp_buffer,
                              samples * sizeof( jack_default_audio_sample_t ));
        samples_read[c] = bytes_read / sizeof( jack_default_audio_sample_t );
        

        // Convert samples from float to short and put in output buffer
        for(n=0; n<samples_read[c]; n++) {
            int tmp = lrintf(tmp_buffer[n] * 32768.0f);
            if (tmp > SHRT_MAX) {
                output[n*getChannel()+c] = SHRT_MAX;
            } else if (tmp < SHRT_MIN) {
                output[n*getChannel()+c] = SHRT_MIN;
            } else {
                output[n*getChannel()+c] = (short) tmp;
            }
        }
    }

    // Didn't get as many samples as we wanted ?
    if (getChannel() == 2 && samples_read[0] != samples_read[1]) {
        Reporter::reportEvent( 2,
                              "Warning: Read a different number of samples "
                              "for left and right channels");
    }

    // Return the number of bytes put in the output buffer
    return samples_read[0] * 2 * getChannel();
}
Exemplo n.º 28
0
/*
 * ============================ Input ==============================
 */
static
void do_jack_input(alsa_seqmidi_t *self, port_t *port, struct process_info *info)
{
	// process port->early_events
	alsa_midi_event_t ev;
	while (jack_ringbuffer_read(port->early_events, (char*)&ev, sizeof(ev))) {
		jack_midi_data_t* buf;
		int64_t time = ev.time - info->period_start;
		if (time < 0)
			time = 0;
		else if (time >= info->nframes)
			time = info->nframes - 1;
		buf = jack_midi_event_reserve(port->jack_buf, (jack_nframes_t)time, ev.size);
		if (buf)
			jack_ringbuffer_read(port->early_events, (char*)buf, ev.size);
		else
			jack_ringbuffer_read_advance(port->early_events, ev.size);
		debug_log("input: it's time for %d bytes at %lld", ev.size, time);
	}
}
Exemplo n.º 29
0
void cbox_jackio_poll_ports(struct cbox_io_impl *impl, struct cbox_command_target *fb)
{
    struct cbox_jack_io_impl *jii = (struct cbox_jack_io_impl *)impl;

    while (jack_ringbuffer_read_space(jii->rb_autoconnect) >= sizeof(jack_port_t *))
    {
        jack_port_t *portobj;
        jack_ringbuffer_read(jii->rb_autoconnect, (char *)&portobj, sizeof(portobj));
        port_autoconnect(jii, portobj, fb);
    }
}
Exemplo n.º 30
0
static
void jack_add_ports(midi_stream_t *str)
{
    midi_port_t *port;
    while (can_pass(sizeof(port), str->jack.new_ports, str->midi.new_ports) && str->jack.nports < MAX_PORTS) {
        jack_ringbuffer_read(str->jack.new_ports, (char*)&port, sizeof(port));
        str->jack.ports[str->jack.nports++] = port;
        port->state = PORT_ADDED_TO_MIDI;
        jack_ringbuffer_write(str->midi.new_ports, (char*)&port, sizeof(port));
    }
}