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
|
/* gretl - The Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2004 Allin Cottrell
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* gretl audio graph plugin */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libgretl.h"
#include "miditypes.h"
#include "midi_utils.h"
#include <glib.h>
#include <gtk/gtk.h>
#if GLIB_CHECK_VERSION(2,0,0)
# define GLIB2
#else
# include <unistd.h>
# include <signal.h>
# include <wait.h>
# include <errno.h>
#endif
#undef DEBUG
#ifdef HAVE_FLITE
# include <flite/flite.h>
extern cst_voice *register_cmu_us_kal (void);
#endif
#ifdef G_OS_WIN32
# define COBJMACROS
# include <sapi.h>
#endif
#if defined(HAVE_FLITE) || defined(G_OS_WIN32)
# define DO_SPEECH
# include "libgretl.h"
#endif
const char *track_hdr = "MTrk";
enum dataset_comments {
TS_COMMENT = 0,
YLABEL_COMMENT,
XLABEL_COMMENT,
XRANGE_COMMENT,
N_COMMENTS
};
#define DEFAULT_FORCE 96 /* volume of MIDI notes */
#ifndef G_OS_WIN32
# define DEFAULT_MIDI_PROG "timidity" /* MIDI player to be used */
# define DEFAULT_MIDI_OPT "-A500" /* option to pass to MIDI player */
#endif
typedef struct _datapoint datapoint;
typedef struct _dataset dataset;
typedef struct _midi_spec midi_spec;
typedef struct _midi_track midi_track;
typedef struct _note_event note_event;
struct _datapoint {
double x;
double y;
};
struct _dataset {
int pd;
int n;
int series2;
double intercept;
double slope;
datapoint *points;
double *y2;
gchar *comments[N_COMMENTS];
};
struct _midi_spec {
int ntracks;
int nticks;
int nsecs;
dataset *dset;
FILE *fp;
};
struct _midi_track {
unsigned char channel;
unsigned char patch;
int n_notes;
note_event *notes;
};
struct _note_event {
double dtime;
double duration;
unsigned char pitch;
unsigned char force;
unsigned char channel;
};
#define delta_time_zero(f) (putc(0x00, f))
#define YMAX 60
static int write_note_event (note_event *event, midi_spec *spec)
{
int len;
len = delta_time(event->dtime, spec->nticks, spec->fp);
putc(MIDI_NOTE_ON + event->channel, spec->fp);
putc(event->pitch, spec->fp);
putc(event->force, spec->fp);
len += 3;
/* use "running status" */
len += delta_time(event->duration, spec->nticks, spec->fp);
putc(event->pitch, spec->fp);
putc(0, spec->fp);
len += 2;
return len;
}
static void write_midi_track (midi_track *track,
midi_spec *spec)
{
char tmp[32];
long len = 0;
unsigned char bal = 0x7f * (track->channel % 2);
int i, n;
fwrite(track_hdr, 1, 4, spec->fp);
write_be_long(0, spec->fp); /* revisited below */
delta_time_zero(spec->fp);
len++;
sprintf(tmp, "Series %d", track->channel + 1);
n = strlen(tmp);
len += write_midi_meta(MIDI_TRACK_NAME, spec->fp);
len += write_var_len(n, spec->fp);
for (i=0; i<n; i++) {
putc(tmp[i], spec->fp);
}
len += n;
delta_time_zero(spec->fp);
len++;
strcpy(tmp, get_patch_name(track->patch));
n = strlen(tmp);
len += write_midi_meta(MIDI_INSTR_NAME, spec->fp);
len += write_var_len(n, spec->fp);
for (i=0; i<n; i++) {
putc(tmp[i], spec->fp);
}
len += n;
/* Stereo separation of series */
delta_time_zero(spec->fp);
fputc(0xb0 + track->channel, spec->fp); /* Controller on channel */
fputc(MIDI_PAN_MSB, spec->fp);
fputc(bal, spec->fp); /* MSB = 0 or 0x7f */
delta_time_zero(spec->fp);
fputc(0xb0 + track->channel, spec->fp);
fputc(MIDI_PAN_LSB, spec->fp);
fputc(bal, spec->fp); /* LSB = 0 or 0x7f */
len += 8;
delta_time_zero(spec->fp);
len++;
len += write_midi_byte(MIDI_PROGRAM_CHANGE + track->channel, spec->fp);
len += write_midi_byte(track->patch, spec->fp);
for (i=0; i<track->n_notes; i++) {
track->notes[i].channel = track->channel;
len += write_note_event(&track->notes[i], spec);
}
delta_time_zero(spec->fp);
len++;
len += write_midi_eot(spec->fp);
fseek(spec->fp, -(len + 4), SEEK_CUR);
/* now fix the length */
write_be_long(len, spec->fp);
fseek(spec->fp, len, SEEK_CUR);
}
static void four_four_header (midi_spec *spec)
{
int ticklen, len = 0;
long pos, pos1;
long min_note_time = 60000;
long max_note_time = 180000;
write_midi_header(1, spec->ntracks + 1, spec->nticks, spec->fp);
/* tempo/time track */
fwrite(track_hdr, 1, 4, spec->fp);
pos = ftell(spec->fp);
write_be_long(0, spec->fp); /* revisited below */
/* time sig */
delta_time_zero(spec->fp);
len++;
len += write_midi_meta(MIDI_TIME_SIG, spec->fp);
/* size (bytes) */
len += write_midi_byte(4, spec->fp);
/* 4/4 time */
len += write_midi_byte(4, spec->fp);
len += write_midi_byte(2, spec->fp);
/* 24 MIDI clocks per metronome click */
len += write_midi_byte(24, spec->fp);
/* 8 32nd notes per tick */
len += write_midi_byte(8, spec->fp);
/* tempo */
delta_time_zero(spec->fp);
len++;
len += write_midi_meta(MIDI_TEMPO, spec->fp);
/* bytes */
len += write_midi_byte(3, spec->fp);
/* microseconds per quarter note */
if (spec->dset == NULL) {
len += write_be_24(500000, spec->fp);
} else {
double nms = spec->nsecs * 1.0e6;
long msq = nms / spec->dset->n;
if (msq > max_note_time) {
msq = max_note_time;
} else if (msq < min_note_time) {
msq = min_note_time;
}
len += write_be_24(msq, spec->fp);
}
/* end */
if (spec->dset == NULL) {
ticklen = 4 * spec->nticks;
} else {
ticklen = spec->dset->n * spec->nticks;
}
len += write_var_len(ticklen, spec->fp);
len += write_midi_eot(spec->fp);
/* length of header */
pos1 = ftell(spec->fp);
fseek(spec->fp, pos, SEEK_SET);
write_be_long(len, spec->fp);
fseek(spec->fp, pos1, SEEK_SET);
}
static void dataset_init (dataset *dset)
{
int i;
dset->points = NULL;
dset->y2 = NULL;
dset->n = 0;
dset->pd = 0;
dset->series2 = 0;
dset->intercept = NADBL;
dset->slope = NADBL;
for (i=0; i<N_COMMENTS; i++) {
dset->comments[i] = NULL;
}
}
static void dataset_free (dataset *dset)
{
int i;
free(dset->points);
if (dset->y2 != NULL) {
free(dset->y2);
}
for (i=0; i<N_COMMENTS; i++) {
if (dset->comments[i] != NULL) {
g_free(dset->comments[i]);
}
}
}
const char *cent_str (int cent)
{
switch (cent) {
case 14:
return "fourteen";
case 15:
return "fifteen";
case 16:
return "sixteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "nineteen";
case 20:
return "two thousand";
default:
return "unknown";
}
}
static int make_xrange_comment (const char *line, dataset *dset)
{
double x1, x2;
if (sscanf(line, "set xrange [%lf:%lf]", &x1, &x2) != 2) {
return 0;
}
if (dset->pd == 0) {
dset->comments[XRANGE_COMMENT] =
g_strdup_printf("x range %.4g to %.4g", x1, x2);
} else {
char tmp1[64], tmp2[64];
int ix1 = x1;
int ix2 = x2;
int cent, yr;
cent = x1 / 100;
yr = ix1 - 100 * cent;
sprintf(tmp1, "%s %d", cent_str(cent), yr);
cent = x2 / 100;
yr = ix2 - 100 * cent;
sprintf(tmp2, "%s %d", cent_str(cent), yr);
dset->comments[XRANGE_COMMENT] =
g_strdup_printf("%s to %s", tmp1, tmp2);
}
return 1;
}
static int make_axis_label_comment (const char *line, dataset *dset)
{
char tmp[16];
int ret = 0;
if (sscanf(line, "set ylabel '%15[^']", tmp) == 1) {
if (dset->pd > 0) {
dset->comments[YLABEL_COMMENT] = g_strdup(tmp);
} else {
dset->comments[YLABEL_COMMENT] =
g_strdup_printf("y variable %s", tmp);
}
ret = 1;
} else if (sscanf(line, "set xlabel '%15[^']", tmp) == 1) {
if (dset->pd > 0) {
dset->comments[XLABEL_COMMENT] = g_strdup(tmp);
} else {
dset->comments[XLABEL_COMMENT] =
g_strdup_printf("x variable %s", tmp);
}
ret = 1;
}
return ret;
}
static void make_ts_comment (const char *line, dataset *dset)
{
const char *pdstr = NULL;
sscanf(line, "# timeseries %d", &dset->pd);
switch (dset->pd) {
case 1:
pdstr = "Annual";
break;
case 4:
pdstr = "Quarterly";
break;
case 5:
case 7:
pdstr = "Daily";
break;
case 12:
pdstr = "Monthly";
break;
case 24:
pdstr = "Hourly";
break;
default:
break;
}
if (pdstr != NULL) {
dset->comments[TS_COMMENT] =
g_strdup_printf("%s time series", pdstr);
} else {
dset->comments[TS_COMMENT] = g_strdup("time series");
}
}
static int get_comment (const char *line, dataset *dset)
{
int ret = 0;
if (!strncmp(line, "# timeseries", 12)) {
make_ts_comment(line, dset);
ret = 1;
}
else if (!strncmp(line, "set ylabel", 10) ||
!strncmp(line, "set xlabel", 10)) {
make_axis_label_comment(line, dset);
ret = 1;
}
else if (!strncmp(line, "set xrange", 10)) {
make_xrange_comment(line, dset);
ret = 1;
}
return ret;
}
#ifdef PLAY_AUTOFIT_LINE
static int get_fit_params (char *line, dataset *dset)
{
double a, b;
char *p = strchr(line, '*');
if (p != NULL) *p = '\0';
if (sscanf(line, "%lf + %lf", &a, &b) == 2) {
dset->intercept = a;
dset->slope = b;
dset->series2 = 1;
return 1;
}
return 0;
}
#endif
static int get_data_x_y (const char *line, double *x, double *y)
{
if (sscanf(line, "%lf %lf", x, y) == 2) return 0;
if (sscanf(line, "%lf ?", x) == 1) {
*y = NADBL;
return 0;
}
return 1;
}
static int read_datafile (const char *fname, dataset *dset)
{
char line[256];
int i, err = 0;
int got_e = 0, y2data = 0, fitline = 0;
FILE *fdat;
dataset_init(dset);
fdat = gretl_fopen(fname, "r");
if (fdat == NULL) {
fprintf(stderr, "Couldn't open '%s'\n", fname);
return 1;
} else {
fprintf(stderr, "Reading %s...\n", fname);
}
while (fgets(line, sizeof line, fdat)) {
tailstrip(line);
if (get_comment(line, dset)) {
continue;
} else if (!strcmp(line, "e")) {
fprintf(stderr, "Got end of data marker\n");
got_e++;
if (got_e == 2) {
/* can't handle more than two series! */
break;
}
} else if (strstr(line, "automatic OLS")) {
fitline = 1;
} else if (isdigit((unsigned char) line[0])) {
if (strstr(line, "title")) {
#ifdef PLAY_AUTOFIT_LINE
if (fitline) {
get_fit_params(line, dset);
}
#endif
continue;
}
if (!got_e) {
dset->n += 1;
} else if (!y2data) {
y2data = 1;
}
}
}
if (dset->n == 0) {
fprintf(stderr, "No data in '%s'\n", fname);
err = 1;
goto bailout;
}
dset->points = malloc(dset->n * sizeof *dset->points);
if (dset->points == NULL) {
err = 1;
fputs("Out of memory\n", stderr);
goto bailout;
}
if (y2data) {
dset->y2 = malloc(dset->n * sizeof *dset->y2);
if (dset->y2 == NULL) {
err = 1;
fputs("Out of memory\n", stderr);
goto bailout;
}
dset->series2 = 1;
}
rewind(fdat);
i = got_e = 0;
while (!err && fgets(line, 256, fdat)) {
tailstrip(line);
if (!strcmp(line, "e")) {
got_e++;
if (got_e == 2) {
break;
}
i = 0;
} else if (isdigit((unsigned char) line[0])) {
double x, y;
if (strstr(line, "title")) {
continue;
}
if (get_data_x_y(line, &x, &y)) {
fprintf(stderr, "Couldn't read data on line %d\n", i + 1);
err = 1;
} else {
if (!got_e) {
dset->points[i].x = x;
dset->points[i].y = y;
} else {
dset->y2[i] = y;
}
}
i++;
}
}
bailout:
fclose(fdat);
if (err) {
dataset_free(dset);
}
return err;
}
static void points_min_max (const datapoint *points,
double *xmin, double *xmax,
double *ymin, double *ymax,
int n)
{
int i;
*xmin = *xmax = points[0].x;
*ymin = *ymax = points[0].y;
for (i=1; i<n; i++) {
if (points[i].x < *xmin) *xmin = points[i].x;
if (points[i].x > *xmax) *xmax = points[i].x;
if (!na(points[i].y)) {
if (points[i].y < *ymin) *ymin = points[i].y;
if (points[i].y > *ymax) *ymax = points[i].y;
}
}
}
static void min_max (const double *x, double *min, double *max,
int n)
{
int i;
*min = *max = x[0];
for (i=1; i<n; i++) {
if (!na(x[i])) {
if (x[i] < *min) *min = x[i];
if (x[i] > *max) *max = x[i];
}
}
}
#if defined(HAVE_FLITE)
#if 0
static int save_dataset_comments (const dataset *dset)
{
int i, j;
cst_voice *v;
cst_wave *w, *fullw = NULL;
flite_init();
v = register_cmu_us_kal();
j = 0;
for (i=0; i<N_COMMENTS; i++) {
if (dset->comments[i] != NULL) {
if (j == 0) {
fullw = flite_text_to_wave(dset->comments[i], v);
} else {
w = flite_text_to_wave(dset->comments[i], v);
concat_wave(fullw, w);
delete_wave(w);
}
j++;
}
}
cst_wave_save_riff(fullw, "gretl_flite.wav");
delete_wave(fullw);
return 0;
}
#endif
static void speak_dataset_comments (const dataset *dset)
{
int i;
cst_voice *v;
flite_init();
v = register_cmu_us_kal();
for (i=0; i<N_COMMENTS; i++) {
if (dset->comments[i] != NULL) {
flite_text_to_speech(dset->comments[i], v, "play");
}
}
}
#elif defined(G_OS_WIN32)
static WCHAR *wide_string (const char *s)
{
unsigned char *ret;
int i, j, n = strlen(s);
ret = malloc(2 * (n + 1));
if (ret == NULL) return NULL;
j = 0;
for (i=0; i<=n; i++) {
ret[j++] = s[i];
ret[j++] = '\0';
}
return (WCHAR *) ret;
}
static void speak_dataset_comments (const dataset *dset)
{
int i;
ISpVoice *v = NULL;
HRESULT hr;
hr = CoInitialize(NULL);
if (!SUCCEEDED(hr)) {
fprintf(stderr, "CoInitialize failed\n");
return;
}
hr = CoCreateInstance(&CLSID_SpVoice,
NULL,
CLSCTX_ALL,
&IID_ISpVoice,
(void **) &v);
if (SUCCEEDED(hr)) {
for (i=0; i<N_COMMENTS; i++) {
if (dset->comments[i] != NULL) {
WCHAR *w = wide_string(dset->comments[i]);
ISpVoice_Speak(v, w, 0, NULL);
free(w);
}
}
ISpVoice_Release(v);
}
CoUninitialize();
}
#endif
static void print_dataset_comments (const dataset *dset)
{
int i;
for (i=0; i<N_COMMENTS; i++) {
if (dset->comments[i] != NULL) {
printf("Comment[%d]: %s\n", i+1, dset->comments[i]);
}
}
#ifdef DO_SPEECH
speak_dataset_comments(dset);
#endif
}
static void audio_graph_error (const char *msg)
{
#ifdef HAVE_FLITE
cst_voice *v;
#endif
#ifdef G_OS_WIN32
ISpVoice *v = NULL;
HRESULT hr;
#endif
fprintf(stderr, "%s\n", msg);
#ifdef HAVE_FLITE
flite_init();
v = register_cmu_us_kal();
flite_text_to_speech(msg, v, "play");
#endif
#ifdef G_OS_WIN32
hr = CoInitialize(NULL);
if (!SUCCEEDED(hr)) return;
hr = CoCreateInstance(&CLSID_SpVoice,
NULL,
CLSCTX_ALL,
&IID_ISpVoice,
(void **) &v);
if (SUCCEEDED(hr)) {
wchar_t *w = wide_string(msg);
ISpVoice_Speak(v, w, 0, NULL);
free(w);
ISpVoice_Release(v);
}
#endif
}
static int compare_points (const void *a, const void *b)
{
const datapoint *pa = (const datapoint *) a;
const datapoint *pb = (const datapoint *) b;
return (pa->x > pb->x) - (pa->x < pb->x);
}
static int play_dataset (midi_spec *spec, midi_track *track,
const dataset *dset)
{
double xmin, xmax, xavg;
double ymin, ymax;
double xscale, yscale;
int i;
track->notes = malloc(dset->n * sizeof *track->notes);
if (track->notes == NULL) {
fputs("out of memory\n", stderr);
return 1;
}
track->channel = 0;
track->patch = PC_GRAND;
track->n_notes = dset->n;
if (dset->pd == 0) {
/* scatter plot: sort data by x value */
qsort((void *) dset->points, (size_t) dset->n,
sizeof dset->points[0], compare_points);
}
points_min_max(dset->points, &xmin, &xmax, &ymin, &ymax, dset->n);
if (dset->y2 != NULL) {
double y2min, y2max;
min_max(dset->y2, &y2min, &y2max, dset->n);
if (y2min < ymin) ymin = y2min;
if (y2max > ymax) ymax = y2max;
}
xavg = (xmax - xmin) / dset->n;
/* normalize average x step to quarter note */
xscale = 1.0 / xavg;
yscale = YMAX / (ymax - ymin);
for (i=0; i<dset->n; i++) {
double dtx, dux, ypos;
if (!na(dset->points[i].y)) {
ypos = (dset->points[i].y - ymin) * yscale;
} else {
ypos = NADBL;
}
if (i == 0) {
dtx = 0.0;
} else {
dtx = xscale * (dset->points[i].x - dset->points[i-1].x);
}
if (i == dset->n - 1) {
dux = xscale * xavg;
} else {
dux = xscale * (dset->points[i+1].x - dset->points[i].x);
}
track->notes[i].dtime = dtx;
track->notes[i].duration = dux;
if (!na(ypos)) {
track->notes[i].pitch = 36 + (int) (ypos + 0.5);
track->notes[i].force = DEFAULT_FORCE;
} else {
track->notes[i].pitch = 36;
track->notes[i].force = 0;
}
#ifdef DEBUG
fprintf(stderr, "Obs %d: x = %g, y = %g, ypos = %g\n", i,
dset->points[i].x, dset->points[i].y, ypos);
fprintf(stderr, " dtime=%g, duration=%g, pitch=%d\n",
track->notes[i].dtime, track->notes[i].duration,
track->notes[i].pitch);
#endif
}
write_midi_track(track, spec);
if (dset->series2) {
for (i=0; i<dset->n; i++) {
double yi, ypos;
if (dset->y2 != NULL) {
yi = dset->y2[i];
} else {
yi = dset->intercept + dset->slope * dset->points[i].x;
}
if (!na(yi)) {
ypos = (yi - ymin) * yscale;
track->notes[i].pitch = 36 + (int) (ypos + 0.5);
track->notes[i].force = DEFAULT_FORCE;
} else {
track->notes[i].pitch = 36;
track->notes[i].force = 0;
}
#ifdef DEBUG
fprintf(stderr, "Series2, Obs %d: x = %g, y = %g, ypos = %g\n", i,
dset->points[i].x, yi, ypos);
fprintf(stderr, " dtime=%g, duration=%g, pitch=%d\n",
track->notes[i].dtime, track->notes[i].duration,
track->notes[i].pitch);
#endif
}
track->channel = 1;
track->patch = PC_MARIMBA;
write_midi_track(track, spec);
}
free(track->notes);
return 0;
}
#if defined(G_OS_WIN32)
static int midi_fork (const char *fname, const char *midiplayer)
{
int err = 0;
if ((long) ShellExecute(NULL, "open", fname, NULL, NULL, SW_SHOW) < 32)
err = 1;
return err;
}
#else /* non-Windows versions */
static char **get_midi_args (int *argc, const char *fname,
const char *midiplayer)
{
char **argv = NULL;
const char *midistr;
if (midiplayer != NULL && *midiplayer != '\0') {
midistr = midiplayer;
} else {
midistr = (const char *) getenv("GRETL_MIDI_PLAYER");
}
if (midistr == NULL || *midistr == '\0') {
*argc = 3;
argv = malloc((*argc + 1) * sizeof *argv);
if (argv == NULL) return NULL;
argv[0] = g_strdup(DEFAULT_MIDI_PROG);
argv[1] = g_strdup(DEFAULT_MIDI_OPT);
argv[2] = g_strdup(fname);
argv[3] = NULL;
} else {
char *tmp = g_strdup(midistr);
char *s = tmp;
int i, ntoks = 1;
while (*s) {
if (*s == ' ') ntoks++;
s++;
}
*argc = ntoks + 1;
argv = malloc((*argc + 1) * sizeof *argv);
if (argv == NULL) {
g_free(tmp);
return NULL;
}
if (ntoks > 1) {
argv[0] = g_strdup(strtok(tmp, " "));
for (i=1; i<ntoks; i++) {
argv[i] = g_strdup(strtok(NULL, " "));
}
} else {
argv[0] = g_strdup(tmp);
i = 1;
}
argv[i++] = g_strdup(fname);
argv[i] = NULL;
g_free(tmp);
}
return argv;
}
# ifdef GLIB2
static int real_midi_fork (char **argv)
{
gboolean run;
run = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
NULL, NULL, NULL, NULL);
return !run;
}
# else
static volatile int fork_err;
static void fork_err_set (int signum)
{
fork_err = 1;
}
static int real_midi_fork (char **argv)
{
pid_t pid;
fork_err = 0;
signal(SIGUSR1, fork_err_set);
pid = fork();
if (pid == -1) {
perror("fork");
return 1;
} else if (pid == 0) {
execvp(argv[0], argv);
perror("execlp");
kill(getppid(), SIGUSR1);
_exit(EXIT_FAILURE);
}
sleep(1);
if (fork_err) {
fprintf(stderr, "%s: %s", _("Command failed"), argv[0]);
}
return fork_err;
}
# endif
static int midi_fork (const char *fname, const char *midiplayer)
{
int i, err, argc;
char **argv;
argv = get_midi_args(&argc, fname, midiplayer);
if (argv == NULL) return 1;
err = real_midi_fork(argv);
for (i=0; i<argc; i++) {
g_free(argv[i]);
}
free(argv);
return err;
}
#endif
int midi_play_graph (const char *fname, const char *userdir,
const char *midiplayer)
{
char outname[FILENAME_MAX];
midi_spec spec;
midi_track track;
dataset dset;
sprintf(outname, "%sgretl.mid", userdir);
spec.fp = gretl_fopen(outname, "wb");
if (spec.fp == NULL) {
fprintf(stderr, "Couldn't write to '%s'\n", outname);
return 1;
}
if (read_datafile(fname, &dset)) {
audio_graph_error("Error reading data file");
fclose(spec.fp);
return 1;
}
spec.ntracks = 1 + dset.series2;
spec.nticks = 96;
spec.dset = &dset;
spec.nsecs = 16;
four_four_header(&spec);
print_dataset_comments(&dset);
play_dataset(&spec, &track, &dset);
dataset_free(&dset);
fclose(spec.fp);
midi_fork(outname, midiplayer);
return 0;
}
#ifdef DO_SPEECH
# include "audioprint.c"
#endif
|