~jaromil/freej/master

« back to all changes in this revision

Viewing changes to src/audio_jack.cpp

  • Committer: fred
  • Date: 2011-01-25 17:32:31 UTC
  • Revision ID: git-v1:6126f899abcd96f6bab887cb2f840caf272b2c6b
Little modif in qt to test sound and streaming + various modif in freej

in qfreej : adding automatic call to ::openSoundDevice() when opening a
video.
Layer replaced by VideoLayer.
Added streaming config in ::Sound()
Changed from 10 to 1ms.

TODO file created (instead of the comments in the main)

in freej :
JackClient::Process() -> now writing jack outputs in a ring buffer.
commented out the RunCallback call.
OggTheoraEncoder:: -> a 30 sec recording to a file
from the JackClient ring buffer in wav PCM format added.

In theorautils.cpp -> work in progress :)

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
m_NextInputID(0),
45
45
m_NextOutputID(0),
46
46
m_inbuf(NULL),
47
 
m_ringbuffer(NULL)
 
47
m_ringbuffer(NULL),
 
48
audio_fred(NULL)
48
49
{
49
50
}
50
51
 
93
94
        }
94
95
 
95
96
        m_Attached=true;
96
 
                        
 
97
        
 
98
        
 
99
        audio_fred = ringbuffer_create(2048 * 512 * 4);         //1024 not enought
 
100
        
97
101
        return true;
98
102
}
99
103
 
108
112
                m_Client=NULL;
109
113
                m_Attached=false;
110
114
        }
111
 
        
 
115
        if(audio_fred) ringbuffer_free(audio_fred);
112
116
        // tells ssm to go back to non callback mode
113
117
        //if (RunCallback) RunCallback(RunContext, false);
114
118
}
118
122
extern "C" {
119
123
#include <jack/jack.h>
120
124
 
121
 
void deinterleave(void * _in, jack_default_audio_sample_t *out, int num_channels, int channel, int num_samples)
 
125
void deinterleave(void * _in, jack_default_audio_sample_t *out
 
126
                , int num_channels, int channel, int num_samples)
122
127
{
123
128
  int j;
124
129
  float * in;
147
152
*/
148
153
int JackClient::Process(jack_nframes_t nframes, void *self)
149
154
{       
150
 
        //std::cerr << "nframes :" << nframes << std::endl;
151
155
        for (std::map<int,JackPort*>::iterator i=m_InputPortMap.begin();
152
156
                i!=m_InputPortMap.end(); i++)
153
157
        {
155
159
                {
156
160
                        sample_t *in = (sample_t *) jack_port_get_buffer(i->second->Port, nframes);
157
161
                        memcpy (i->second->Buf, in, sizeof (sample_t) * m_BufferSize);
 
162
                        //Buff attribué par SetInputBuf dans le constructeur de AudioCollector
158
163
                }                       
159
164
        }
160
165
 
161
166
        int channels = ((JackClient*) self)->m_ringbufferchannels;
162
167
        bool output_available = false;
163
 
 
 
168
//m_ringbuffer created by ViewPort::add_audio
 
169
//1024*512 rounded up to the next power of two.
164
170
        if (((JackClient*) self)->m_ringbuffer) 
165
171
        {
166
 
          //std::cerr << "Jack inbuf avail " << ringbuffer_read_space(((JackClient*) self)->m_ringbuffer)
167
 
          //<< std::endl;       
168
172
        //  func("Jack inbuf avail %i", ringbuffer_read_space(((JackClient*) self)->m_ringbuffer));
169
173
        //  fprintf(stderr, "Jack inbuf avail %i\n", ringbuffer_read_space(((JackClient*) self)->m_ringbuffer));
170
174
 
171
175
          static int firsttime = 1 + ceil(4096/nframes); // XXX pre-buffer  TODO decrease this and compensate latency
172
176
        
173
 
        //std::cerr << "firsttime :" << firsttime << " nframes :" << nframes << std::endl;
174
177
          if (ringbuffer_read_space(((JackClient*) self)->m_ringbuffer) >= 
175
178
                              firsttime * channels * nframes * sizeof(float)) 
176
179
          {
178
181
                  size_t rv = ringbuffer_read(((JackClient*) self)->m_ringbuffer, 
179
182
                                              ((JackClient*) self)->m_inbuf,
180
183
                                              channels*nframes*sizeof(float));
181
 
                  std::cerr << "in boucle, rv :" << rv << " nframes :" << nframes
182
 
                        << " channels :" << channels;
 
184
                  if (ringbuffer_write_space (((JackClient*) self)->audio_fred) >= rv)
 
185
                  {
 
186
                    unsigned char *aPtr = (unsigned char *)((JackClient*) self)->m_inbuf;
 
187
//                  printf("----:\n");
 
188
//                  for (int i=0; i < 24; i++, aPtr++)
 
189
//                  {
 
190
//                    printf ("%02x ", *aPtr);
 
191
//                  }
 
192
//                  printf ("\n----;\n");
 
193
//                  fflush(stdout);
 
194
                    size_t rf = ringbuffer_write (((JackClient*) self)->audio_fred, ((JackClient*) self)->m_inbuf, rv);
 
195
                    if (rf != rv)
 
196
                        std::cerr << "---" << rf << " : au lieu de :" << rv << " octets ecrits dans le ringbuffer !!"\
 
197
                                                                               << std::endl;
 
198
                  }
 
199
                  else
 
200
                  {
 
201
                     std::cerr << "-----------Pas suffisament de place dans audio_fred !!!" << std::endl; 
 
202
                  }
 
203
//reads m_ringbuffer and puts it in m_inbuf
 
204
//m_inbuf created in SetRingbufferPtr called by add_audio
 
205
//4096 * channels * sizeof(float)
183
206
                if (rv >= channels * nframes * sizeof(float))
184
207
                {
185
208
                        output_available = true;
186
 
                        std::cerr << "  output_available";
187
209
                }
188
 
                 std::cerr << std::endl; 
189
210
          }
190
211
#if 0
191
212
          else if (firsttime==1)
193
214
#endif
194
215
        }
195
216
 
196
 
        int j=0;        
 
217
        int j=0;
197
218
        for (std::map<int,JackPort*>::iterator i=m_OutputPortMap.begin();
198
219
                i!=m_OutputPortMap.end(); i++)
199
220
        {
201
222
                {
202
223
                        sample_t *out = (sample_t *) jack_port_get_buffer(i->second->Port, nframes);
203
224
                        memset (out, 0, sizeof (jack_default_audio_sample_t) * nframes);
204
 
                        deinterleave(((JackClient*) self)->m_inbuf, out, channels, j, nframes);
205
 
 
 
225
                        deinterleave(((JackClient*) self)->m_inbuf, out, channels
 
226
                                                , j, nframes);
 
227
//writes nframes of channels m_inbuf to out
 
228
//two times if stereo (shifted by the channel number)
206
229
#if 0                   // test-noise:
207
230
                        int i; for (i=0; i< nframes; i++) out[i]=(float) i/(float)nframes;
208
231
#endif
209
232
                }
210
 
                else // no output availible, clear
 
233
                else // no output availaible, clear
211
234
                {
212
235
                        sample_t *out = (sample_t *) jack_port_get_buffer(i->second->Port, nframes);
213
236
                        memset (out, 0, sizeof (sample_t) * nframes);
217
240
        
218
241
        m_BufferSize=nframes;
219
242
                        
220
 
        if(RunCallback&&RunContext)
221
 
        {
222
 
                // do the work
223
 
                RunCallback(RunContext, nframes);
224
 
        }
 
243
//      if(RunCallback&&RunContext)
 
244
//      {
 
245
//              // do the work
 
246
//              RunCallback(RunContext, nframes);
 
247
//      }
225
248
        
226
249
        return 0;
227
250
}
231
254
        m_ringbuffer = NULL;
232
255
 
233
256
        func ("jack-client ringbuffer set for %i channels", channels);
234
 
 
 
257
std::cout << "SetRingbufferPtr, channels :" << channels << std::endl;
235
258
        for (i=m_NextOutputID; i<channels; i++) {
236
259
                AddOutputPort();
 
260
                if (i == 0)     //connects output ports to system input ports (fred_99)
 
261
                {
 
262
                        this->ConnectOutput(i, "system:playback_1");
 
263
                        this->ConnectOutput(i, "freej:In0");
 
264
                }
 
265
                //else if (i == 1)
 
266
                        //this->ConnectOutput(i, "system:playback_2"); //see how to test if there is two system channels
237
267
        }
238
268
 
239
269
        if(m_inbuf) free(m_inbuf);
363
393
void JackClient::ConnectOutput(int n, const std::string &JackPort)
364
394
{
365
395
  if (!IsAttached()) return;
366
 
  std::cerr<<"JackClient::ConnectOutput: connecting source ["<<m_OutputPortMap[n]->Name<<"] to dest ["<<JackPort<<"]"<<std::endl;
 
396
  std::cerr<<"JackClient::ConnectOutput: connecting source ["<<m_OutputPortMap[n]->Name<<"] \
 
397
        to dest ["<<JackPort<<"]"<<std::endl;
367
398
  
368
399
  if (m_OutputPortMap[n]->ConnectedTo!="")
369
400
    {