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.

131 lines
3.2 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #include <jack/jack.h>
  2. #include <jack/midiport.h>
  3. #include "cvin.h"
  4. void CVIn::shutdown()
  5. {
  6. }
  7. int CVIn::process(jack_nframes_t nframes)
  8. {
  9. if (!m_ready) return 0;
  10. void *midi_out = jack_port_get_buffer(m_midi_out, nframes);
  11. jack_midi_clear_buffer(midi_out);
  12. for (unsigned i = 0; i < m_mapping_list.size(); i++) {
  13. m_buffers[i] = static_cast<sample_t *>(jack_port_get_buffer(m_ports[i], nframes));
  14. }
  15. for (jack_nframes_t f = 0; f < nframes; f++) {
  16. int port = 0;
  17. MappingList::iterator it;
  18. for (it = m_it_begin; it != m_it_end; ++it, port++) {
  19. Mapping *m = &(*it);
  20. jack_midi_data_t buf[3];
  21. m->cur_mv = m->to_mv(*m_buffers[port]++);
  22. if (m->tick_cvin()) {
  23. switch (m->type) {
  24. case TYPE_PB:
  25. buf[0] = 0xE0 | m->channel;
  26. buf[1] = m->last_mv >> 7;
  27. buf[2] = m->last_mv & 0x7F;
  28. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  29. break;
  30. case TYPE_CC:
  31. buf[0] = 0xB0 | m->channel;
  32. buf[1] = m->ccmsb;
  33. if (m->has_lsb) {
  34. buf[2] = m->last_mv >> 7;
  35. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  36. buf[1] = m->cclsb;
  37. }
  38. buf[2] = m->last_mv & 0x7F;
  39. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  40. break;
  41. case TYPE_NRPN:
  42. buf[0] = 0xB0 | m->channel;
  43. buf[1] = 99;
  44. buf[2] = m->ccmsb;
  45. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  46. buf[1] = 98;
  47. buf[2] = m->cclsb;
  48. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  49. buf[1] = 6;
  50. if (m->has_lsb) {
  51. buf[2] = m->last_mv >> 7;
  52. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  53. buf[1] = 38;
  54. }
  55. buf[2] = m->last_mv & 0x7F;
  56. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  57. break;
  58. case TYPE_RPN:
  59. buf[0] = 0xB0 | m->channel;
  60. buf[1] = 101;
  61. buf[2] = m->ccmsb;
  62. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  63. buf[1] = 100;
  64. buf[2] = m->cclsb;
  65. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  66. buf[1] = 6;
  67. if (m->has_lsb) {
  68. buf[2] = m->last_mv >> 7;
  69. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  70. buf[1] = 38;
  71. }
  72. buf[2] = m->last_mv & 0x7F;
  73. jack_midi_event_write(midi_out, f, buf, sizeof buf);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. return 0;
  80. }
  81. void CVIn::start()
  82. {
  83. m_ready = false;
  84. m_tick = 0;
  85. if (m_mapping_list.size() == 0) return;
  86. m_ports = new jack_port_t *[m_mapping_list.size()];
  87. m_buffers = new sample_t *[m_mapping_list.size()];
  88. if (!open(m_config_name.empty() ? "m2cv_in" : m_config_name.c_str())) exit(EXIT_FAILURE);
  89. m_midi_out = port_register("midi_out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  90. MappingList::iterator it_begin = m_mapping_list.begin();
  91. MappingList::iterator it_end = m_mapping_list.end();
  92. int port = 0;
  93. MappingList::iterator it;
  94. for (it = it_begin; it != it_end; ++it, port++) {
  95. Mapping *m = &(*it);
  96. m_ports[port] = port_register(m->name.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  97. m->latency_ticks = m_sample_rate * m->latency / 1000.0f;
  98. }
  99. m_it_end = it_end;
  100. m_it_begin = it_begin;
  101. m_ready = true;
  102. }
  103. void CVIn::stop()
  104. {
  105. if (m_mapping_list.size() == 0) return;
  106. close();
  107. delete [] m_ports;
  108. delete [] m_buffers;
  109. }