1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
|
/*
jacko.cpp:
Copyright (C) 2010nby Michael Gogins
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/**
* T H E J A C K O P C O D E S
* Michael Gogins
*
* The Jack opcodes can be used to connect any number
* of Csound instances, instruments, or user-defined
* opcodes to any number of Jack ports
* in any number of external Jack servers.
* Audio and MIDI signal types are supported
*
* Sending MIDI and/or audio to external Jack clients,
* such as synthesizers, and receiving audio and/or
* MIDI back from them, is supported.
*
* Receiving MIDI and/or audio from external Jack clients,
* such as sequencers, and sending MIDI and/or audio
* back to them, is supported.
*
* Other uses also are supported.
*
* O P C O D E S
*
* JackoInit -- Initializes Csound as a Jack client.
*
* Description
*
* Initializes this instance of Csound as a Jack client.
*
* Csound's sr must be equal to the Jack daemon's
* frames per second.
*
* Csound's ksmps must be equal to the Jack daemon's
* frames per period.
*
* Frames per period must not only (a) be a power of 2,
* but also (b) go evenly into the frames per second,
* e.g. 128 frames per period goes into 48000
* frames per second 375 times, for a latency or
* MIDI time granularity of about 2.7 milliseconds
* (as good as or better than the absolute best
* human performers).
*
* The order of processing of all signals that pass
* from Jack input ports, through Csound processing,
* and to Jack output ports, must be properly
* determined by sequence of instrument and
* opcode definition within the Csound orchestra.
*
* Syntax
*
* JackoInit SclientName, ServerName
*
* Initialization
*
* SclientName -- The name of the Jack client;
* normally, should be "csound".
*
* ServerName -- The name of the Jack daemon;
* normally, will be "default".
*
* This opcode must be called once and only once in the
* orchestra header, and before any other Jack opcodes.
* If more than one instance of Csound is using the Jack
* opcodes at the same time, then each instance of Csound
* must use a different client name.
*
*
* JackoInfo -- Prints information about the Jack system.
*
* Description
*
* Prints the Jack daemon and client names, the
* sampling rate and frames per period,
* and all active Jack port names,
* types, states, and connections.
*
* Syntax
*
* JackoInfo
*
* Initialization
*
* May be called any number of times in the orchestra header,
* for example both before and after creating Jack ports
* in the Csound orchestra header.
*
*
* JackoFreewheel -- Turns freewheeling mode on or off.
*
* Description
*
* Turns Jack's freewheeling mode on or off.
*
* When freewheeling is on, if supported by the rest
* of the Jack system, Csound will run as fast as possible,
* which may be either faster or slower than real time.
*
* This is essential for rendering scores that are too
* dense for real-time jacko_is_driving to a soundfile,
* without xruns or dropouts.
*
* Syntax
*
* JackoFreewheel [ienabled]
*
* Initialization
*
* ienabled -- Turns freewheeling on (the default) or off.
*
*
* JackoAudioInConnect -- Creates an audio connection
* from a Jack port to Csound.
*
* Description
*
* In the orchestra header, creates an audio connection
* from an external Jack audio output port to a
* Jack audio input port inside this instance of Csound.
*
* Syntax
*
* JackoAudioInConnect SexternalPortName, ScsoundPortName
*
* Initialization
*
* SexternalPortName -- The full name ("clientname:portname")
* of an external Jack audio output port.
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack audio input port.
*
* Performance
*
* The actual audio must be read with the JackoAudioIn opcode.
*
*
* JackoAudioOutConnect -- Creates an audio connection
* from Csound to a Jack port.
*
* Description
*
* In the orchestra header, creates an audio connection
* from a Jack audio output port inside this instance
* of Csound to an external Jack audio input port.
*
* Syntax
*
* JackoAudioOutConnect ScsoundPortName, SexternalPortName
*
* Initialization
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack audio input port.
*
* SexternalPortName -- The full name ("clientname:portname")
* of an external Jack audio output port.
*
* Performance
*
* The actual audio must be written with the JackoAudioOut
* opcode.
*
*
* JackoMidiInConnect -- Creates a MIDI connection from
* Jack to Csound.
*
* Description
*
* Creates a MIDI connection from an external Jack MIDI
* output port to this instance of Csound.
*
* Syntax
*
* JackoMidiInConnect SexternalPortName, ScsoundPortName
*
* Initialization
*
* SexternalPortName -- The full name ("clientname:portname")
* of an external Jack MIDI output port.
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack MIDI input port.
*
* Must be used in conjunction with the
* -M0 -+rtmidi=null Csound command-line options.
* Can be used in with the MIDI inter-operability
* command-line options and/or opcodes to enable the
* use of ordinary Csound instrument definitions to
* render external scores or MIDI sequences.
*
* Note that Csound can connect to ALSA ports through Jack,
* but in this case you will have to identify the port by
* its alias in the JackInfo printout.
*
* Performance
*
* The actual MIDI events will be received in the
* regular Csound way, i.e. through a MIDI driver
* and the sensevents mechanism, rather than through
* a Jack input port opcode.
*
* The granularity of timing is Csound's kperiod.
*
*
* JackoMidiOutConnect -- Creates a MIDI connection from
* Csound to Jack.
*
* Description
*
* In the orchestra header, creates a connection
* from a Jack MIDI output port inside this instance
* of Csound to an external Jack MIDI input port.
*
* Syntax
*
* JackoMidiOutConnect ScsoundPortName, SexternalPortName
*
* Initialization
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack audio input port.
*
* SexternalPortName -- The full name ("clientname:portname")
* of an external Jack audio output port.
*
* Performance
*
* The actual MIDI data must be written with the JackoMidiOut
* or JackoNoteOut opcodes.
*
*
* JackoOn -- Enables or disables all Jack ports.
*
* Description
*
* After all Jack connections have been created, enables
* or disables all Jack input and output opcodes
* inside this instance of Csound to read or write data.
*
* Syntax
*
* JackoOn [iactive]
*
* Initialization
*
* iactive -- A flag that turns the ports on (the default)
* or off.
*
*
* JackoAudioIn -- Receives an audio signal from a Jack port.
*
* Description
*
* Receives an audio signal from a Jack audio input port
* inside this instance of Csound, which in turn has
* received the signal from its connected external Jack
* audio output port.
*
* Syntax
*
* asignal JackoAudioIn ScsoundPortName
*
* Initialization
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack audio input port.
*
* Performance
*
* asignal -- Audio received from the external Jack
* output port to which ScsoundPortName is connected.
*
*
* JackoAudioOut -- Sends an audio signal to a Jack port.
*
* Description
*
* Sends an audio signal to an internal Jack audio
* output port, and in turn to its connected external
* Jack audio input port.
*
* Note that it is possible to send audio out via Jack
* to the system audio interface, while at the same time
* rendering to a regular Csound output soundfile.
*
* Syntax
*
* JackoAudioOut ScsoundPortName, asignal
*
* Initialization
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack audio output port.
*
* Performance
*
* asignal -- Audio to be sent to the external Jack audio
* input port to which CsoundPortName is connected.
*
* Audio from multiple instances of the opcode sending
* to the same Jack port is summed before sending.
*
*
* JackoMidiOut -- Sends a MIDI channel message to a
* Jack port.
*
* Description
*
* Sends a MIDI channel message to a Jack MIDI output port
* inside this instance of Csound, and in turn to its
* connected external Jack MIDI input port.
*
* Syntax
*
* JackoMidiOut ScsoundPortName, kstatus, kchannel, kdata1[, kdata2]
*
* Initialization
*
* ScsoundPortName -- The short name ("portname")
* of the internal Jack MIDI input port.
*
* Performance
*
* kstatus -- MIDI status byte; must indicate a MIDI channel
* message.
*
* kchannel -- MIDI channel (from 0 through 15).
*
* kdata1 -- First data byte of a MIDI channel message.
*
* kdata2 -- Optional second data byte of a MIDI channel message.
*
* This opcode can be called any number of times
* in the same kperiod. Messages from multiple instances
* of the opcode sending to the same port are collected
* before sending.
*
* Running status, system exclusive messages, and
* real-time messages are not supported.
*
* The granularity of timing is Csound's kperiod.
*
*
* JackoNoteOut -- Send one note to a Jack MIDI port.
*
* Description
*
* Sends one note to a Jack MIDI output port inside this
* instance of Csound, and in turn to the connected external
* Jack MIDI input port, with a duration
* specified by the score instrument statement.
* The matching MIDI note off message is generated
* and scheduled for later transmission.
*
* Notes from multiple instances of the opcode
* sending to the same output port are collected
* before sending.
*
* The granularity of timing is Csound's kperiod.
*
* Syntax
*
* JackoNoteOut ScsoundPortName, ichannel, ikey, ivelocity
*
* Initialization
*
* ScsoundPortName -- The short name ("portname") of the
* Jack MIDI output port created by jackmidioutconnect.
*
* ichannel -- The MIDI channel (from 0 through 15) to
* receive the note.
*
* ikey -- The MIDI key number (from 0 through 127, 60 is
* middle C) of the note.
*
* ivelocity -- The MIDI velocity number (from 0 through 127)
* of the note.
*
*
* JackoTransport -- Control the Jack transport.
*
* Description
*
* Starts, stops, or repositions the Jack transport.
* This is useful, e.g., for starting an external sequencer
* playing to send MIDI messages to Csound.
*
* Syntax
*
* JackoTransport kcommand, [kposition]
*
* Performance
*
* kcommand -- 0 means "no action", 1 starts the transport,
* 2 stops the transport, and 3 positions the transport
* to kposition seconds from the beginning of jacko_is_driving
* (i.e. time 0 in the score).
*
* This opcode can be used at init time or during jacko_is_driving.
*
* The granularity of timing is Csound's kperiod.
*
*
* I M P L E M E N T A T I O N
*
* Processing is done according to the callback model:
*
* 1. The jackinit opcode:
* 1.1. Creates a Jack client,
* which is associated with the
* running instance of Csound.
* 1.2. Registers a JackProcessCallback with Jack.
* 1.3. Registers a SenseEventCallback with Csound.
* 1.4. Installs a MIDI driver callback to consume
* MIDI events coming from Jack input ports.
* 1.5. Puts the Csound processing thread to sleep.
* 1.6. Activates the client.
* 2. The ports are created and connected in the orchestra header.
* 3. After all ports are connected, they are turned on in the
* orchestra header.
* 4. Every time the Jack callback fires:
* 4.1. Any MIDI events pending in the input ports are enqueued
* for dispatch via the MIDI driver callack through
* Csound's normal sensevents mechanism.
* 4.2. csoundPerformKsmps is called.
* 4.3. When the Csound jacko_is_driving is finished:
* 4.3.1. The Csound processing thread is re-awakened.
* 4.3.2. The Jack processing callback is deactivated.
* 4.3.2. The Jack client is closed.
* 5. At the end of processing, the module deinitialization
* function erases all Jack-related state.
*/
#include "OpcodeBase.hpp"
#include <atomic>
#include <csound.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <jack/jack.h>
#include <jack/midiport.h>
#include <list>
#include <map>
#include <pthread.h>
#include <set>
#include <string>
#include <vector>
using namespace csound;
struct JackoInit;
struct JackoInfo;
struct JackoFreewheel;
struct JackoAudioIn;
struct JackoAudioOut;
struct JackoMidiOut;
struct JackoNoteOut;
struct JackoTransport;
struct JackoState;
/**
* There is one and only one JackoState instance per instance of Csound.
* It is created by and only by the JackoInit opcode.
*/
// static JackoState *jackoState = 0;
static void SenseEventCallback_(CSOUND *csound, void *data);
static int JackProcessCallback_(jack_nframes_t frames, void *data);
static int midiDeviceOpen_(CSOUND *csound, void **userData,
const char *devName);
static int midiRead_(CSOUND *csound, void *userData, unsigned char *midiData,
int nbytes);
/**
* Manages all state relevant to the global
* interaction between Jack and Csound for a particular
* client and instance of Csound. The actual reading
* and writing of data is done by the input and output
* opcodes.
*/
struct JackoState {
CSOUND *csound;
const char *serverName;
const char *clientName;
jack_client_t *jackClient;
std::atomic<bool> jacko_is_driving;
std::atomic<bool> jackActive;
std::atomic<bool> is_closed;
jack_nframes_t csoundFramesPerTick;
jack_nframes_t jackFramesPerTick;
jack_nframes_t csoundFramesPerSecond;
jack_nframes_t jackFramesPerSecond;
jack_nframes_t jackFrameTime;
std::map<std::string, jack_port_t *> audioInPorts;
std::map<std::string, jack_port_t *> audioOutPorts;
std::map<std::string, jack_port_t *> midiInPorts;
std::map<std::string, jack_port_t *> midiOutPorts;
std::list<unsigned char> midiInputQueue;
jack_position_t jack_position;
pthread_t closeThread;
pthread_mutex_t csoundPerformanceThreadConditionMutex;
pthread_mutexattr_t csoundPerformanceThreadConditionMutexAttribute;
pthread_cond_t csoundPerformanceThreadCondition;
JackoState(CSOUND *csound_, const char *serverName_, const char *clientName_)
: csound(csound_), serverName(serverName_), clientName(clientName_),
jacko_is_driving(false), jackActive(false), is_closed(true) {
int result = 0;
csound = csound_;
csoundFramesPerTick = csound->GetKsmps(csound);
csoundFramesPerSecond = csound->GetSr(csound);
pthread_mutexattr_init(&csoundPerformanceThreadConditionMutexAttribute);
pthread_mutexattr_settype(&csoundPerformanceThreadConditionMutexAttribute,
PTHREAD_MUTEX_RECURSIVE);
result |=
pthread_mutex_init(&csoundPerformanceThreadConditionMutex,
&csoundPerformanceThreadConditionMutexAttribute);
result |= pthread_cond_init(&csoundPerformanceThreadCondition, 0);
std::memset(&jack_position, 0, sizeof(jack_position_t));
jack_options_t jack_options =
(jack_options_t)(JackServerName | JackNoStartServer | JackUseExactName);
jack_status_t status = jack_status_t(0);
jackClient =
jack_client_open(clientName, jack_options, &status, serverName);
if (!jackClient) {
csound->Message(csound,
Str("Could not create Jack client \"%s\" -- "
"is Jack server \"%s\" running? Status: %d\n"),
clientName, serverName, status);
csound->LongJmp(csound, 1);
} else {
csound->Message(
csound, Str("Created Jack client \"%s\" for Jack server \"%s\".\n"),
clientName, serverName);
}
jackFramesPerTick = jack_get_buffer_size(jackClient);
if (csoundFramesPerTick != jackFramesPerTick) {
csound->Message(
csound, Str("Jack buffer size %d != Csound ksmps %d, exiting...\n"),
jackFramesPerTick, csoundFramesPerTick);
csound->LongJmp(csound, 1);
}
jackFramesPerSecond = jack_get_sample_rate(jackClient);
if (csoundFramesPerSecond != jackFramesPerSecond) {
csound->Message(
csound, Str("Jack sampling rate %d != Csound sr %d, exiting...\n"),
jackFramesPerSecond, csoundFramesPerSecond);
csound->LongJmp(csound, 1);
}
csound->SetExternalMidiInOpenCallback(csound, midiDeviceOpen_);
csound->SetExternalMidiReadCallback(csound, midiRead_);
csound->RegisterSenseEventCallback(csound, SenseEventCallback_, this);
is_closed = false;
result |= jack_set_process_callback(jackClient, JackProcessCallback_, this);
result |= jack_activate(jackClient);
if (!result) {
csound->Message(csound, Str("Activated Jack client \"%s\".\n"),
jack_get_client_name(jackClient));
} else {
csound->Message(
csound, Str("Failed to activate Jack client \"%s\": status %d.\n"),
jack_get_client_name(jackClient), result);
return;
}
// jackInitialized = true;
}
~JackoState() {
}
int SenseEventCallback() {
int result = 0;
// Here we must wait once and only once, in order to put
// the original Csound processing thread to sleep --
// but we must NOT put the Jack processing callback
// to sleep when it comes here!
if (jacko_is_driving == false) {
// While Jack is processing, wait here.
// The Jack process callback will then call csoundPerformKsmps
// until the Csound performance is complete.
csound->Message(csound,
"%s", Str("Jacko is now driving Csound performance...\n"));
result |= pthread_mutex_lock(&csoundPerformanceThreadConditionMutex);
jacko_is_driving = true;
while (jacko_is_driving == true) {
result |= pthread_cond_wait(&csoundPerformanceThreadCondition,
&csoundPerformanceThreadConditionMutex);
}
result |= pthread_mutex_unlock(&csoundPerformanceThreadConditionMutex);
csound->Message(csound,
"%s", Str("Jacko has quit driving Csound performance.\n"));
return 1;
}
return result;
}
int JackProcessCallback(jack_nframes_t frames) {
IGN(frames);
int result = 0;
if (jacko_is_driving == false) {
return result;
}
// We must call PerformKsmps here ONLY after the original
// Csound jacko_is_driving thread is waiting on its condition.
jackFrameTime = jack_last_frame_time(jackClient);
if (jacko_is_driving == true) {
// Enqueue any MIDI messages pending in input ports.
for (std::map<std::string, jack_port_t *>::iterator it =
midiInPorts.begin();
it != midiInPorts.end(); ++it) {
jack_port_t *midiinport = it->second;
void *portbuffer = jack_port_get_buffer(midiinport, jackFramesPerTick);
if (portbuffer) {
jack_nframes_t eventN = jack_midi_get_event_count(portbuffer);
for (jack_nframes_t eventI = 0; eventI < eventN; ++eventI) {
jack_midi_event_t event;
int result = jack_midi_event_get(&event, portbuffer, eventI);
if (result == 0) {
for (size_t i = 0; i < event.size; ++i) {
midiInputQueue.push_back(event.buffer[i]);
}
}
}
}
}
// Clear MIDI output buffers.
for (std::map<std::string, jack_port_t *>::iterator it =
midiOutPorts.begin();
it != midiOutPorts.end(); ++it) {
void *buffer = jack_port_get_buffer(it->second, jackFramesPerTick);
jack_midi_clear_buffer(buffer);
}
int finished = csound->PerformKsmps(csound);
// We break here when the Csound performace is complete, and close
// the Jack connection in a separate thread.
if (finished /* && jackActive */) {
jacko_is_driving = false;
csound->Message(csound, "%s", Str("Jacko performance finished.\n"));
// Create a thread to run the close routine.
result = pthread_create(&closeThread, 0,
&JackoState::closeThreadRoutine_, this);
}
}
return result;
}
int close() {
csound->Message(csound, "%s", Str("JackoState::close...\n"));
jacko_is_driving = false;
int result = OK;
// Try not to do thread related operations more than once...
result = jack_deactivate(jackClient);
csound->Message(csound, "%s", Str("Jack client deactivated.\n"));
// Jack documentation says deactivating the client does the following also:
for (std::map<std::string, jack_port_t *>::iterator it =
audioInPorts.begin();
it != audioInPorts.end(); ++it) {
result = jack_port_unregister(jackClient, it->second);
}
for (std::map<std::string, jack_port_t *>::iterator it =
audioOutPorts.begin();
it != audioOutPorts.end(); ++it) {
result = jack_port_unregister(jackClient, it->second);
}
for (std::map<std::string, jack_port_t *>::iterator it =
midiInPorts.begin();
it != midiInPorts.end(); ++it) {
result = jack_port_unregister(jackClient, it->second);
}
for (std::map<std::string, jack_port_t *>::iterator it =
midiOutPorts.begin();
it != midiOutPorts.end(); ++it) {
result = jack_port_unregister(jackClient, it->second);
}
csound->Message(csound, "%s", Str("Jack ports unregistered.\n"));
result |= jack_client_close(jackClient);
csound->Message(csound, "%s", Str("Jack client closed.\n"));
pthread_cond_signal(&csoundPerformanceThreadCondition);
result |= pthread_cond_destroy(&csoundPerformanceThreadCondition);
result |= pthread_mutex_unlock(&csoundPerformanceThreadConditionMutex);
result |= pthread_mutex_destroy(&csoundPerformanceThreadConditionMutex);
audioOutPorts.clear();
audioInPorts.clear();
midiInPorts.clear();
midiOutPorts.clear();
is_closed = true;
csound->Message(csound, "%s", Str("JackoState::close.\n"));
return result;
}
void *closeThreadRoutine() {
int result = 0;
close();
void *result_ = 0;
memcpy(&result_, &result, std::min(sizeof(result), sizeof(result_)));
return result_;
}
static void *closeThreadRoutine_(void *userdata) {
return ((JackoState *)userdata)->closeThreadRoutine();
}
void startTransport() {
midiInputQueue.clear();
jack_transport_start(jackClient);
}
void stopTransport() { jack_transport_stop(jackClient); }
int positionTransport(double timeSeconds) {
int result = OK;
jack_position.frame_time = timeSeconds;
midiInputQueue.clear();
result = jack_transport_reposition(jackClient, &jack_position);
return result;
}
/**
* Return a MIDI output buffer,
* clearing it if not yet cleared for this tick.
*/
jack_midi_data_t *getMidiOutBuffer(jack_port_t *csoundPort) {
jack_midi_data_t *buffer = (jack_midi_data_t *)jack_port_get_buffer(
csoundPort, csoundFramesPerTick);
return buffer;
}
};
static int JackProcessCallback_(jack_nframes_t frames, void *data) {
return ((JackoState *)data)->JackProcessCallback(frames);
}
static void SenseEventCallback_(CSOUND *csound, void *data) {
IGN(csound);
((JackoState *)data)->SenseEventCallback();
}
static int midiDeviceOpen_(CSOUND *csound, void **userData,
const char *devName) {
IGN(devName);
JackoState *jackoState;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
*userData = jackoState;
return 0;
}
/**
* Dispatch any MIDI channel messages that have
* been read from Jack MIDI input ports.
*/
static int midiRead_(CSOUND *csound, void *userData, unsigned char *midiData,
int midiN) {
IGN(csound);
JackoState *jackoState_ = (JackoState *)userData;
if (jackoState_->is_closed == true) {
return 0;
}
int midiI = 0;
while (!jackoState_->midiInputQueue.empty() && midiI < midiN) {
midiData[midiI] = jackoState_->midiInputQueue.front();
jackoState_->midiInputQueue.pop_front();
midiI++;
}
// if (midiI) {
// csound->Message(csound, "midiRead_: %d bytes.\n", midiI);
//}
return midiI;
}
struct JackoInit : public OpcodeBase<JackoInit> {
STRINGDAT *ServerName;
STRINGDAT *SclientName;
const char *serverName;
const char *clientName;
int init(CSOUND *csound) {
serverName = csound->strarg2name(csound, (char *)0, ServerName->data,
(char *)"default", (int)1);
clientName = csound->strarg2name(csound, (char *)0, SclientName->data,
(char *)"csound", (int)1);
JackoState *jackoState = new JackoState(csound, serverName, clientName);
int result = csound::CreateGlobalPointer(csound, "jackoState", jackoState);
return result;
}
};
struct JackoInfo : public OpcodeBase<JackoInfo> {
int init(CSOUND *csound) {
JackoState *jackoState = 0;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
log(csound, "Jack information for client: %s\n", jackoState->clientName);
log(csound, " Daemon name: %s\n", jackoState->serverName);
log(csound, " Frames per second: %d\n",
jackoState->jackFramesPerSecond);
log(csound, " Frames per period: %d\n",
jackoState->jackFramesPerTick);
const char **ports = jack_get_ports(jackoState->jackClient, 0, 0, 0);
if (ports) {
log(csound, " Ports and connections:\n");
for (size_t i = 0; ports[i]; ++i) {
const char *PortName = ports[i];
jack_port_t *port = jack_port_by_name(jackoState->jackClient, PortName);
int flags = jack_port_flags(port);
const char *type = jack_port_type(port);
const char *portType = " ";
if ((flags & JackPortIsOutput) == JackPortIsOutput) {
portType = "Output";
} else if ((flags & JackPortIsInput) == JackPortIsInput) {
portType = "Input ";
}
log(csound, " %3d: %s %-25s %s\n", (i + 1), portType, type,
(PortName ? PortName : "(no name)"));
char alias1[0x100];
char alias2[0x100];
char *const aliases[2] = {alias1, alias2};
size_t aliasN = jack_port_get_aliases(port, aliases);
for (size_t aliasI = 0; aliasI < aliasN; ++aliasI) {
log(csound, " Alias: %s\n", aliases[aliasI]);
}
const char **connections =
jack_port_get_all_connections(jackoState->jackClient, port);
if (connections) {
for (size_t j = 0; connections[j]; ++j) {
if ((jack_port_flags(port) & JackPortIsOutput) ==
JackPortIsOutput) {
log(csound,
" Sends to: >> %s\n",
connections[j]);
} else {
log(csound,
" Receives from: << %s\n",
connections[j]);
}
}
}
std::free(connections);
}
std::free(ports);
}
return OK;
}
};
struct JackoFreewheel : public OpcodeBase<JackoFreewheel> {
MYFLT *ifreewheel;
int init(CSOUND *csound) {
JackoState *jackoState = 0;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int freewheel = (int)*ifreewheel;
int result = jack_set_freewheel(jackoState->jackClient, freewheel);
if (result) {
warn(csound,
Str("Failed to set Jack freewheeling mode to \"%s\": error %d.\n"),
(freewheel ? "on" : "off"), result);
} else {
log(csound, Str("Set Jack freewheeling mode to \"%s\".\n"),
(freewheel ? "on" : "off"));
}
return result;
}
};
struct JackoOn : public OpcodeBase<JackoOn> {
MYFLT *jon;
int init(CSOUND *csound) {
int result = OK;
JackoState *jackoState = 0;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
jackoState->jackActive = (char)*jon;
log(csound, Str("Turned Jack connections \"%s\".\n"),
(jackoState->jackActive ? "on" : "off"));
return result;
}
};
struct JackoAudioInConnect : public OpcodeBase<JackoAudioInConnect> {
// Out.
// Ins.
STRINGDAT *SexternalPortName;
STRINGDAT *ScsoundPortName;
// State.
const char *csoundPortName;
char csoundFullPortName[0x100];
const char *externalPortName;
const char *clientName;
jack_port_t *csoundPort;
jack_port_t *externalPort;
int init(CSOUND *csound) {
JackoState *jackoState = 0;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
clientName = jack_get_client_name(jackoState->jackClient);
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
std::sprintf(csoundFullPortName, "%s:%s", clientName, csoundPortName);
externalPortName = csound->strarg2name(
csound, (char *)0, SexternalPortName->data, (char *)"csound", (int)1);
csoundPort = jack_port_by_name(jackoState->jackClient, csoundFullPortName);
if (!csoundPort) {
csoundPort =
jack_port_register(jackoState->jackClient, csoundPortName,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
if (csoundPort) {
log(csound, "Created Jack port \"%s\".\n", csoundFullPortName);
} else {
warn(csound, Str("Could not create Jack port \"%s\".\n"),
csoundFullPortName);
}
}
externalPort = jack_port_by_name(jackoState->jackClient, externalPortName);
result = jack_connect(jackoState->jackClient, jack_port_name(externalPort),
jack_port_name(csoundPort));
if (result == EEXIST) {
log(csound, "Connection from \"%s\" to \"%s\" already exists.\n",
externalPortName, csoundFullPortName);
} else if (result) {
warn(csound,
Str("Could not create Jack connection from \"%s\" to \"%s\": "
"status %d.\n"),
externalPortName, csoundFullPortName, result);
return result;
} else {
log(csound, "Created Jack connection from \"%s\" to \"%s\".\n",
externalPortName, csoundFullPortName);
}
jackoState->audioInPorts[csoundPortName] = csoundPort;
return result;
}
};
struct JackoAudioIn : public OpcodeBase<JackoAudioIn> {
// Out.
MYFLT *asignal;
// Ins.
STRINGDAT *ScsoundPortName;
// State.
const char *csoundPortName;
jack_port_t *csoundPort;
jack_nframes_t csoundFramesPerTick;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
csoundFramesPerTick = jackoState->csoundFramesPerTick;
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
csoundPort = jackoState->audioInPorts[csoundPortName];
return result;
}
int audio(CSOUND *csound) {
IGN(csound);
if (jackoState->is_closed == true) {
return OK;
}
jack_default_audio_sample_t *buffer =
(jack_default_audio_sample_t *)jack_port_get_buffer(
csoundPort, csoundFramesPerTick);
for (size_t frame = 0; frame < csoundFramesPerTick; ++frame) {
asignal[frame] = buffer[frame];
}
return OK;
}
};
struct JackoAudioOutConnect : public OpcodeBase<JackoAudioOutConnect> {
// No outs.
// Ins.
STRINGDAT *ScsoundPortName;
STRINGDAT *SexternalPortName;
// State.
const char *csoundPortName;
char csoundFullPortName[0x100];
const char *externalPortName;
const char *clientName;
size_t frames;
jack_port_t *csoundPort;
jack_port_t *externalPort;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
frames = opds.insdshead->ksmps;
clientName = jack_get_client_name(jackoState->jackClient);
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
std::sprintf(csoundFullPortName, "%s:%s", clientName, csoundPortName);
externalPortName = csound->strarg2name(
csound, (char *)0, SexternalPortName->data, (char *)"csound", (int)1);
csoundPort = jack_port_by_name(jackoState->jackClient, csoundFullPortName);
if (!csoundPort) {
csoundPort =
jack_port_register(jackoState->jackClient, csoundPortName,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (csoundPort) {
log(csound, "Created Jack port \"%s\".\n", csoundFullPortName);
} else {
warn(csound, Str("Could not create Jack port \"%s\".\n"),
csoundFullPortName);
}
}
externalPort = jack_port_by_name(jackoState->jackClient, externalPortName);
result = jack_connect(jackoState->jackClient, jack_port_name(csoundPort),
jack_port_name(externalPort));
if (result == EEXIST) {
log(csound, "Connection from \"%s\" to \"%s\" already exists.\n",
csoundFullPortName, externalPortName);
} else if (result) {
warn(csound,
Str("Could not create Jack connection from \"%s\" to \"%s\": "
"status %d.\n"),
csoundFullPortName, externalPortName, result);
return result;
} else {
log(csound, "Created Jack connection from \"%s\" to \"%s\".\n",
csoundFullPortName, externalPortName);
}
jackoState->audioOutPorts[csoundPortName] = csoundPort;
return result;
}
};
struct JackoAudioOut : public OpcodeBase<JackoAudioOut> {
// No outs.
// Ins.
STRINGDAT *ScsoundPortName;
MYFLT *asignal;
// State.
const char *csoundPortName;
jack_port_t *csoundPort;
jack_nframes_t csoundFramesPerTick;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
csoundFramesPerTick = jackoState->csoundFramesPerTick;
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
csoundPort = jackoState->audioOutPorts[csoundPortName];
return result;
}
int audio(CSOUND *csound) {
IGN(csound);
if (jackoState->is_closed == true) {
return OK;
}
jack_default_audio_sample_t *buffer =
(jack_default_audio_sample_t *)jack_port_get_buffer(
csoundPort, csoundFramesPerTick);
for (size_t frame = 0; frame < csoundFramesPerTick; ++frame) {
buffer[frame] = asignal[frame];
}
return OK;
}
};
struct JackoMidiInConnect : public OpcodeBase<JackoMidiInConnect> {
// No outs.
// Ins.
STRINGDAT *SexternalPortName;
STRINGDAT *ScsoundPortName;
// State.
const char *csoundPortName;
char csoundFullPortName[0x100];
const char *externalPortName;
const char *clientName;
size_t frames;
jack_port_t *csoundPort;
jack_port_t *externalPort;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
frames = opds.insdshead->ksmps;
clientName = jack_get_client_name(jackoState->jackClient);
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
std::sprintf(csoundFullPortName, "%s:%s", clientName, csoundPortName);
externalPortName = csound->strarg2name(
csound, (char *)0, SexternalPortName->data, (char *)"csound", (int)1);
csoundPort = jack_port_by_name(jackoState->jackClient, csoundFullPortName);
if (!csoundPort) {
csoundPort =
jack_port_register(jackoState->jackClient, csoundPortName,
JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
if (csoundPort) {
log(csound, "Created Jack port \"%s\".\n", csoundFullPortName);
} else {
warn(csound, Str("Could not create Jack port \"%s\".\n"),
csoundFullPortName);
}
}
externalPort = jack_port_by_name(jackoState->jackClient, externalPortName);
result = jack_connect(jackoState->jackClient, jack_port_name(externalPort),
jack_port_name(csoundPort));
if (result == EEXIST) {
log(csound, "Connection from \"%s\" to \"%s\" already exists.\n",
externalPortName, csoundFullPortName);
} else if (result) {
warn(csound,
Str("Could not create Jack connection from \"%s\" to \"%s\": "
"status %d.\n"),
externalPortName, csoundFullPortName, result);
return result;
} else {
log(csound, "Created Jack connection from \"%s\" to \"%s\".\n",
externalPortName, csoundFullPortName);
}
jackoState->midiInPorts[csoundPortName] = csoundPort;
return result;
}
};
struct JackoMidiOutConnect : public OpcodeBase<JackoMidiOutConnect> {
// No outs.
// Ins.
STRINGDAT *ScsoundPortName;
STRINGDAT *SexternalPortName;
// State.
const char *csoundPortName;
char csoundFullPortName[0x100];
const char *externalPortName;
const char *clientName;
size_t frames;
jack_port_t *csoundPort;
jack_port_t *externalPort;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
frames = opds.insdshead->ksmps;
clientName = jack_get_client_name(jackoState->jackClient);
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
std::sprintf(csoundFullPortName, "%s:%s", clientName, csoundPortName);
externalPortName = csound->strarg2name(
csound, (char *)0, SexternalPortName->data, (char *)"csound", (int)1);
csoundPort = jack_port_by_name(jackoState->jackClient, csoundFullPortName);
if (!csoundPort) {
csoundPort =
jack_port_register(jackoState->jackClient, csoundPortName,
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (csoundPort) {
log(csound, "Created Jack port \"%s\".\n", csoundFullPortName);
} else {
warn(csound, Str("Could not create Jack port \"%s\".\n"),
csoundFullPortName);
}
}
externalPort = jack_port_by_name(jackoState->jackClient, externalPortName);
result = jack_connect(jackoState->jackClient, jack_port_name(csoundPort),
jack_port_name(externalPort));
if (result == EEXIST) {
log(csound, "Connection from \"%s\" to \"%s\" already exists.\n",
csoundFullPortName, externalPortName);
} else if (result) {
warn(csound,
Str("Could not create Jack connection from \"%s\" to \"%s\": "
"status %d.\n"),
csoundFullPortName, externalPortName, result);
return result;
} else {
log(csound, "Created Jack connection from \"%s\" to \"%s\".\n",
csoundFullPortName, externalPortName);
}
jackoState->midiOutPorts[csoundPortName] = csoundPort;
return result;
}
};
struct JackoMidiOut : public OpcodeBase<JackoMidiOut> {
// No outs.
// Ins.
STRINGDAT *ScsoundPortName;
MYFLT *kstatus;
MYFLT *kchannel;
MYFLT *kdata1;
MYFLT *kdata2;
char status;
char channel;
char data1;
char data2;
char priorstatus;
char priorchannel;
char priordata1;
char priordata2;
// State.
const char *csoundPortName;
jack_port_t *csoundPort;
jack_nframes_t csoundFramesPerTick;
jack_midi_data_t *buffer;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
csoundFramesPerTick = jackoState->csoundFramesPerTick;
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
csoundPort = jackoState->midiOutPorts[csoundPortName];
priorstatus = -1;
priorchannel = -1;
priordata1 = -1;
priordata2 = -1;
return result;
}
int kontrol(CSOUND *csound) {
IGN(csound);
if (jackoState->is_closed == true) {
return OK;
}
int result = OK;
status = *kstatus;
channel = *kchannel;
data1 = *kdata1;
data2 = *kdata2;
if (status != priorstatus || channel != priorchannel ||
data1 != priordata1 || data2 != priordata2) {
size_t dataSize = 0;
if (data2 == -1) {
dataSize = 2;
} else {
dataSize = 3;
}
buffer = jackoState->getMidiOutBuffer(csoundPort);
jack_midi_data_t *data = jack_midi_event_reserve(buffer, 0, dataSize);
data[0] = (status + channel);
data[1] = data1;
if (data2 != -1) {
data[2] = data2;
// log(csound, "MIDI: %3d %3d %3d\n", data[0], data[1], data[2]);
} else {
// log(csound, "MIDI: %3d %3d\n", data[0], data[1]);
}
}
priorstatus = status;
priorchannel = channel;
priordata1 = data1;
priordata2 = data2;
return result;
}
};
struct JackoNoteOut : public OpcodeNoteoffBase<JackoNoteOut> {
// No outs.
// Ins.
STRINGDAT *ScsoundPortName;
MYFLT *ichannel;
MYFLT *ikey;
MYFLT *ivelocity;
char status;
char channel;
char key;
char velocity;
// State.
const char *csoundPortName;
jack_port_t *csoundPort;
jack_nframes_t csoundFramesPerTick;
jack_midi_data_t *buffer;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
int result = OK;
csoundFramesPerTick = jackoState->csoundFramesPerTick;
csoundPortName = csound->strarg2name(
csound, (char *)0, ScsoundPortName->data, (char *)"", (int)1);
csoundPort = jackoState->midiOutPorts[csoundPortName];
status = (char)144;
channel = (char)*ichannel;
key = (char)*ikey;
velocity = (char)*ivelocity;
buffer = jackoState->getMidiOutBuffer(csoundPort);
jack_midi_data_t *data = jack_midi_event_reserve(buffer, 0, 3);
data[0] = (status + channel);
data[1] = key;
data[2] = velocity;
// log(csound, "noteon: %3d %3d %3d\n", data[0], data[1], data[2]);
return result;
}
int noteoff(CSOUND *csound) {
IGN(csound);
if (jackoState->is_closed == true) {
return OK;
}
int result = OK;
buffer = jackoState->getMidiOutBuffer(csoundPort);
jack_midi_data_t *data = jack_midi_event_reserve(buffer, 0, 3);
data[0] = (status + channel);
data[1] = key;
data[2] = 0;
// log(csound, "noteoff: %3d %3d %3d\n", data[0], data[1], data[2]);
return result;
}
};
struct JackoTransport : public OpcodeBase<JackoTransport> {
// Outs.
// Ins.
MYFLT *kcommand;
MYFLT *Oposition;
// State.
int command;
int priorCommand;
double positionSeconds;
double priorPositionSeconds;
JackoState *jackoState;
int init(CSOUND *csound) {
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
priorCommand = -1;
priorPositionSeconds = 0.0;
return kontrol(csound);
}
int kontrol(CSOUND *csound) {
if (jackoState->is_closed == true) {
return OK;
}
int result = OK;
command = int(*kcommand);
positionSeconds = double(*Oposition);
if (command) {
if (command != priorCommand) {
priorCommand = command;
switch (command) {
case 1:
result = jackoState->positionTransport(0.0);
jackoState->startTransport();
log(csound, "Started Jack transport.\n");
break;
case 2:
jackoState->stopTransport();
log(csound, "Stopped Jack transport.\n");
break;
case 3:
if (positionSeconds != priorPositionSeconds) {
priorPositionSeconds = positionSeconds;
result = jackoState->positionTransport(positionSeconds);
jackoState->startTransport();
if (result) {
log(csound, "Failed to start Jack transport at %f seconds with"
" result: %d\n",
positionSeconds, result);
} else {
log(csound, "Started Jack transport at %f seconds.\n",
positionSeconds);
}
}
break;
};
}
}
return result;
}
};
extern "C" {
static OENTRY oentries[] = {
{(char *)"JackoInit", sizeof(JackoInit), 0, 1, (char *)"", (char *)"SS",
(SUBR)&JackoInit::init_, 0, 0},
{(char *)"JackoInfo", sizeof(JackoInfo), 0, 1, (char *)"", (char *)"",
(SUBR)&JackoInfo::init_, 0, 0},
{(char *)"JackoFreewheel", sizeof(JackoFreewheel), 0, 1, (char *)"",
(char *)"i", (SUBR)&JackoFreewheel::init_, 0, 0},
{(char *)"JackoOn", sizeof(JackoOn), 0, 1, (char *)"", (char *)"j",
(SUBR)&JackoOn::init_, 0, 0},
{
(char *)"JackoAudioInConnect", sizeof(JackoAudioInConnect), 0, 1,
(char *)"", (char *)"SS", (SUBR)&JackoAudioInConnect::init_, 0, 0,
},
{
(char *)"JackoAudioIn", sizeof(JackoAudioIn), 0, 3, (char *)"a",
(char *)"S", (SUBR)&JackoAudioIn::init_, (SUBR)&JackoAudioIn::audio_,
},
{
(char *)"JackoAudioOutConnect", sizeof(JackoAudioOutConnect), 0, 1,
(char *)"", (char *)"SS", (SUBR)&JackoAudioOutConnect::init_, 0, 0,
},
{
(char *)"JackoAudioOut", sizeof(JackoAudioOut), 0, 3, (char *)"",
(char *)"Sa", (SUBR)&JackoAudioOut::init_,
(SUBR)&JackoAudioOut::audio_,
},
{
(char *)"JackoMidiInConnect", sizeof(JackoMidiInConnect), 0, 1,
(char *)"", (char *)"SS", (SUBR)&JackoMidiInConnect::init_, 0, 0,
},
{
(char *)"JackoMidiOutConnect", sizeof(JackoMidiOutConnect), 0, 1,
(char *)"", (char *)"SS", (SUBR)&JackoMidiOutConnect::init_, 0, 0,
},
{
(char *)"JackoMidiOut", sizeof(JackoMidiOut), 0, 3, (char *)"",
(char *)"SkkkO", (SUBR)&JackoMidiOut::init_,
(SUBR)&JackoMidiOut::kontrol_, 0,
},
{(char *)"JackoNoteOut", sizeof(JackoNoteOut), 0, 3, (char *)"",
(char *)"Siii", (SUBR)&JackoNoteOut::init_, (SUBR)&JackoNoteOut::kontrol_,
0},
{(char *)"JackoTransport", sizeof(JackoTransport), 0, 3, (char *)"",
(char *)"kO", // O defaults to 0.
(SUBR)&JackoTransport::init_, (SUBR)&JackoTransport::kontrol_, 0},
{0, 0, 0, 0, 0, 0, (SUBR)0, (SUBR)0, (SUBR)0}};
PUBLIC int csoundModuleCreate(CSOUND *csound) {
IGN(csound);
return 0;
}
PUBLIC int csoundModuleInit(CSOUND *csound) {
OENTRY *ep = (OENTRY *)&(oentries[0]);
int err = 0;
while (ep->opname != 0) {
err |= csound->AppendOpcode(csound, ep->opname, ep->dsblksiz, ep->flags,
ep->thread, ep->outypes, ep->intypes,
(int (*)(CSOUND *, void *))ep->iopadr,
(int (*)(CSOUND *, void *))ep->kopadr,
(int (*)(CSOUND *, void *))ep->aopadr);
ep++;
}
return err;
}
PUBLIC int csoundModuleDestroy(CSOUND *csound) {
if (csound->GetDebug(csound)) {
csound->Message(csound, "jacko: csoundModuleDestroy(%p)...\n",
csound);
}
int result = OK;
JackoState *jackoState = 0;
csound::QueryGlobalPointer(csound, "jackoState", jackoState);
if (jackoState) {
if (jackoState->is_closed == false) {
jackoState->close();
}
delete jackoState;
jackoState = 0;
}
if (csound->GetDebug(csound)) {
csound->Message(csound, "jacko: csoundModuleDestroy(%p).\n",
csound);
}
return result;
}
}
|