static int coreaudio_driver_write (coreaudio_driver_t *driver, jack_nframes_t nframes) { jack_default_audio_sample_t *buf; channel_t chn; jack_port_t *port; JSList *node; for (chn = 0, node = driver->playback_ports; node; node = jack_slist_next (node), chn++) { port = (jack_port_t *)node->data; if (driver->outcoreaudio[chn] != NULL) { float *out = driver->outcoreaudio[chn]; if (jack_port_connected (port)) { buf = jack_port_get_buffer (port, nframes); memcpy(out,buf,sizeof(float)*nframes); } else { memset(out,0,sizeof(float)*nframes); } } } return 0; }
static int portaudio_driver_read (portaudio_driver_t *driver, jack_nframes_t nframes) { jack_default_audio_sample_t *buf; channel_t chn; jack_port_t *port; JSList *node; int i; for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) { port = (jack_port_t *)node->data; if (jack_port_connected (port) && (driver->inPortAudio != NULL)) { int channels = driver->capture_nchannels; float* in = driver->inPortAudio; buf = jack_port_get_buffer (port, nframes); for (i = 0; i< nframes; i++) buf[i] = in[channels*i+chn]; } } driver->engine->transport_cycle_start (driver->engine, driver->engine->get_microseconds ()); return 0; }
static int portaudio_driver_write (portaudio_driver_t *driver, jack_nframes_t nframes) { jack_default_audio_sample_t *buf; channel_t chn; jack_port_t *port; JSList *node; int i,bytes = nframes*sizeof(float); /* Clear in case of nothing is connected */ memset(driver->outPortAudio, 0, driver->playback_nchannels*bytes); for (chn = 0, node = driver->playback_ports; node; node = jack_slist_next (node), chn++) { port = (jack_port_t *)node->data; if (jack_port_connected (port) && (driver->outPortAudio != NULL)) { int channels = driver->playback_nchannels; float* out = driver->outPortAudio; buf = jack_port_get_buffer (port, nframes); for (i = 0; i< nframes; i++) out[channels*i+chn] = buf[i]; } } return 0; }
unsigned int JackCpp::AudioIO::numConnectionsOutPort(unsigned int index) throw(std::range_error) { if(index < mOutputPorts.size()) return jack_port_connected(mOutputPorts[index]); else throw std::range_error("outport index out of range"); }
static int oss_driver_write (oss_driver_t *driver, jack_nframes_t nframes) { int channel; jack_sample_t *portbuf; JSList *node; jack_port_t *port; if (!driver->run) return 0; if (nframes != driver->period_size) { jack_error( "OSS: write failed nframes != period_size (%u/%u): %s@%i", nframes, driver->period_size, __FILE__, __LINE__); return -1; } pthread_mutex_lock(&driver->mutex_out); node = driver->playback_ports; channel = 0; while (node != NULL) { port = (jack_port_t *) node->data; if (jack_port_connected(port)) { portbuf = jack_port_get_buffer(port, nframes); copy_and_convert_out(driver->outdevbuf, portbuf, nframes, channel, driver->playback_channels, driver->bits); } node = jack_slist_next(node); channel++; } pthread_mutex_unlock(&driver->mutex_out); return 0; }
static int coreaudio_driver_read (coreaudio_driver_t *driver, jack_nframes_t nframes) { jack_default_audio_sample_t *buf; channel_t chn; jack_port_t *port; JSList *node; for (chn = 0, node = driver->capture_ports; node; node = jack_slist_next (node), chn++) { port = (jack_port_t *)node->data; if (jack_port_connected (port) && (driver->incoreaudio[chn] != NULL)) { float *in = driver->incoreaudio[chn]; buf = jack_port_get_buffer (port, nframes); memcpy(buf,in,sizeof(float)*nframes); } } driver->engine->transport_cycle_start (driver->engine,jack_get_microseconds ()); return 0; }
static int iio_driver_read(iio_driver_t *driver, jack_nframes_t nframes) { JSList *node; channel_t chn; jack_nframes_t i; //Debugger<<"iio_driver_read\n"; if (nframes > 0) { ////Debugger<<"iio_driver_read nframes = "<<nframes<<"\n"; // for (jack_nframes_t i=0; i<nframes; i++){ // cout<<(float)(*data)(i,0)<<endl; // cout<<endl; // write to the connected capture ports ... node = (JSList *)driver->capture_ports; for (chn = 0; node; node = (JSList *)jack_slist_next(node), chn++) { //jack_port_t *port = static_cast<jack_port_t *>(node->data); jack_port_t *port = (jack_port_t*)(node->data); if (!jack_port_connected (port)) /* no-copy optimization */ continue; //jack_default_audio_sample_t *buf = static_cast<jack_default_audio_sample_t *>(jack_port_get_buffer (port, nframes)); jack_default_audio_sample_t *buf = (jack_default_audio_sample_t *)(jack_port_get_buffer (port, nframes)); for (i=0; i<nframes; i++) { //cout<<"row = "<<i*devChCnt+rowOffset<<" col = "<<col<<endl; //buf[i]=(*data)(i*devChCnt+rowOffset, col)*100.; buf[i]=(float)i/(float)nframes; //cout<<(*data)(i*devChCnt+rowOffset, col)<<'\t'<<buf[i]<<'\n'; } } //Debugger<<" spent "<< (driver->engine->get_microseconds()-driver->debug_last_time)<<" us waiting for lock and copying data over\n"; } return 0; }
int jack_init(JackCard* obj) { char* client_name; int error; if (!obj->jack_running) { obj->client = NULL; client_name = g_strdup_printf("linphone-%u", g_random_int()); if ((obj->client = jack_client_new (client_name)) == NULL) { g_warning("cannot create jack client"); g_free(client_name); return -1; } g_message("Found Jack Daemon"); g_free(client_name); /* tell the JACK server to call `process()' whenever there is work to be done. */ jack_set_process_callback (obj->client, process, obj); /* tell the JACK server to call `jack_shutdown()' if it ever shuts down, either entirely, or if it just decides to stop calling us. */ jack_on_shutdown (obj->client, jack_shutdown, obj); jack_set_sample_rate_callback (obj->client, samplerate, obj); obj->rate = jack_get_sample_rate (obj->client); if (obj->rate == 0) { g_warning ("rate is 0???"); if (jack_client_close(obj->client) != 0) g_warning("could not close client"); return -1; } obj->buffer_size = jack_get_buffer_size(obj->client); obj->jack_running = TRUE; } if (!obj->jack_active) { if (jack_activate (obj->client)) { g_warning("cannot activate jack client"); return -1; } else obj->jack_active = TRUE; } if (obj->read.init) { if (!obj->read.port && (obj->read.port = jack_port_register (obj->client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0))==NULL) { g_warning("error while trying to register input port"); return -1; } if (!obj->read.phys_ports && (obj->read.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) { g_warning("Cannot find any physical capture ports\n"); jack_port_unregister(obj->client, obj->read.port); obj->read.port = NULL; return -1; } if (!jack_port_connected(obj->read.port)) if ((error = jack_connect (obj->client, obj->read.phys_ports[0], jack_port_name (obj->read.port))) != 0) { g_warning("cannot connect input ports: %s -> %s\n", jack_port_name (obj->read.port), obj->read.phys_ports[0]); if (error == EEXIST) g_warning("connection already made"); else { jack_port_unregister(obj->client, obj->read.port); obj->read.port = NULL; return -1; } } obj->read.init = FALSE; } if (obj->write.init) { if (!obj->write.port && (obj->write.port = jack_port_register (obj->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0))==NULL) { g_warning("error while trying to register output port"); return -1; } if (!obj->write.phys_ports && (obj->write.phys_ports = jack_get_ports (obj->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) { g_warning("Cannot find any physical playback ports\n"); jack_port_unregister(obj->client, obj->write.port); obj->write.port = NULL; return -1; } if (!jack_port_connected(obj->write.port)) { if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[0])) != 0) { g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[0]); if (error == EEXIST) g_warning("connection already made"); else { jack_port_unregister(obj->client, obj->write.port); obj->write.port = NULL; return -1; } } if ((error = jack_connect (obj->client, jack_port_name (obj->write.port), obj->write.phys_ports[1])) != 0) { g_warning("cannot connect output ports: %s -> %s\n", jack_port_name (obj->write.port), obj->write.phys_ports[1]); if (error == EEXIST) g_warning("connection already made"); else { jack_port_unregister(obj->client, obj->write.port); obj->write.port = NULL; return -1; } } } obj->write.init = FALSE; } return 0; }
int main(int argc, char *argv[]) { jack_client_t *client = NULL; const char* client_name = DEFAULT_CLIENT_NAME; const char* connect_port = NULL; float peakdb = 0.0f; // The current peak signal level (in dB) float last_peakdb = 0.0f; // The previous peak signal level (in dB) int silence_period = 1; // Required period of silence for trigger int nodynamic_period = 10; // Required period of no-dynamic for trigger int grace_period = 0; // Period to wait before triggering again float silence_theshold = -40; // Level considered silent (in dB) float nodynamic_theshold = 0; // Minimum allowed delta between peaks (in dB) int silence_count = 0; // Number of seconds of silence detected int nodynamic_count = 0; // Number of seconds of no-dynamic detected int in_grace = 0; // Number of seconds left in grace int opt; // Make STDOUT unbuffered setbuf(stdout, NULL); // Parse command line arguments while ((opt = getopt(argc, argv, "c:n:l:p:P:d:g:vqh")) != -1) { switch (opt) { case 'c': connect_port = optarg; break; case 'n': client_name = optarg; break; case 'l': silence_theshold = atof(optarg); break; case 'p': silence_period = fabs(atoi(optarg)); break; case 'd': nodynamic_theshold = atof(optarg); break; case 'P': nodynamic_period = atof(optarg); break; case 'g': grace_period = fabs(atoi(optarg)); break; case 'v': verbose = 1; break; case 'q': quiet = 1; break; case 'h': default: /* Show usage information */ usage(); break; } } argc -= optind; argv += optind; // Validate parameters if (quiet && verbose) { fprintf(stderr, "Can't be quiet and verbose at the same time.\n"); usage(); } // Initialise Jack client = init_jack( client_name, connect_port ); // Main loop while (running) { // Sleep for 1 second usleep( 1000000 ); // Are we in grace period ? if (in_grace) { in_grace--; if (verbose) printf("%d seconds left in grace period.\n", in_grace); continue; } // Check we are connected to something if (jack_port_connected(input_port)==0) { if (verbose) printf("Input port isn't connected to anything.\n"); continue; } // Read the recent peak (in decibels) last_peakdb = peakdb; peakdb = read_peak(); // Do silence detection? if (silence_theshold) { if (verbose) printf("peak: %2.2fdB", peakdb); // Is peak too low? if (peakdb < silence_theshold) { silence_count++; if (verbose) printf(" (%d seconds of silence)\n", silence_count); } else { if (verbose) printf(" (not silent)\n"); silence_count=0; } // Have we had enough seconds of silence? if (silence_count >= silence_period) { if (!quiet) printf("**SILENCE**\n"); run_command( argc, argv ); silence_count = 0; in_grace = grace_period; } } // Do no-dynamic detection if (nodynamic_theshold) { if (verbose) printf("delta: %2.2fdB", fabs(last_peakdb-peakdb)); // Check the dynamic/delta between peaks if (fabs(last_peakdb-peakdb) < nodynamic_theshold) { nodynamic_count++; if (verbose) printf(" (%d seconds of no dynamic)\n", nodynamic_count); } else { if (verbose) printf(" (dynamic)\n"); nodynamic_count=0; } // Have we had enough seconds of no dynamic? if (nodynamic_count >= nodynamic_period) { if (!quiet) printf("**NO DYNAMIC**\n"); run_command( argc, argv ); nodynamic_count = 0; in_grace = grace_period; } } } // Clean up finish_jack( client ); return 0; }
int JackClient::Process(jack_nframes_t nframes, void *self) { int j = 0; bool isEncoded = ((JackClient*) self)->m_Encoded; for(std::map<int, JackPort*>::iterator i = m_InputPortMap.begin(); i != m_InputPortMap.end(); i++) { if(jack_port_connected(i->second->Port)) { sample_t *in = (sample_t *) jack_port_get_buffer(i->second->Port, nframes); // memcpy (i->second->Buf, in, sizeof (sample_t) * m_BufferSize); //m_BufferSize -> 2nd AudioCollector parameter //Buff attribué par SetInputBuf dans le construcAteur de AudioCollector if(isEncoded) { //Added this to write in the buffer only if //the encoder is in action if(!j) { //only streams the 1st Jack Input port if(ringbuffer_write_space(((JackClient*) self)->first) >= (sizeof(sample_t) * nframes)) { ringbuffer_write(((JackClient*) self)->first, (char *)in, (sizeof(sample_t) * nframes)); } /* else { std::cerr << "-----------Pas suffisament de place dans audio_fred !!!" << std::endl; }*/ j++; } } } } int channels = ((JackClient*) self)->m_ringbufferchannels; bool output_available = false; //m_ringbuffer created by ViewPort::add_audio //1024*512 rounded up to the next power of two. if(((JackClient*) self)->m_ringbuffer) { // static int firsttime = 1 + ceil(4096/nframes); // XXX pre-buffer TODO decrease this and compensate latency if(ringbuffer_read_space(((JackClient*) self)->m_ringbuffer) >= /*firsttime */ channels * nframes * sizeof(float)) { // firsttime=1; size_t rv = ringbuffer_read(((JackClient*) self)->m_ringbuffer, ((JackClient*) self)->m_inbuf, channels * nframes * sizeof(float)); if(isEncoded) { //Added this to write in the buffer only if //the encoder is in action if(ringbuffer_write_space(((JackClient*) self)->audio_mix_ring) >= rv) { // unsigned char *aPtr = (unsigned char *)((JackClient*) self)->m_inbuf; size_t rf = ringbuffer_write(((JackClient*) self)->audio_mix_ring, ((JackClient*) self)->m_inbuf, rv); if(rf != rv) std::cerr << "---" << rf << " : au lieu de :" << rv << " octets ecrits dans le ringbuffer !!" \ << std::endl; } else { std::cerr << "-----------Not enough room in audio_mix_ring !!!" << std::endl; } } //reads m_ringbuffer and puts it in m_inbuf //m_inbuf created in SetRingbufferPtr called by add_audio //4096 * channels * sizeof(float) if(rv >= channels * nframes * sizeof(float)) { output_available = true; } } #if 0 else if(firsttime == 1) fprintf(stderr, "AUDIO BUFFER UNDERRUN: %i samples < %i\n", ringbuffer_read_space(((JackClient*) self)->m_ringbuffer) / sizeof(float) / channels, nframes); #endif } j = 0; for(std::map<int, JackPort*>::iterator i = m_OutputPortMap.begin(); i != m_OutputPortMap.end(); i++) { if(output_available && j < channels) { sample_t *out = (sample_t *) jack_port_get_buffer(i->second->Port, nframes); memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes); deinterleave(((JackClient*) self)->m_inbuf, out, channels , j, nframes); //writes nframes of channels m_inbuf to out //two times if stereo (shifted by the channel number) #if 0 // test-noise: int i; for(i = 0; i < nframes; i++) out[i] = (float) i / (float)nframes; #endif } else { // no output availaible, clear sample_t *out = (sample_t *) jack_port_get_buffer(i->second->Port, nframes); memset(out, 0, sizeof(sample_t) * nframes); } j++; } m_BufferSize = nframes; // if(RunCallback&&RunContext) // { // // do the work // RunCallback(RunContext, nframes); // } return 0; }