jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

868 lines
28KB

  1. /*
  2. * NetJack - Packet Handling functions
  3. *
  4. * used by the driver and the jacknet_client
  5. *
  6. * Copyright (C) 2008 Marc-Olivier Barre <marco@marcochapeau.org>
  7. * Copyright (C) 2008 Pieter Palmers <pieterpalmers@users.sourceforge.net>
  8. * Copyright (C) 2006 Torben Hohn <torbenh@gmx.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * $Id: net_driver.c,v 1.16 2006/03/20 19:41:37 torbenh Exp $
  25. *
  26. */
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include <memory.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <errno.h>
  33. #include <stdarg.h>
  34. #include <jack/types.h>
  35. #include <jack/engine.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <samplerate.h>
  40. #include "net_driver.h"
  41. #include "netjack_packet.h"
  42. int fraggo = 0;
  43. packet_cache *global_packcache;
  44. void
  45. packet_header_hton (jacknet_packet_header *pkthdr)
  46. {
  47. pkthdr->capture_channels_audio = htonl(pkthdr->capture_channels_audio);
  48. pkthdr->playback_channels_audio = htonl(pkthdr->playback_channels_audio);
  49. pkthdr->capture_channels_midi = htonl(pkthdr->capture_channels_midi);
  50. pkthdr->playback_channels_midi = htonl(pkthdr->playback_channels_midi);
  51. pkthdr->period_size = htonl(pkthdr->period_size);
  52. pkthdr->sample_rate = htonl(pkthdr->sample_rate);
  53. pkthdr->sync_state = htonl(pkthdr->sync_state);
  54. pkthdr->transport_frame = htonl(pkthdr->transport_frame);
  55. pkthdr->transport_state = htonl(pkthdr->transport_state);
  56. pkthdr->framecnt = htonl(pkthdr->framecnt);
  57. pkthdr->latency = htonl(pkthdr->latency);
  58. pkthdr->reply_port = htonl(pkthdr->reply_port);
  59. pkthdr->mtu = htonl(pkthdr->mtu);
  60. pkthdr->fragment_nr = htonl(pkthdr->fragment_nr);
  61. }
  62. void
  63. packet_header_ntoh (jacknet_packet_header *pkthdr)
  64. {
  65. pkthdr->capture_channels_audio = ntohl(pkthdr->capture_channels_audio);
  66. pkthdr->playback_channels_audio = ntohl(pkthdr->playback_channels_audio);
  67. pkthdr->capture_channels_midi = ntohl(pkthdr->capture_channels_midi);
  68. pkthdr->playback_channels_midi = ntohl(pkthdr->playback_channels_midi);
  69. pkthdr->period_size = ntohl(pkthdr->period_size);
  70. pkthdr->sample_rate = ntohl(pkthdr->sample_rate);
  71. pkthdr->sync_state = ntohl(pkthdr->sync_state);
  72. pkthdr->transport_frame = ntohl(pkthdr->transport_frame);
  73. pkthdr->transport_state = ntohl(pkthdr->transport_state);
  74. pkthdr->framecnt = ntohl(pkthdr->framecnt);
  75. pkthdr->latency = ntohl(pkthdr->latency);
  76. pkthdr->reply_port = ntohl(pkthdr->reply_port);
  77. pkthdr->mtu = ntohl(pkthdr->mtu);
  78. pkthdr->fragment_nr = ntohl(pkthdr->fragment_nr);
  79. }
  80. int get_sample_size (int bitdepth)
  81. {
  82. if (bitdepth == 8)
  83. return sizeof (int8_t);
  84. if (bitdepth == 16)
  85. return sizeof (int16_t);
  86. return sizeof (int32_t);
  87. }
  88. // fragment management functions.
  89. packet_cache
  90. *packet_cache_new (int num_packets, int pkt_size, int mtu)
  91. {
  92. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  93. int fragment_number = (pkt_size - sizeof (jacknet_packet_header) - 1) / fragment_payload_size + 1;
  94. int i;
  95. packet_cache *pcache = malloc (sizeof (packet_cache));
  96. if (pcache == NULL)
  97. {
  98. jack_error ("could not allocate packet cache (1)\n");
  99. return NULL;
  100. }
  101. pcache->size = num_packets;
  102. pcache->packets = malloc (sizeof (cache_packet) * num_packets);
  103. if (pcache->packets == NULL)
  104. {
  105. jack_error ("could not allocate packet cache (2)\n");
  106. return NULL;
  107. }
  108. for (i = 0; i < num_packets; i++)
  109. {
  110. pcache->packets[i].valid = 0;
  111. pcache->packets[i].num_fragments = fragment_number;
  112. pcache->packets[i].packet_size = pkt_size;
  113. pcache->packets[i].mtu = mtu;
  114. pcache->packets[i].framecnt = 0;
  115. pcache->packets[i].fragment_array = malloc (sizeof (char) * fragment_number);
  116. pcache->packets[i].packet_buf = malloc (pkt_size);
  117. if ((pcache->packets[i].fragment_array == NULL) || (pcache->packets[i].packet_buf == NULL))
  118. {
  119. jack_error ("could not allocate packet cache (3)\n");
  120. return NULL;
  121. }
  122. }
  123. return pcache;
  124. }
  125. void
  126. packet_cache_free (packet_cache *pcache)
  127. {
  128. int i;
  129. for (i = 0; i < pcache->size; i++)
  130. {
  131. free (pcache->packets[i].fragment_array);
  132. free (pcache->packets[i].packet_buf);
  133. }
  134. free (pcache->packets);
  135. free (pcache);
  136. }
  137. cache_packet
  138. *packet_cache_get_packet (packet_cache *pcache, jack_nframes_t framecnt)
  139. {
  140. int i;
  141. cache_packet *retval;
  142. for (i = 0; i < pcache->size; i++)
  143. {
  144. if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt))
  145. return &(pcache->packets[i]);
  146. }
  147. // The Packet is not in the packet cache.
  148. // find a free packet.
  149. retval = packet_cache_get_free_packet (pcache);
  150. if (retval != NULL)
  151. {
  152. cache_packet_set_framecnt (retval, framecnt);
  153. return retval;
  154. }
  155. // No Free Packet available
  156. // Get The Oldest packet and reset it.
  157. retval = packet_cache_get_oldest_packet (pcache);
  158. cache_packet_reset (retval);
  159. cache_packet_set_framecnt (retval, framecnt);
  160. return retval;
  161. }
  162. cache_packet
  163. *packet_cache_get_oldest_packet (packet_cache *pcache)
  164. {
  165. jack_nframes_t minimal_frame = 0;
  166. cache_packet *retval = &(pcache->packets[0]);
  167. int i;
  168. for (i = 0; i < pcache->size; i++)
  169. {
  170. if (pcache->packets[i].valid && (pcache->packets[i].framecnt < minimal_frame))
  171. {
  172. minimal_frame = pcache->packets[i].framecnt;
  173. retval = &(pcache->packets[i]);
  174. }
  175. }
  176. return retval;
  177. }
  178. cache_packet
  179. *packet_cache_get_free_packet (packet_cache *pcache)
  180. {
  181. int i;
  182. for (i = 0; i < pcache->size; i++)
  183. {
  184. if (pcache->packets[i].valid == 0)
  185. return &(pcache->packets[i]);
  186. }
  187. return NULL;
  188. }
  189. void
  190. cache_packet_reset (cache_packet *pack)
  191. {
  192. int i;
  193. pack->valid = 0;
  194. // XXX: i dont think this is necessary here...
  195. // fragement array is cleared in _set_framecnt()
  196. for (i = 0; i < pack->num_fragments; i++)
  197. pack->fragment_array[i] = 0;
  198. }
  199. void
  200. cache_packet_set_framecnt (cache_packet *pack, jack_nframes_t framecnt)
  201. {
  202. int i;
  203. pack->framecnt = framecnt;
  204. for (i = 0; i < pack->num_fragments; i++)
  205. pack->fragment_array[i] = 0;
  206. pack->valid = 1;
  207. }
  208. void
  209. cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
  210. {
  211. jacknet_packet_header *pkthdr = (jacknet_packet_header *) packet_buf;
  212. int fragment_payload_size = pack->mtu - sizeof (jacknet_packet_header);
  213. char *packet_bufX = pack->packet_buf + sizeof (jacknet_packet_header);
  214. char *dataX = packet_buf + sizeof (jacknet_packet_header);
  215. jack_nframes_t fragment_nr = ntohl (pkthdr->fragment_nr);
  216. jack_nframes_t framecnt = ntohl (pkthdr->framecnt);
  217. if (framecnt != pack->framecnt)
  218. {
  219. jack_error ("errror. framecnts dont match\n");
  220. return;
  221. }
  222. if (fragment_nr == 0)
  223. {
  224. memcpy (pack->packet_buf, packet_buf, pack->mtu);
  225. pack->fragment_array[0] = 1;
  226. return;
  227. }
  228. if ((fragment_nr < pack->num_fragments) && (fragment_nr > 0))
  229. {
  230. if ((fragment_nr * fragment_payload_size + rcv_len - sizeof (jacknet_packet_header)) <= (pack->packet_size - sizeof (jacknet_packet_header)))
  231. {
  232. memcpy (packet_bufX + fragment_nr * fragment_payload_size, dataX, rcv_len - sizeof (jacknet_packet_header));
  233. pack->fragment_array[fragment_nr] = 1;
  234. }
  235. else
  236. jack_error ("too long packet received...");
  237. }
  238. }
  239. int
  240. cache_packet_is_complete (cache_packet *pack)
  241. {
  242. int i;
  243. for (i = 0; i < pack->num_fragments; i++)
  244. if (pack->fragment_array[i] == 0)
  245. return FALSE;
  246. return TRUE;
  247. }
  248. // fragmented packet IO
  249. int
  250. netjack_recvfrom (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, socklen_t *addr_size, int mtu)
  251. {
  252. if (pkt_size <= mtu)
  253. return recvfrom (sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  254. char *rx_packet = alloca (mtu);
  255. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  256. int rcv_len;
  257. jack_nframes_t framecnt;
  258. cache_packet *cpack;
  259. do
  260. {
  261. rcv_len = recvfrom (sockfd, rx_packet, mtu, 0, addr, addr_size);
  262. if (rcv_len < 0)
  263. return rcv_len;
  264. framecnt = ntohl (pkthdr->framecnt);
  265. cpack = packet_cache_get_packet (global_packcache, framecnt);
  266. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  267. } while (!cache_packet_is_complete (cpack));
  268. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  269. cache_packet_reset (cpack);
  270. return pkt_size;
  271. }
  272. int
  273. netjack_recv (int sockfd, char *packet_buf, int pkt_size, int flags, int mtu)
  274. {
  275. if (pkt_size <= mtu)
  276. return recv (sockfd, packet_buf, pkt_size, flags);
  277. char *rx_packet = alloca (mtu);
  278. jacknet_packet_header *pkthdr = (jacknet_packet_header *) rx_packet;
  279. int rcv_len;
  280. jack_nframes_t framecnt;
  281. cache_packet *cpack;
  282. do
  283. {
  284. rcv_len = recv (sockfd, rx_packet, mtu, flags);
  285. if (rcv_len < 0)
  286. return rcv_len;
  287. framecnt = ntohl (pkthdr->framecnt);
  288. cpack = packet_cache_get_packet (global_packcache, framecnt);
  289. cache_packet_add_fragment (cpack, rx_packet, rcv_len);
  290. } while (!cache_packet_is_complete (cpack));
  291. memcpy (packet_buf, cpack->packet_buf, pkt_size);
  292. cache_packet_reset (cpack);
  293. return pkt_size;
  294. }
  295. void
  296. netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct sockaddr *addr, int addr_size, int mtu)
  297. {
  298. int frag_cnt = 0;
  299. char *tx_packet, *dataX;
  300. jacknet_packet_header *pkthdr;
  301. tx_packet = alloca (mtu + 10);
  302. dataX = tx_packet + sizeof (jacknet_packet_header);
  303. pkthdr = (jacknet_packet_header *) tx_packet;
  304. int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
  305. if (pkt_size <= mtu)
  306. sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
  307. else
  308. {
  309. // Copy the packet header to the tx pack first.
  310. memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
  311. // Now loop and send all
  312. char *packet_bufX = packet_buf + sizeof (jacknet_packet_header);
  313. while (packet_bufX < (packet_buf + pkt_size - fragment_payload_size))
  314. {
  315. pkthdr->fragment_nr = htonl (frag_cnt++);
  316. memcpy (dataX, packet_bufX, fragment_payload_size);
  317. sendto (sockfd, tx_packet, mtu, flags, addr, addr_size);
  318. packet_bufX += fragment_payload_size;
  319. }
  320. int last_payload_size = packet_buf + pkt_size - packet_bufX;
  321. memcpy (dataX, packet_bufX, last_payload_size);
  322. pkthdr->fragment_nr = htonl (frag_cnt);
  323. //jack_error("last fragment_count = %d, payload_size = %d\n", fragment_count, last_payload_size);
  324. // sendto(last_pack_size);
  325. sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
  326. }
  327. }
  328. void
  329. decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  330. {
  331. int i;
  332. jack_midi_clear_buffer (buf);
  333. for (i = 0; i < buffer_size_uint32 - 3;)
  334. {
  335. uint32_t payload_size;
  336. payload_size = buffer_uint32[i];
  337. payload_size = ntohl (payload_size);
  338. if (payload_size)
  339. {
  340. jack_midi_event_t event;
  341. event.time = ntohl (buffer_uint32[i+1]);
  342. event.size = ntohl (buffer_uint32[i+2]);
  343. event.buffer = (jack_midi_data_t*) (&(buffer_uint32[i+3]));
  344. jack_midi_event_write (buf, event.time, event.buffer, event.size);
  345. // skip to the next event
  346. unsigned int nb_data_quads = (((event.size-1) & ~0x3) >> 2)+1;
  347. i += 3+nb_data_quads;
  348. }
  349. else
  350. break; // no events can follow an empty event, we're done
  351. }
  352. }
  353. void
  354. encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
  355. {
  356. int i;
  357. unsigned int written = 0;
  358. // midi port, encode midi events
  359. unsigned int nevents = jack_midi_get_event_count (buf);
  360. for (i = 0; i < nevents; ++i)
  361. {
  362. jack_midi_event_t event;
  363. jack_midi_event_get (&event, buf, i);
  364. unsigned int nb_data_quads = (((event.size - 1) & ~0x3) >> 2) + 1;
  365. unsigned int payload_size = 3 + nb_data_quads;
  366. // only write if we have sufficient space for the event
  367. // otherwise drop it
  368. if (written + payload_size < buffer_size_uint32 - 1)
  369. {
  370. // write header
  371. buffer_uint32[written]=htonl (payload_size);
  372. written++;
  373. buffer_uint32[written]=htonl (event.time);
  374. written++;
  375. buffer_uint32[written]=htonl (event.size);
  376. written++;
  377. // write data
  378. jack_midi_data_t* tmpbuff = (jack_midi_data_t*)(&(buffer_uint32[written]));
  379. memcpy (tmpbuff, event.buffer, event.size);
  380. written += nb_data_quads;
  381. }
  382. else
  383. {
  384. // buffer overflow
  385. jack_error ("midi buffer overflow");
  386. break;
  387. }
  388. }
  389. // now put a netjack_midi 'no-payload' event, signaling EOF
  390. buffer_uint32[written]=0;
  391. }
  392. // render functions for float
  393. void
  394. render_payload_to_jack_ports_float ( void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  395. {
  396. channel_t chn = 0;
  397. JSList *node = capture_ports;
  398. JSList *src_node = capture_srcs;
  399. uint32_t *packet_bufX = (uint32_t *)packet_payload;
  400. while (node != NULL)
  401. {
  402. int i;
  403. int_float_t val;
  404. SRC_DATA src;
  405. jack_port_t *port = (jack_port_t *) node->data;
  406. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  407. const char *porttype = jack_port_type (port);
  408. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  409. {
  410. // audio port, resample if necessary
  411. if (net_period_down != nframes)
  412. {
  413. SRC_STATE *src_state = src_node->data;
  414. for (i = 0; i < net_period_down; i++)
  415. {
  416. packet_bufX[i] = ntohl (packet_bufX[i]);
  417. }
  418. src.data_in = (float *) packet_bufX;
  419. src.input_frames = net_period_down;
  420. src.data_out = buf;
  421. src.output_frames = nframes;
  422. src.src_ratio = (float) nframes / (float) net_period_down;
  423. src.end_of_input = 0;
  424. src_set_ratio (src_state, src.src_ratio);
  425. src_process (src_state, &src);
  426. src_node = jack_slist_next (src_node);
  427. }
  428. else
  429. {
  430. for (i = 0; i < net_period_down; i++)
  431. {
  432. val.i = packet_bufX[i];
  433. val.i = ntohl (val.i);
  434. buf[i] = val.f;
  435. }
  436. }
  437. }
  438. else if (strncmp (porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  439. {
  440. // midi port, decode midi events
  441. // convert the data buffer to a standard format (uint32_t based)
  442. unsigned int buffer_size_uint32 = net_period_down;
  443. uint32_t * buffer_uint32 = (uint32_t*)packet_bufX;
  444. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  445. }
  446. packet_bufX = (packet_bufX + net_period_down);
  447. node = jack_slist_next (node);
  448. chn++;
  449. }
  450. }
  451. void
  452. render_jack_ports_to_payload_float (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  453. {
  454. channel_t chn = 0;
  455. JSList *node = playback_ports;
  456. JSList *src_node = playback_srcs;
  457. uint32_t *packet_bufX = (uint32_t *) packet_payload;
  458. while (node != NULL)
  459. {
  460. SRC_DATA src;
  461. int i;
  462. int_float_t val;
  463. jack_port_t *port = (jack_port_t *) node->data;
  464. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  465. const char *porttype = jack_port_type (port);
  466. if (strncmp (porttype, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  467. {
  468. // audio port, resample if necessary
  469. if (net_period_up != nframes) {
  470. SRC_STATE *src_state = src_node->data;
  471. src.data_in = buf;
  472. src.input_frames = nframes;
  473. src.data_out = (float *) packet_bufX;
  474. src.output_frames = net_period_up;
  475. src.src_ratio = (float) net_period_up / (float) nframes;
  476. src.end_of_input = 0;
  477. src_set_ratio (src_state, src.src_ratio);
  478. src_process (src_state, &src);
  479. for (i = 0; i < net_period_up; i++)
  480. {
  481. packet_bufX[i] = htonl (packet_bufX[i]);
  482. }
  483. src_node = jack_slist_next (src_node);
  484. }
  485. else
  486. {
  487. for (i = 0; i < net_period_up; i++)
  488. {
  489. val.f = buf[i];
  490. val.i = htonl (val.i);
  491. packet_bufX[i] = val.i;
  492. }
  493. }
  494. }
  495. else if (strncmp(porttype, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  496. {
  497. // encode midi events from port to packet
  498. // convert the data buffer to a standard format (uint32_t based)
  499. unsigned int buffer_size_uint32 = net_period_up;
  500. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  501. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  502. }
  503. packet_bufX = (packet_bufX + net_period_up);
  504. node = jack_slist_next (node);
  505. chn++;
  506. }
  507. }
  508. // render functions for 16bit
  509. void
  510. render_payload_to_jack_ports_16bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  511. {
  512. channel_t chn = 0;
  513. JSList *node = capture_ports;
  514. JSList *src_node = capture_srcs;
  515. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  516. while (node != NULL)
  517. {
  518. int i;
  519. //uint32_t val;
  520. SRC_DATA src;
  521. jack_port_t *port = (jack_port_t *) node->data;
  522. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  523. float *floatbuf = alloca (sizeof(float) * net_period_down);
  524. const char *portname = jack_port_type (port);
  525. if (strncmp(portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  526. {
  527. // audio port, resample if necessary
  528. if (net_period_down != nframes)
  529. {
  530. SRC_STATE *src_state = src_node->data;
  531. for (i = 0; i < net_period_down; i++)
  532. {
  533. floatbuf[i] = ((float) ntohs(packet_bufX[i])) / 32767.0 - 1.0;
  534. }
  535. src.data_in = floatbuf;
  536. src.input_frames = net_period_down;
  537. src.data_out = buf;
  538. src.output_frames = nframes;
  539. src.src_ratio = (float) nframes / (float) net_period_down;
  540. src.end_of_input = 0;
  541. src_set_ratio (src_state, src.src_ratio);
  542. src_process (src_state, &src);
  543. src_node = jack_slist_next (src_node);
  544. }
  545. else
  546. for (i = 0; i < net_period_down; i++)
  547. buf[i] = ((float) ntohs (packet_bufX[i])) / 32768.0 - 1.0;
  548. }
  549. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  550. {
  551. // midi port, decode midi events
  552. // convert the data buffer to a standard format (uint32_t based)
  553. unsigned int buffer_size_uint32 = net_period_down / 2;
  554. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  555. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  556. }
  557. packet_bufX = (packet_bufX + net_period_down);
  558. node = jack_slist_next (node);
  559. chn++;
  560. }
  561. }
  562. void
  563. render_jack_ports_to_payload_16bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  564. {
  565. channel_t chn = 0;
  566. JSList *node = playback_ports;
  567. JSList *src_node = playback_srcs;
  568. uint16_t *packet_bufX = (uint16_t *)packet_payload;
  569. while (node != NULL)
  570. {
  571. SRC_DATA src;
  572. int i;
  573. jack_port_t *port = (jack_port_t *) node->data;
  574. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  575. const char *portname = jack_port_type (port);
  576. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  577. {
  578. // audio port, resample if necessary
  579. if (net_period_up != nframes)
  580. {
  581. SRC_STATE *src_state = src_node->data;
  582. float *floatbuf = alloca (sizeof(float) * net_period_up);
  583. src.data_in = buf;
  584. src.input_frames = nframes;
  585. src.data_out = floatbuf;
  586. src.output_frames = net_period_up;
  587. src.src_ratio = (float) net_period_up / (float) nframes;
  588. src.end_of_input = 0;
  589. src_set_ratio (src_state, src.src_ratio);
  590. src_process (src_state, &src);
  591. for (i = 0; i < net_period_up; i++)
  592. {
  593. packet_bufX[i] = htons ((floatbuf[i] + 1.0) * 32767.0);
  594. }
  595. src_node = jack_slist_next (src_node);
  596. }
  597. else
  598. for (i = 0; i < net_period_up; i++)
  599. packet_bufX[i] = htons((buf[i] + 1.0) * 32767.0);
  600. }
  601. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  602. {
  603. // encode midi events from port to packet
  604. // convert the data buffer to a standard format (uint32_t based)
  605. unsigned int buffer_size_uint32 = net_period_up / 2;
  606. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  607. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  608. }
  609. packet_bufX = (packet_bufX + net_period_up);
  610. node = jack_slist_next (node);
  611. chn++;
  612. }
  613. }
  614. // render functions for 8bit
  615. void
  616. render_payload_to_jack_ports_8bit (void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  617. {
  618. channel_t chn = 0;
  619. JSList *node = capture_ports;
  620. JSList *src_node = capture_srcs;
  621. int8_t *packet_bufX = (int8_t *)packet_payload;
  622. while (node != NULL)
  623. {
  624. int i;
  625. //uint32_t val;
  626. SRC_DATA src;
  627. jack_port_t *port = (jack_port_t *) node->data;
  628. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  629. float *floatbuf = alloca (sizeof (float) * net_period_down);
  630. const char *portname = jack_port_type (port);
  631. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  632. {
  633. // audio port, resample if necessary
  634. if (net_period_down != nframes)
  635. {
  636. SRC_STATE *src_state = src_node->data;
  637. for (i = 0; i < net_period_down; i++)
  638. floatbuf[i] = ((float) packet_bufX[i]) / 127.0;
  639. src.data_in = floatbuf;
  640. src.input_frames = net_period_down;
  641. src.data_out = buf;
  642. src.output_frames = nframes;
  643. src.src_ratio = (float) nframes / (float) net_period_down;
  644. src.end_of_input = 0;
  645. src_set_ratio (src_state, src.src_ratio);
  646. src_process (src_state, &src);
  647. src_node = jack_slist_next (src_node);
  648. }
  649. else
  650. for (i = 0; i < net_period_down; i++)
  651. buf[i] = ((float) packet_bufX[i]) / 127.0;
  652. }
  653. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  654. {
  655. // midi port, decode midi events
  656. // convert the data buffer to a standard format (uint32_t based)
  657. unsigned int buffer_size_uint32 = net_period_down / 2;
  658. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  659. decode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  660. }
  661. packet_bufX = (packet_bufX + net_period_down);
  662. node = jack_slist_next (node);
  663. chn++;
  664. }
  665. }
  666. void
  667. render_jack_ports_to_payload_8bit (JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  668. {
  669. channel_t chn = 0;
  670. JSList *node = playback_ports;
  671. JSList *src_node = playback_srcs;
  672. int8_t *packet_bufX = (int8_t *)packet_payload;
  673. while (node != NULL)
  674. {
  675. SRC_DATA src;
  676. int i;
  677. jack_port_t *port = (jack_port_t *) node->data;
  678. jack_default_audio_sample_t* buf = jack_port_get_buffer (port, nframes);
  679. const char *portname = jack_port_type (port);
  680. if (strncmp (portname, JACK_DEFAULT_AUDIO_TYPE, jack_port_type_size()) == 0)
  681. {
  682. // audio port, resample if necessary
  683. if (net_period_up != nframes)
  684. {
  685. SRC_STATE *src_state = src_node->data;
  686. float *floatbuf = alloca (sizeof (float) * net_period_up);
  687. src.data_in = buf;
  688. src.input_frames = nframes;
  689. src.data_out = floatbuf;
  690. src.output_frames = net_period_up;
  691. src.src_ratio = (float) net_period_up / (float) nframes;
  692. src.end_of_input = 0;
  693. src_set_ratio (src_state, src.src_ratio);
  694. src_process (src_state, &src);
  695. for (i = 0; i < net_period_up; i++)
  696. packet_bufX[i] = floatbuf[i] * 127.0;
  697. src_node = jack_slist_next (src_node);
  698. }
  699. else
  700. for (i = 0; i < net_period_up; i++)
  701. packet_bufX[i] = buf[i] * 127.0;
  702. }
  703. else if (strncmp(portname, JACK_DEFAULT_MIDI_TYPE, jack_port_type_size()) == 0)
  704. {
  705. // encode midi events from port to packet
  706. // convert the data buffer to a standard format (uint32_t based)
  707. unsigned int buffer_size_uint32 = net_period_up / 4;
  708. uint32_t * buffer_uint32 = (uint32_t*) packet_bufX;
  709. encode_midi_buffer (buffer_uint32, buffer_size_uint32, buf);
  710. }
  711. packet_bufX = (packet_bufX + net_period_up);
  712. node = jack_slist_next (node);
  713. chn++;
  714. }
  715. }
  716. /* Wrapper functions with bitdepth argument... */
  717. void
  718. render_payload_to_jack_ports (int bitdepth, void *packet_payload, jack_nframes_t net_period_down, JSList *capture_ports, JSList *capture_srcs, jack_nframes_t nframes)
  719. {
  720. if (bitdepth == 8)
  721. render_payload_to_jack_ports_8bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  722. else if (bitdepth == 16)
  723. render_payload_to_jack_ports_16bit (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  724. else
  725. render_payload_to_jack_ports_float (packet_payload, net_period_down, capture_ports, capture_srcs, nframes);
  726. }
  727. void
  728. render_jack_ports_to_payload (int bitdepth, JSList *playback_ports, JSList *playback_srcs, jack_nframes_t nframes, void *packet_payload, jack_nframes_t net_period_up)
  729. {
  730. if (bitdepth == 8)
  731. render_jack_ports_to_payload_8bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  732. else if (bitdepth == 16)
  733. render_jack_ports_to_payload_16bit (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  734. else
  735. render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up);
  736. }