root/jack2/branches/libjacknet/common/JackNetTool.cpp

Revision 3951, 35.2 kB (checked in by sletz, 6 months ago)

Add NetIntAudioBuffer? class.

Line 
1 /*
2 Copyright (C) 2008 Romain Moret at Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "JackNetTool.h"
21
22 #ifdef __APPLE__
23
24 #include <mach/mach_time.h>
25
26 class HardwareClock
27 {
28 public:
29         HardwareClock();
30
31         void Reset();
32         void Update();
33
34         float GetDeltaTime() const;
35         double GetTime() const;         
36        
37 private:
38         double m_clockToSeconds;
39
40         uint64_t m_startAbsTime;
41         uint64_t m_lastAbsTime;
42
43         double m_time;
44         float m_deltaTime;
45 };
46
47 HardwareClock::HardwareClock()
48 {
49         mach_timebase_info_data_t info;
50         mach_timebase_info(&info);
51         m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
52
53         Reset();
54 }
55
56 void HardwareClock::Reset()
57 {
58         m_startAbsTime = mach_absolute_time();
59         m_lastAbsTime = m_startAbsTime;
60        
61         m_time = m_startAbsTime*m_clockToSeconds;
62         m_deltaTime = 1.0f/60.0f;
63 }
64
65 void HardwareClock::Update()
66 {
67         const uint64_t currentTime = mach_absolute_time();
68         const uint64_t dt = currentTime - m_lastAbsTime;
69
70         m_time = currentTime*m_clockToSeconds;
71         m_deltaTime = (double)dt*m_clockToSeconds;
72
73         m_lastAbsTime = currentTime;
74 }
75
76 float HardwareClock::GetDeltaTime() const
77 {
78         return m_deltaTime;
79 }
80
81 double HardwareClock::GetTime() const
82 {
83         return m_time;
84 }
85
86 #endif
87
88 using namespace std;
89
90 namespace Jack
91 {
92 // NetMidiBuffer**********************************************************************************
93
94     NetMidiBuffer::NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
95     {
96         fNPorts = nports;
97         fMaxBufsize = fNPorts * sizeof ( sample_t ) * params->fPeriodSize ;
98         fMaxPcktSize = params->fMtu - sizeof ( packet_header_t );
99         fBuffer = new char[fMaxBufsize];
100         fPortBuffer = new JackMidiBuffer* [fNPorts];
101         for ( int port_index = 0; port_index < fNPorts; port_index++ )
102             fPortBuffer[port_index] = NULL;
103         fNetBuffer = net_buffer;
104        
105         fCycleSize = params->fMtu * (max(params->fSendMidiChannels, params->fReturnMidiChannels) *
106                                      params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
107     }
108
109     NetMidiBuffer::~NetMidiBuffer()
110     {
111         delete[] fBuffer;
112         delete[] fPortBuffer;
113     }
114
115     size_t NetMidiBuffer::GetCycleSize()
116     {
117         return fCycleSize;
118     }
119    
120     int NetMidiBuffer::GetNumPackets()
121     {
122         /*
123         return (data_size % PACKET_AVAILABLE_SIZE)
124                 ? (data_size / PACKET_AVAILABLE_SIZE + 1)
125                 : data_size / PACKET_AVAILABLE_SIZE;
126         */
127        
128         //TODO
129         return 0;
130     }
131
132     void NetMidiBuffer::SetBuffer ( int index, JackMidiBuffer* buffer )
133     {
134         fPortBuffer[index] = buffer;
135     }
136
137     JackMidiBuffer* NetMidiBuffer::GetBuffer ( int index )
138     {
139         return fPortBuffer[index];
140     }
141
142     void NetMidiBuffer::DisplayEvents()
143     {
144         for ( int port_index = 0; port_index < fNPorts; port_index++ )
145         {
146             for ( uint event = 0; event < fPortBuffer[port_index]->event_count; event++ )
147                 if ( fPortBuffer[port_index]->IsValid() )
148                     jack_info ( "port %d : midi event %u/%u -> time : %u, size : %u",
149                                 port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
150                                 fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size );
151         }
152     }
153
154     int NetMidiBuffer::RenderFromJackPorts()
155     {
156         int pos = 0;
157         size_t copy_size;
158         for ( int port_index = 0; port_index < fNPorts; port_index++ )
159         {
160             char* write_pos = fBuffer + pos;
161            
162             copy_size = sizeof ( JackMidiBuffer ) + fPortBuffer[port_index]->event_count * sizeof ( JackMidiEvent );
163             memcpy ( fBuffer + pos, fPortBuffer[port_index], copy_size );
164             pos += copy_size;
165             memcpy ( fBuffer + pos, fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
166                      fPortBuffer[port_index]->write_pos );
167             pos += fPortBuffer[port_index]->write_pos;
168            
169             JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
170             MidiBufferHToN(midi_buffer, midi_buffer);
171         }
172         return pos;
173     }
174
175     int NetMidiBuffer::RenderToJackPorts()
176     {
177         int pos = 0;
178         int copy_size;
179         for ( int port_index = 0; port_index < fNPorts; port_index++ )
180         {
181             JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
182             MidiBufferNToH(midi_buffer, midi_buffer);
183            
184             copy_size = sizeof ( JackMidiBuffer ) + reinterpret_cast<JackMidiBuffer*> ( fBuffer + pos )->event_count * sizeof ( JackMidiEvent );
185             memcpy ( fPortBuffer[port_index], fBuffer + pos, copy_size );
186             pos += copy_size;
187             memcpy ( fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
188                      fBuffer + pos, fPortBuffer[port_index]->write_pos );
189             pos += fPortBuffer[port_index]->write_pos;
190         }
191         return pos;
192     }
193    
194     int NetMidiBuffer::RenderFromNetwork ( int subcycle, size_t copy_size )
195     {
196         memcpy ( fBuffer + subcycle * fMaxPcktSize, fNetBuffer, copy_size );
197         return copy_size;
198     }
199
200     int NetMidiBuffer::RenderToNetwork ( int subcycle, size_t total_size )
201     {
202         int size = total_size - subcycle * fMaxPcktSize;
203         int copy_size = ( size <= fMaxPcktSize ) ? size : fMaxPcktSize;
204         memcpy ( fNetBuffer, fBuffer + subcycle * fMaxPcktSize, copy_size );
205         return copy_size;
206     }
207
208 // net audio buffer *********************************************************************************
209
210     NetSingleAudioBuffer::NetSingleAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
211         : fPortBuffer(params, nports), fNetBuffer(net_buffer)
212     {}
213
214     NetSingleAudioBuffer::~NetSingleAudioBuffer()
215     {}
216
217     size_t NetSingleAudioBuffer::GetCycleSize()
218     {
219         return fPortBuffer.GetCycleSize();
220     }
221
222     void NetSingleAudioBuffer::SetBuffer ( int index, sample_t* buffer )
223     {
224         fPortBuffer.SetBuffer(index, buffer);
225     }
226
227     sample_t* NetSingleAudioBuffer::GetBuffer ( int index )
228     {
229         return fPortBuffer.GetBuffer(index);
230     }
231
232     int NetSingleAudioBuffer::RenderFromJackPorts ()
233     {
234         return fPortBuffer.RenderFromJackPorts();
235     }
236
237     int NetSingleAudioBuffer::RenderToJackPorts ()
238     {
239         return fPortBuffer.RenderToJackPorts();
240     }
241    
242      //network<->buffer
243     int NetSingleAudioBuffer::RenderFromNetwork ( int cycle,  int subcycle, size_t copy_size )
244     {
245         return fPortBuffer.RenderFromNetwork(fNetBuffer, cycle, subcycle, copy_size);
246     }
247    
248     int NetSingleAudioBuffer::RenderToNetwork (int subcycle, size_t total_size )
249     {
250         return fPortBuffer.RenderToNetwork(fNetBuffer, subcycle, total_size);
251     }
252    
253     // Celt audio buffer *********************************************************************************
254    
255 #ifdef CELT
256
257     #define KPS 32
258     #define KPS_DIV 8
259
260     NetCeltAudioBuffer::NetCeltAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
261         : fNetBuffer(net_buffer)
262     {
263         int res1, res2;
264        
265         fNPorts = nports;
266         fPeriodSize = params->fPeriodSize;
267        
268         fCeltMode = new CELTMode *[fNPorts];
269         fCeltEncoder = new CELTEncoder *[fNPorts];
270         fCeltDecoder = new CELTDecoder *[fNPorts];
271        
272         memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
273         memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
274         memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
275        
276         int error = CELT_OK;
277        
278         for (int i = 0; i < fNPorts; i++)  {
279             fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
280             if (error != CELT_OK)
281                 goto error;
282            
283             fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
284             if (error != CELT_OK)
285                 goto error;
286             celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
287                
288             fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
289             if (error != CELT_OK)
290                 goto error;
291             celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
292         }
293        
294         fPortBuffer = new sample_t* [fNPorts];
295         for (int port_index = 0; port_index < fNPorts; port_index++)
296             fPortBuffer[port_index] = NULL;
297          
298         /*
299         celt_int32 lookahead;
300         celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
301         */
302        
303         //fCompressedSizeByte = (KPS * params->fPeriodSize * 1024 / params->fSampleRate / 8)&(~1);
304         fCompressedSizeByte = (params->fPeriodSize * sizeof(sample_t)) / KPS_DIV;   // TODO
305        
306         fCompressedBuffer = new unsigned char* [fNPorts];
307         for (int port_index = 0; port_index < fNPorts; port_index++)
308             fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
309      
310         jack_log("fCompressedSizeByte %d", fCompressedSizeByte);
311        
312         res1 = (fNPorts * fCompressedSizeByte) % (params->fMtu - sizeof(packet_header_t));
313         res2 = (fNPorts * fCompressedSizeByte) / (params->fMtu - sizeof(packet_header_t));
314        
315         jack_log("res1 = %d res2 = %d", res1, res2);
316          
317         fNumPackets = (res1) ? (res2 + 1) : res2;
318            
319         fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
320         fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
321        
322         jack_log("fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
323        
324         fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
325         fCycleSize = params->fMtu * fNumPackets;
326        
327         fLastSubCycle = -1;
328         return;
329        
330     error:
331    
332         FreeCelt();
333         throw std::bad_alloc();
334     }
335
336     NetCeltAudioBuffer::~NetCeltAudioBuffer()
337     {
338         FreeCelt();
339        
340         for (int port_index = 0; port_index < fNPorts; port_index++)
341             delete [] fCompressedBuffer[port_index];
342            
343         delete [] fCompressedBuffer;
344         delete [] fPortBuffer;
345     }
346    
347     void NetCeltAudioBuffer::FreeCelt()
348     {
349         for (int i = 0; i < fNPorts; i++)  {
350             if (fCeltEncoder[i])
351                 celt_encoder_destroy(fCeltEncoder[i]);
352             if (fCeltDecoder[i])
353                 celt_decoder_destroy(fCeltDecoder[i]);
354             if (fCeltMode[i])
355                 celt_mode_destroy(fCeltMode[i]);
356         }
357          
358         delete [] fCeltMode;
359         delete [] fCeltEncoder;
360         delete [] fCeltDecoder;
361     }
362
363     size_t NetCeltAudioBuffer::GetCycleSize()
364     {
365         return fCycleSize;
366     }
367    
368     float NetCeltAudioBuffer::GetCycleDuration()
369     {
370         return fCycleDuration;
371     }
372    
373     int NetCeltAudioBuffer::GetNumPackets()
374     {
375         return fNumPackets;
376     }
377
378     void NetCeltAudioBuffer::SetBuffer(int index, sample_t* buffer)
379     {
380         fPortBuffer[index] = buffer;
381     }
382
383     sample_t* NetCeltAudioBuffer::GetBuffer(int index)
384     {
385         return fPortBuffer[index];
386     }
387
388     int NetCeltAudioBuffer::RenderFromJackPorts()
389     {
390         float floatbuf[fPeriodSize];
391        
392         for (int port_index = 0; port_index < fNPorts; port_index++) {
393             memcpy(floatbuf, fPortBuffer[port_index], fPeriodSize * sizeof(float));
394             int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
395             if (res != fCompressedSizeByte) {
396                 jack_error("celt_encode_float error fCompressedSizeByte = %d  res = %d", fCompressedSizeByte, res);
397             }
398         }
399        
400         return fNPorts * fCompressedSizeByte;  // in bytes
401     }
402
403     int NetCeltAudioBuffer::RenderToJackPorts()
404     {
405         for (int port_index = 0; port_index < fNPorts; port_index++) {
406             int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
407             if (res != CELT_OK) {
408                 jack_error("celt_decode_float error res = %d", fCompressedSizeByte, res);
409             }
410         }
411        
412         fLastSubCycle = -1;
413         //return fPeriodSize * sizeof(sample_t);  // in bytes; TODO
414         return 0;
415     }
416    
417     HardwareClock clock;
418      //network<->buffer
419     int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int subcycle, size_t copy_size)
420     {
421         //clock.Update();
422        
423         if (subcycle == fNumPackets - 1) {
424             for (int port_index = 0; port_index < fNPorts; port_index++)
425                 memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
426         } else {
427             for (int port_index = 0; port_index < fNPorts; port_index++)
428                 memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
429         }
430        
431         if (subcycle != fLastSubCycle + 1)
432             jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
433        
434         fLastSubCycle = subcycle;
435        
436         //clock.Update();
437                 //const float dt = clock.GetDeltaTime();
438                 //printf("Delta: %f s\n", dt);
439        
440         return copy_size;
441     }
442    
443     int NetCeltAudioBuffer::RenderToNetwork(int subcycle, size_t total_size)
444     {
445         if (subcycle == fNumPackets - 1) {
446             for (int port_index = 0; port_index < fNPorts; port_index++)
447                 memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
448             return fNPorts * fLastSubPeriodBytesSize;
449         } else {
450             for (int port_index = 0; port_index < fNPorts; port_index++)
451                 memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
452             return fNPorts * fSubPeriodBytesSize;
453         }
454            
455         return fNPorts * fSubPeriodBytesSize;
456     }
457
458 #endif
459
460
461     NetIntAudioBuffer::NetIntAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
462         : fNetBuffer(net_buffer)
463     {
464         int res1, res2;
465        
466         fNPorts = nports;
467         fPeriodSize = params->fPeriodSize;
468        
469         fPortBuffer = new sample_t* [fNPorts];
470         for (int port_index = 0; port_index < fNPorts; port_index++)
471             fPortBuffer[port_index] = NULL;
472          
473         fIntBuffer = new short* [fNPorts];
474         for (int port_index = 0; port_index < fNPorts; port_index++)
475             fIntBuffer[port_index] = new short[fPeriodSize];
476            
477         fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
478      
479         jack_log("fCompressedSizeByte %d", fCompressedSizeByte);
480        
481         res1 = (fNPorts * fCompressedSizeByte) % (params->fMtu - sizeof(packet_header_t));
482         res2 = (fNPorts * fCompressedSizeByte) / (params->fMtu - sizeof(packet_header_t));
483        
484         jack_log("res1 = %d res2 = %d", res1, res2);
485          
486         fNumPackets = (res1) ? (res2 + 1) : res2;
487        
488         fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
489         fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
490        
491         //fLastSubPeriodBytesSize = fSubPeriodBytesSize + (fCompressedSizeByte - (fSubPeriodBytesSize * fNumPackets));
492         fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
493         fLastSubPeriodSize = fLastSubPeriodBytesSize / sizeof(short);
494        
495         jack_log("fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
496        
497         fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
498         fCycleSize = params->fMtu * fNumPackets;
499        
500         fLastSubCycle = -1;
501         return;
502      }
503
504     NetIntAudioBuffer::~NetIntAudioBuffer()
505     {
506         for (int port_index = 0; port_index < fNPorts; port_index++)
507             delete [] fIntBuffer[port_index];
508            
509         delete [] fIntBuffer;
510         delete [] fPortBuffer;
511     }
512
513     size_t NetIntAudioBuffer::GetCycleSize()
514     {
515         return fCycleSize;
516     }
517    
518     float NetIntAudioBuffer::GetCycleDuration()
519     {
520         return fCycleDuration;
521     }
522    
523     int NetIntAudioBuffer::GetNumPackets()
524     {
525         return fNumPackets;
526     }
527
528     void NetIntAudioBuffer::SetBuffer(int index, sample_t* buffer)
529     {
530         fPortBuffer[index] = buffer;
531     }
532
533     sample_t* NetIntAudioBuffer::GetBuffer(int index)
534     {
535         return fPortBuffer[index];
536     }
537
538     int NetIntAudioBuffer::RenderFromJackPorts()
539     {
540         for (int port_index = 0; port_index < fNPorts; port_index++) {
541             for (unsigned int frame = 0; frame < fPeriodSize; frame++)
542                 fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
543         }
544        
545         return fNPorts * fCompressedSizeByte;  // in bytes
546     }
547
548     int NetIntAudioBuffer::RenderToJackPorts()
549     {
550         for (int port_index = 0; port_index < fNPorts; port_index++) {
551             float coef = 1.f / 32768.f;
552             for (unsigned int frame = 0; frame < fPeriodSize; frame++)
553                 fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
554         }
555        
556         fLastSubCycle = -1;
557         //return fPeriodSize * sizeof(sample_t);  // in bytes; TODO
558         return 0;
559     }
560    
561      //network<->buffer
562     int NetIntAudioBuffer::RenderFromNetwork(int cycle, int subcycle, size_t copy_size)
563     {
564         if (subcycle == fNumPackets - 1) {
565             for (int port_index = 0; port_index < fNPorts; port_index++)
566                 memcpy(fIntBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
567         } else {
568             for (int port_index = 0; port_index < fNPorts; port_index++)
569                 memcpy(fIntBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
570         }
571        
572         if (subcycle != fLastSubCycle + 1)
573             jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
574        
575         fLastSubCycle = subcycle;
576         return copy_size;
577     }
578    
579     int NetIntAudioBuffer::RenderToNetwork(int subcycle, size_t total_size)
580     {
581         if (subcycle == fNumPackets - 1) {
582             for (int port_index = 0; port_index < fNPorts; port_index++)
583                 memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + subcycle * fSubPeriodSize, fLastSubPeriodBytesSize);
584             return fNPorts * fLastSubPeriodBytesSize;
585         } else {
586             for (int port_index = 0; port_index < fNPorts; port_index++)
587                 memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize);
588             return fNPorts * fSubPeriodBytesSize;
589         }
590     }
591
592 // Buffered
593
594 /*
595     NetBufferedAudioBuffer::NetBufferedAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
596     {
597         fMaxCycle = 0;
598         fNetBuffer = net_buffer;
599        
600         for (int i = 0; i < AUDIO_BUFFER_SIZE; i++) {
601             fPortBuffer[i].Init(params, nports);
602         }
603        
604         fJackPortBuffer = new sample_t* [nports];
605         for ( uint32_t port_index = 0; port_index < nports; port_index++ )
606             fJackPortBuffer[port_index] = NULL;
607     }
608
609     NetBufferedAudioBuffer::~NetBufferedAudioBuffer()
610     {
611         delete [] fJackPortBuffer;
612     }
613
614     size_t NetBufferedAudioBuffer::GetCycleSize()
615     {
616         return fPortBuffer[0].GetCycleSize();
617     }
618
619     void NetBufferedAudioBuffer::SetBuffer ( int index, sample_t* buffer )
620     {
621         fJackPortBuffer[index] = buffer;
622     }
623
624     sample_t* NetBufferedAudioBuffer::GetBuffer ( int index )
625     {
626         return fJackPortBuffer[index];
627     }
628    
629     void NetBufferedAudioBuffer::RenderFromJackPorts (int subcycle )
630     {
631         fPortBuffer[0].RenderFromJackPorts(fNetBuffer, subcycle);  // Always use first buffer...
632     }
633
634     void NetBufferedAudioBuffer::RenderToJackPorts (int cycle, int subcycle)
635     {
636         if (cycle < fMaxCycle) {
637             jack_info("Wrong order fCycle %d subcycle %d fMaxCycle %d", cycle, subcycle, fMaxCycle);
638         }
639         fPortBuffer[cycle % AUDIO_BUFFER_SIZE].RenderToJackPorts(fNetBuffer, subcycle);
640     }
641
642     void NetBufferedAudioBuffer::FinishRenderToJackPorts (int cycle)
643     {
644         fMaxCycle = std::max(fMaxCycle, cycle);
645         fPortBuffer[(cycle + 1) % AUDIO_BUFFER_SIZE].Copy(fJackPortBuffer);  // Copy internal buffer in JACK ports
646     }
647     */
648
649 // SessionParams ************************************************************************************
650
651     SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params )
652     {
653         memcpy(dst_params, src_params, sizeof(session_params_t));
654         dst_params->fPacketID = htonl ( src_params->fPacketID );
655         dst_params->fMtu = htonl ( src_params->fMtu );
656         dst_params->fID = htonl ( src_params->fID );
657         dst_params->fTransportSync = htonl ( src_params->fTransportSync );
658         dst_params->fSendAudioChannels = htonl ( src_params->fSendAudioChannels );
659         dst_params->fReturnAudioChannels = htonl ( src_params->fReturnAudioChannels );
660         dst_params->fSendMidiChannels = htonl ( src_params->fSendMidiChannels );
661         dst_params->fReturnMidiChannels = htonl ( src_params->fReturnMidiChannels );
662         dst_params->fSampleRate = htonl ( src_params->fSampleRate );
663         dst_params->fPeriodSize = htonl ( src_params->fPeriodSize );
664         dst_params->fBitdepth = htonl ( src_params->fBitdepth );
665         dst_params->fSlaveSyncMode = htonl ( src_params->fSlaveSyncMode );
666     }
667
668     SERVER_EXPORT void SessionParamsNToH (  session_params_t* src_params, session_params_t* dst_params )
669     {
670         memcpy(dst_params, src_params, sizeof(session_params_t));
671         dst_params->fPacketID = ntohl ( src_params->fPacketID );
672         dst_params->fMtu = ntohl ( src_params->fMtu );
673         dst_params->fID = ntohl ( src_params->fID );
674         dst_params->fTransportSync = ntohl ( src_params->fTransportSync );
675         dst_params->fSendAudioChannels = ntohl ( src_params->fSendAudioChannels );
676         dst_params->fReturnAudioChannels = ntohl ( src_params->fReturnAudioChannels );
677         dst_params->fSendMidiChannels = ntohl ( src_params->fSendMidiChannels );
678         dst_params->fReturnMidiChannels = ntohl ( src_params->fReturnMidiChannels );
679         dst_params->fSampleRate = ntohl ( src_params->fSampleRate );
680         dst_params->fPeriodSize = ntohl ( src_params->fPeriodSize );
681         dst_params->fBitdepth = ntohl ( src_params->fBitdepth );
682         dst_params->fSlaveSyncMode = ntohl ( src_params->fSlaveSyncMode );
683     }
684
685     SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params )
686     {
687         char bitdepth[16];
688         ( params->fBitdepth ) ? sprintf ( bitdepth, "%u", params->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
689         char mode[8];
690         switch ( params->fNetworkMode )
691         {
692             case 's' :
693                 strcpy ( mode, "slow" );
694                 break;
695             case 'n' :
696                 strcpy ( mode, "normal" );
697                 break;
698             case 'f' :
699                 strcpy ( mode, "fast" );
700                 break;
701         }
702         jack_info ( "**************** Network parameters ****************" );
703         jack_info ( "Name : %s", params->fName );
704         jack_info ( "Protocol revision : %d", params->fProtocolVersion );
705         jack_info ( "MTU : %u", params->fMtu );
706         jack_info ( "Master name : %s", params->fMasterNetName );
707         jack_info ( "Slave name : %s", params->fSlaveNetName );
708         jack_info ( "ID : %u", params->fID );
709         jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
710         jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
711         jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
712         jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
713         jack_info ( "Period size : %u frames per period", params->fPeriodSize );
714         jack_info ( "Bitdepth : %s", bitdepth );
715         jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
716         jack_info ( "Network mode : %s", mode );
717         jack_info ( "****************************************************" );
718     }
719
720     SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
721     {
722         switch ( params->fPacketID )
723         {
724             case 0:
725                 return SLAVE_AVAILABLE;
726             case 1:
727                 return SLAVE_SETUP;
728             case 2:
729                 return START_MASTER;
730             case 3:
731                 return START_SLAVE;
732             case 4:
733                 return KILL_MASTER;
734         }
735         return INVALID;
736     }
737
738     SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
739     {
740         switch ( packet_type )
741         {
742             case INVALID:
743                 return -1;
744             case SLAVE_AVAILABLE:
745                 params->fPacketID = 0;
746                 break;
747             case SLAVE_SETUP:
748                 params->fPacketID = 1;
749                 break;
750             case START_MASTER:
751                 params->fPacketID = 2;
752                 break;
753             case START_SLAVE:
754                 params->fPacketID = 3;
755                 break;
756             case KILL_MASTER:
757                 params->fPacketID = 4;
758         }
759         return 0;
760     }
761
762 // Packet header **********************************************************************************
763
764     SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header )
765     {
766         memcpy(dst_header, src_header, sizeof(packet_header_t));
767         dst_header->fID = htonl ( src_header->fID );
768         dst_header->fBitdepth = htonl ( src_header->fBitdepth );
769         dst_header->fNumPacket = htonl ( src_header->fNumPacket );
770         dst_header->fPacketSize = htonl ( src_header->fPacketSize );
771         dst_header->fCycle = htonl ( src_header->fCycle );
772         dst_header->fSubCycle = htonl ( src_header->fSubCycle );
773         dst_header->fIsLastPckt = htonl ( src_header->fIsLastPckt );
774     }
775
776     SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header )
777     {
778         memcpy(dst_header, src_header, sizeof(packet_header_t));
779         dst_header->fID = ntohl ( src_header->fID );
780         dst_header->fBitdepth = ntohl ( src_header->fBitdepth );
781         dst_header->fNumPacket = ntohl ( src_header->fNumPacket );
782         dst_header->fPacketSize = ntohl ( src_header->fPacketSize );
783         dst_header->fCycle = ntohl ( src_header->fCycle );
784         dst_header->fSubCycle = ntohl ( src_header->fSubCycle );
785         dst_header->fIsLastPckt = ntohl ( src_header->fIsLastPckt );
786     }
787
788     SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header )
789     {
790         char bitdepth[16];
791         ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
792         jack_info ( "********************Header********************" );
793         jack_info ( "Data type : %c", header->fDataType );
794         jack_info ( "Data stream : %c", header->fDataStream );
795         jack_info ( "ID : %u", header->fID );
796         jack_info ( "Cycle : %u", header->fCycle );
797         jack_info ( "SubCycle : %u", header->fSubCycle );
798         jack_info ( "Midi packets : %u", header->fNumPacket );
799         jack_info ( "Last packet : '%s'", ( header->fIsLastPckt ) ? "yes" : "no" );
800         jack_info ( "Bitdepth : %s", bitdepth );
801         jack_info ( "**********************************************" );
802     }
803    
804     SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data )
805     {
806         jack_info ( "********************Network Transport********************" );
807         jack_info ( "Transport new state : %u", data->fNewState );
808         jack_info ( "Transport timebase master : %u", data->fTimebaseMaster );
809         jack_info ( "Transport cycle state : %u", data->fState );
810         jack_info ( "**********************************************" );
811     }
812    
813     SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
814     {
815         dst_buffer->magic = htonl(src_buffer->magic);
816         dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
817         dst_buffer->nframes = htonl(src_buffer->nframes);
818         dst_buffer->write_pos = htonl(src_buffer->write_pos);
819         dst_buffer->event_count = htonl(src_buffer->event_count);
820         dst_buffer->lost_events = htonl(src_buffer->lost_events);
821         dst_buffer->mix_index = htonl(src_buffer->mix_index);
822     }
823    
824     SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
825     {
826         dst_buffer->magic = ntohl(src_buffer->magic);
827         dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
828         dst_buffer->nframes = ntohl(src_buffer->nframes);
829         dst_buffer->write_pos = ntohl(src_buffer->write_pos);
830         dst_buffer->event_count = ntohl(src_buffer->event_count);
831         dst_buffer->lost_events = ntohl(src_buffer->lost_events);
832         dst_buffer->mix_index = ntohl(src_buffer->mix_index);
833     }
834    
835     SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
836     {
837         dst_params->fNewState = htonl(src_params->fNewState);
838         dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
839         dst_params->fState = htonl(src_params->fState);
840         dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
841         dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
842         dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
843         dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
844         dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
845         dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
846         dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
847         dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
848         dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
849         dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
850         dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
851         dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
852         dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
853         dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
854         dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
855         dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
856         dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
857         dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
858         dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
859     }
860    
861     SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
862     {
863         dst_params->fNewState = ntohl(src_params->fNewState);
864         dst_params->fTimebaseMaster =  ntohl(src_params->fTimebaseMaster);
865         dst_params->fState = ntohl(src_params->fState);
866         dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
867         dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
868         dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
869         dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
870         dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
871         dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
872         dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
873         dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
874         dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
875         dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
876         dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
877         dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
878         dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
879         dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
880         dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
881         dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
882         dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
883         dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
884         dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
885     }
886
887 // Utility *******************************************************************************************************
888
889     SERVER_EXPORT int SocketAPIInit()
890     {
891 #ifdef WIN32
892         WORD wVersionRequested = MAKEWORD ( 2, 2 );
893         WSADATA wsaData;
894
895         if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
896         {
897             jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
898             return -1;
899         }
900
901         if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
902         {
903             jack_error ( "Could not find a useable version of Winsock.dll\n" );
904             WSACleanup();
905             return -1;
906         }
907 #endif
908         return 0;
909     }
910
911     SERVER_EXPORT int SocketAPIEnd()
912     {
913 #ifdef WIN32
914         return WSACleanup();
915 #endif
916         return 0;
917     }
918
919     SERVER_EXPORT const char* GetTransportState ( int transport_state )
920     {
921         switch ( transport_state )
922         {
923             case JackTransportRolling:
924                 return "rolling";
925             case JackTransportStarting:
926                 return "starting";
927             case JackTransportStopped:
928                 return "stopped";
929             case JackTransportNetStarting:
930                 return "netstarting";
931         }
932         return NULL;
933     }
934 }
Note: See TracBrowser for help on using the browser.