midisine.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {
  2. Copyright (C) 2004 Ian Esten
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. }
  15. program midisine;
  16. {$MODE objfpc}{$H+}
  17. uses
  18. CTypes, Math, SysUtils, Jack, JackMidiPort;
  19. var
  20. input_port: Pjack_port_t;
  21. output_port: Pjack_port_t;
  22. ramp: jack_default_audio_sample_t = 0.0;
  23. note_on: jack_default_audio_sample_t;
  24. note: cuchar = 0;
  25. note_frqs: array [0..127] of jack_default_audio_sample_t;
  26. procedure calc_note_frqs(srate: jack_default_audio_sample_t);
  27. var
  28. i: cint;
  29. begin
  30. for i := 0 to 127 do
  31. note_frqs[i] := (2.0 * 440.0 / 32.0) * Power(2, ((jack_default_audio_sample_t(i) - 9.0) / 12.0)) / srate;
  32. end;
  33. function process(nframes: jack_nframes_t; arg: Pointer): cint; cdecl;
  34. var
  35. i: cint;
  36. port_buf: Pointer;
  37. _out: Pjack_default_audio_sample_t;
  38. in_event: jack_midi_event_t;
  39. event_index: jack_nframes_t;
  40. event_count: jack_nframes_t;
  41. begin
  42. port_buf := jack_port_get_buffer(input_port, nframes);
  43. _out := jack_port_get_buffer (output_port, nframes);
  44. event_index := 0;
  45. event_count := jack_midi_get_event_count(port_buf);
  46. if event_count > 1 then
  47. begin
  48. Writeln(' midisine: have ', event_count, ' events');
  49. for i := 0 to event_count - 1 do
  50. begin
  51. jack_midi_event_get(@in_event, port_buf, i);
  52. Writeln(' event ', i, ' time is ', in_event.time, '. 1st byte is $', HexStr(in_event.buffer^, 2));
  53. end;
  54. {printf("1st byte of 1st event addr is %p\n", in_events[0].buffer);}
  55. end;
  56. jack_midi_event_get(@in_event, port_buf, 0);
  57. for i := 0 to nframes - 1 do
  58. begin
  59. if (in_event.time = i) and (event_index < event_count) then
  60. begin
  61. if (in_event.buffer^ and $f0) = $90 then
  62. begin
  63. { note on }
  64. note := (in_event.buffer + 1)^;
  65. note_on := 1.0;
  66. end
  67. else if (in_event.buffer^ and $f0) = $80 then
  68. begin
  69. { note off }
  70. note := (in_event.buffer + 1)^;
  71. note_on := 0.0;
  72. end;
  73. Inc(event_index);
  74. if event_index < event_count then
  75. jack_midi_event_get(@in_event, port_buf, event_index);
  76. end;
  77. ramp += note_frqs[note];
  78. if ramp > 1.0 then
  79. ramp -= 2.0;
  80. _out[i] := note_on*sin(2*PI*ramp);
  81. end;
  82. Result := 0;
  83. end;
  84. function srate(nframes: jack_nframes_t; arg: Pointer): cint; cdecl;
  85. begin
  86. Writeln('the sample rate is now ', nframes, '/sec');
  87. calc_note_frqs(jack_default_audio_sample_t(nframes));
  88. Result := 0;
  89. end;
  90. procedure jack_shutdown(arg: Pointer); cdecl;
  91. begin
  92. Halt(1);
  93. end;
  94. var
  95. client: Pjack_client_t;
  96. begin
  97. client := jack_client_open ('midisine', JackNullOption, nil);
  98. if client = nil then
  99. begin
  100. Writeln(StdErr, 'jack server not running?');
  101. Halt(1);
  102. end;
  103. calc_note_frqs(jack_get_sample_rate (client));
  104. jack_set_process_callback (client, @process, nil);
  105. jack_set_sample_rate_callback (client, @srate, nil);
  106. jack_on_shutdown (client, @jack_shutdown, nil);
  107. input_port := jack_port_register (client, 'midi_in', JACK_DEFAULT_MIDI_TYPE, Ord(JackPortIsInput), 0);
  108. output_port := jack_port_register (client, 'audio_out', JACK_DEFAULT_AUDIO_TYPE, Ord(JackPortIsOutput), 0);
  109. if jack_activate (client) <> 0 then
  110. begin
  111. Writeln(StdErr, 'cannot activate client');
  112. Halt(1);
  113. end;
  114. { run until interrupted }
  115. while True do
  116. Sleep(1000);
  117. jack_client_close(client);
  118. Halt (0);
  119. end.