Example of using libresample with jack

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Example of using libresample with jack

Kjetil Matheussen

Paul Davis:

> On Sat, 2007-02-03 at 16:00 -0800, Matthew Rushton wrote:
> > Hi Everyone,
> >   I'm writing a voip application and am interested in using a sound
> > server.  Essentially what I would like is a multi-port mixer.  Jack
> > seems great *except* for the fact it appears to be unable to natively
> > convert sample rates.  Perhaps I missed something but not being able
> > to do this would be a deal breaker for what I would like to do.  Does
> > anyone have any experience with PulseAudio?  Perhaps this is a better
> > choice for what I want to do.  Thanks in advance.
>
> you should probably use gstreamer as the internal architecture of your
> application.
>
> except for the fact that its internal sample rate conversion sucks eggs
> and CPU cycles, its the perfect choice - it will talk to all kinds of
> output sinks (JACK, Pulse, ALSA and many more)
>


Just want to add that using libsamplerate with jack is not very
complicated: (The code below is public domain)


#include <jack/jack.h>
#include <samplerate.h>

int channels;
float data_samplerate;


/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
void getDasData(float **dst,int num_frames){
  /* Provide sound data here, and only here. */
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////



long getDasResampledData_callback(void *cb_data, float **data){
  static float ret[1024];
  static float ret3[1024];
  static float *ret2[2]={&ret[0],&ret[512]};
  getDasData(ret2,512);
  for(int i=0;i<512;i++){
    ret3[i*2]=ret2[0][i];
    ret3[i*2+1]=ret2[1][i];
  }
  *data=&ret3[0];
  return 512;
}

void getDasResampledData(float **dst,int num_frames){
  double ratio=samplerate/getSourceRate();
  float outsound[num_frames*2];
  long read=src_callback_read(dassrc_state,ratio,num_frames,outsound);
  //fprintf(stderr,"read: %d, num_frames: %d\n",read,num_frames);
  for(int i=0;i<read;i++){
      dst[0][i]=outsound[i*2];
      dst[1][i]=outsound[i*2+1];
  }
  if(read<num_frames){
    float *newdst[2]={dst[0]+read,dst[1]+read};
    getDasResampledData(newdst,num_frames-read);
  }
}


static int process (jack_nframes_t nframes, void *arg){
  int ch;
  sample_t *out[channels];

  for(ch=0;ch<channels;ch++){
    out[ch]=(sample_t*)jack_port_get_buffer(ports[ch],nframes);
  }

  if( (fabs(data_samplerate - jack_samplerate)) > 0.1)
    getDasResampledData(out,numSamples);
  else
    getDasData(outputChannelData,numSamples);
  return;

  audioCallback(NULL,0,out,channels,nframes);
}

int main(){
  dassrc_state=src_callback_new(getDasResampledData_callback,SRC_QUALITY,2,NULL,NULL);
  jack_set_process_callback(client, process,NULL);
}



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Kjetil Matheussen


On Sun, 4 Feb 2007, Kjetil Svalastog Matheussen wrote:

>
> Paul Davis:
> > On Sat, 2007-02-03 at 16:00 -0800, Matthew Rushton wrote:
> > > Hi Everyone,
> > >   I'm writing a voip application and am interested in using a sound
> > > server.  Essentially what I would like is a multi-port mixer.  Jack
> > > seems great *except* for the fact it appears to be unable to natively
> > > convert sample rates.  Perhaps I missed something but not being able
> > > to do this would be a deal breaker for what I would like to do.  Does
> > > anyone have any experience with PulseAudio?  Perhaps this is a better
> > > choice for what I want to do.  Thanks in advance.
> >
> > you should probably use gstreamer as the internal architecture of your
> > application.
> >
> > except for the fact that its internal sample rate conversion sucks eggs
> > and CPU cycles, its the perfect choice - it will talk to all kinds of
> > output sinks (JACK, Pulse, ALSA and many more)
> >
>
>
> Just want to add that using libsamplerate with jack is not very
> complicated: (The code below is public domain)
>
>

Oops, some minor problems with the one I just posted. Below is a better
one.

By the way, its not much work to make the code below into a small example
program. Would it be a good idea to include it in the example/ directory?




int channels=2;
float data_samplerate=44100f;
float jack_samplerate;
int jack_buffer_size;

void getDasData(float **dst,int num_frames){
  /* Provide data here. num_frames is allways the same as
     jack_buffer_size. */
}

long getDasResampledData_callback(void *cb_data, float **data){
  float ret[jack_buffer_size*channels];
  float ret3[jack_buffer_size*channels];
  float *ret2[channels];
  for(int ch=0;ch<channels;ch++){
    ret2[ch]=&ret[ch*jack_buffer_size];
  }
  getDasData(ret2,jack_buffer_size);
  for(int i=0;i<jack_buffer_size;i++){
    for(int ch=0;ch<channels;ch++){
      ret3[i*channels+ch]=ret2[ch][i];
    }
  }
  *data=&ret3[0];
  return jack_buffer_size;
}

void getDasResampledData(float **dst,int num_frames){
  double ratio=samplerate/getSourceRate();
  float outsound[num_frames*2];
  long read=src_callback_read(dassrc_state,ratio,num_frames,outsound);
  //fprintf(stderr,"read: %d, num_frames: %d\n",read,num_frames);
  for(int ch=0;ch<channels;ch++){
    for(int i=0;i<read;i++){
      dst[ch][i]=outsound[i*channels+ch];
    }
  }
  if(read<num_frames){
    float *newdst[channels];
    for(int ch=0;ch<channels;ch++){
      newdst[ch]=dst[ch]+read;
    }
    getDasResampledData(newdst,num_frames-read);
  }
}


static int process (jack_nframes_t nframes, void *arg){
  int ch;
  sample_t *out[channels];

  for(ch=0;ch<channels;ch++){
    out[ch]=(sample_t*)jack_port_get_buffer(ports[ch],nframes);
  }

  if( (fabs(data_samplerate - jack_samplerate)) > 0.1)
    getDasResampledData(out,numSamples);
  else
    getDasData(outputChannelData,numSamples);
  return;

  audioCallback(NULL,0,out,channels,nframes);
}

int main(){
 
  dassrc_state=src_callback_new(getDasResampledData_callback,SRC_QUALITY,2,NULL,NULL);

  jack_samplerate=jack_get_sample_rate(client);
  jack_buffer_size=jack_get_buffer_size(client);
  jack_set_process_callback(client, process,NULL);

}


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Kjetil Matheussen


On Sun, 4 Feb 2007, Kjetil Svalastog Matheussen wrote:

>
>
> On Sun, 4 Feb 2007, Kjetil Svalastog Matheussen wrote:
>
> >
> > Paul Davis:
> > > On Sat, 2007-02-03 at 16:00 -0800, Matthew Rushton wrote:
> > > > Hi Everyone,
> > > >   I'm writing a voip application and am interested in using a sound
> > > > server.  Essentially what I would like is a multi-port mixer.  Jack
> > > > seems great *except* for the fact it appears to be unable to natively
> > > > convert sample rates.  Perhaps I missed something but not being able
> > > > to do this would be a deal breaker for what I would like to do.  Does
> > > > anyone have any experience with PulseAudio?  Perhaps this is a better
> > > > choice for what I want to do.  Thanks in advance.
> > >
> > > you should probably use gstreamer as the internal architecture of your
> > > application.
> > >
> > > except for the fact that its internal sample rate conversion sucks eggs
> > > and CPU cycles, its the perfect choice - it will talk to all kinds of
> > > output sinks (JACK, Pulse, ALSA and many more)
> > >
> >
> >
> > Just want to add that using libsamplerate with jack is not very
> > complicated: (The code below is public domain)
> >
> >
>
> Oops, some minor problems with the one I just posted. Below is a better
> one.

Arrgh, third try:
http://www.notam02.no/~kjetism/jackresampleexample.c



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Jack O'Quin
In reply to this post by Kjetil Matheussen
On 2/4/07, Kjetil Svalastog Matheussen <[hidden email]> wrote:
>

> By the way, its not much work to make the code below into a small example
> program. Would it be a good idea to include it in the example/ directory?

Yes, it would.

Could we even package it as a JACK service with its own
<jack/resample.h> header?

Thanks,
--
 joq

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Kjetil Matheussen


On Sun, 4 Feb 2007, Jack O'Quin wrote:

> On 2/4/07, Kjetil Svalastog Matheussen <[hidden email]> wrote:
>>
>
>>  By the way, its not much work to make the code below into a small example
>>  program. Would it be a good idea to include it in the example/ directory?
>
> Yes, it would.
>
> Could we even package it as a JACK service with its own
> <jack/resample.h> header?
>

Yeah, that would be really nice. One minor drawback would be that jack
must be linked against libresample, but I guess libresample is in memory
of most audio machines at all times anyway, so it doesn't matter.

Okay, here is an API proposal:

typedef void  (*JackResamplerCallback)(jack_nframes_t nframes, int nchannels, float *data, void *arg);

jack_resampler_t *jack_resampler_create(jack_nframes_t nframes,int nchannels, int converter_type, JackResamplerCallback resampler_callback, void *arg);

void jack_resampler_resample(jack_resampler_t *resampler,float *data, double rate, void *arg);

void jack_resampler_free(jack_resampler_t *resampler);


And here is how its used. Any suggestions?



struct Data{
   jack_client_t *client;
   jack_ports_t **ports;
   jack_resampler_t *resampler;
   int channels;
   double rate;
}

void resampler(jack_nframes_t nframes, int nchannels, float *data, void *arg){
   struct Data *data=arg;
   ...;
}

static int process(jack_nframes_t nframes, void *arg){
   struct Data *data=arg;
   sample_t *out[data->channels];
   int ch;
   for(ch=0;ch<data->channels;ch++)
     out[ch]=(sample_t*)jack_port_get_buffer(data->ports[ch],nframes);
   jack_resampler_resample(data->resampler,out,data->rate,data);
   return 0;
}

int main(){
   struct Data *data=calloc(1,sizeof(Data));
   data->client=jack_create_client();
   data->channels=2;
   data->resampler=jack_resampler_create(jack_get_buffer_size(data->client),data->channels,SRC_SINC_BEST_QUALITY,resampler,data);
   ...;
}





I would also propose a guarantee that if the rate argument to the
jack_resampler_resample function is very close to 1.0, the "resampler"
function will be called directly, so that the user don't have to add
code like this in process():

   if( (fabs(1.0 - jack_samplerate)) > 0.001)
     jack_resampler_resampler(resampler,out,rate,data);
   else
     resampler(nframes,nchannels,out,data);
   return;


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Kjetil Matheussen


On Mon, 5 Feb 2007, Kjetil S. Matheussen wrote:
>
> Okay, here is an API proposal:
>
> typedef void  (*JackResamplerCallback)(jack_nframes_t nframes, int nchannels,
> float *data, void *arg);
>

s/float *data/sample_t **data/


> jack_resampler_t *jack_resampler_create(jack_nframes_t nframes,int nchannels,
> int converter_type, JackResamplerCallback resampler_callback, void *arg);
>
> void jack_resampler_resample(jack_resampler_t *resampler,float *data, double
> rate, void *arg);
>

s/float *data/sample_t **data/


Well, I guess maybe jack_resampler_resample should have an nframes
argument as well? Its not necessarry, because nframes must be provided
for the jack_resampler_create function, but it would be handy.



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

Jack O'Quin
In reply to this post by Kjetil Matheussen
On 2/5/07, Kjetil S. Matheussen <[hidden email]> wrote:
> On Sun, 4 Feb 2007, Jack O'Quin wrote:
> > Could we even package it as a JACK service with its own
> > <jack/resample.h> header?
>
> Yeah, that would be really nice. One minor drawback would be that jack
> must be linked against libresample, but I guess libresample is in memory
> of most audio machines at all times anyway, so it doesn't matter.

Sure.  I don't see that as a big problem.

We will probably test for SRC presence in ./configure, and make
building this feature conditional.  But, I expect it will be available
in all serious music systems.

> Okay, here is an API proposal:

Thanks.  I'll need to study it a bit to understand what's going on.
I just need to find time, somehow...  ;-)
--
 joq

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jackit-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jackit-devel
Reply | Threaded
Open this post in threaded view
|

Re: Example of using libresample with jack

phamnguyenit
This post has NOT been accepted by the mailing list yet.
In reply to this post by Kjetil Matheussen
Hi. Can you give me full souce code about your sample, because i see some thing in your http://users.notam02.no/~kjetism/jackresampleexample.c  but i don't see anything definition about "jack_nframes_t" and where was you declare dassrc_state ???

Please help me ! because i need understand how it work and bring it to windows phone runtime component !