File: fuse_loops.c

package info (click to toggle)
freebirth 0.3.2-9.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,632 kB
  • ctags: 640
  • sloc: ansic: 4,724; makefile: 89; sh: 1
file content (207 lines) | stat: -rw-r--r-- 6,248 bytes parent folder | download
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
/* Freebirth
 * Copyright (C) 1999 topher lafata <topher@topher.com>,
 *		      Jake Donham <jake@bitmechanic.com>
 *
 * 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 program 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 program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */



#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

#ifndef __SAMPLE_PRODUCER_H
  #include "sample_producer.h"
#endif

#ifndef __SEQ_H
 #include "sequencer.h"
#endif

extern seq *sequencer;

void topo_sort(sample_producer *node, GList **order) {
  if (g_list_find(*order, (gpointer)node))
    return;
  else {
    sample_producer **children;
    for (children = node->get_children(node);
	 *children;
	 children++) {
      topo_sort(*children, order);
    }
    *order = g_list_append(*order, (gpointer)node);
  }
}

void expand_seq(char **tmpl, char *prefix, FILE *file) {
  for (; *tmpl; tmpl++)
      fprintf(file, "%s%s\n", prefix, *tmpl);
}

void break_func(void) { }

void expand(sample_producer *node, char **tmpl, GList *order,
	    char *prefix, FILE *file) {
  sample_producer **children = node->get_children(node);
  int node_i = g_list_index(order, (gpointer)node);
  for (; *tmpl; tmpl++) {
    char buf[1024];
    char *sp = *tmpl, *dp = buf;
    dp += sprintf(dp, "%s", prefix);

    while (*sp) {
      if (*sp == '$') {
	switch (*++sp) {
	  char nbuf[8], *np;

	  /*
	    case '^':
	    break_func();
	    sp++;
	    break;
	  */

	case 'i':
	  np = nbuf;
	  while (isdigit(*++sp))
	    *np++ = *sp;
	  *np = '\0';
	  dp += sprintf(dp, "samp%d",
			g_list_index(order, (gpointer)children[atoi(nbuf)]));
	  break;

	case 'n':
	  dp += sprintf(dp, "n%d", node_i);
	  sp++;
	  break;

	case 'o':
	  dp += sprintf(dp, "samp%d", node_i);
	  sp++;
	  break;

	case 't':
	  dp += sprintf(dp, "sp[%d]", node_i);
	  sp++;
	  break;
	}
      }
      else
	*dp++ = *sp++;
    }
    *dp++ = '\n';
    *dp = '\0';
    fprintf(file, "%s", buf);
  }
}

void fuse_loops(sample_producer *left, sample_producer *right, FILE *file) {
  GList *order = NULL;
  GList *gl;
  int i, sampL, sampR;

  topo_sort(left, &order);
  topo_sort(right, &order);

  fprintf(file, "/* generated file -- don't edit */\n");
  fprintf(file, "#include <unistd.h>\n");
  fprintf(file, "#include <math.h>\n");
  fprintf(file, "#include <endian.h>\n");
  fprintf(file, "#include \"freebirth.h\"\n");
  fprintf(file, "/* borrowed from glib2 */\n");
  fprintf(file, "#define SHORT_SWAP_LE_BE(val)  ((short) ( \\\n");
  fprintf(file, "    (short) ((short) (val) >> 8) |     \\\n");
  fprintf(file, "    (short) ((short) (val) << 8)))\n");
  fprintf(file, "static void swap_endian(short *data, int length)\n");
  fprintf(file, "{\n");
  fprintf(file, "       int i;\n");
  fprintf(file, "       for (i = 0; i < length; i += 1, data++)\n");
  fprintf(file, "               *data = SHORT_SWAP_LE_BE(*data);\n");
  fprintf(file, "}\n");
  fprintf(file, "\n");

  fprintf(file, "sample_producer *sp[%d];\n", g_list_length(order));
  fprintf(file, "\n");

  fprintf(file, "int play_buffer(gpointer data, gint source, GdkInputCondition condition)\n");
  fprintf(file, "{\n");
  fprintf(file, "  int i;\n");
  fprintf(file, "  short buffer[TBASS_BUFF_SIZE * 2];\n");
  fprintf(file, "  \n");

  for (gl = order, i=0; gl; gl = g_list_next(gl), i++)
    fprintf(file, "  sample samp%d;\n", i);
  fprintf(file, "\n");

  expand_seq(seq_get_header(sequencer), "  ", file);
  fprintf(file, "\n");
  for (gl = order; gl; gl = g_list_next(gl)) {
    sample_producer *node = (sample_producer *)gl->data;
    expand(node, node->get_header(node), order, "  ", file);
    fprintf(file, "\n");
  }

  fprintf(file, "  for (i = 0; i < TBASS_BUFF_SIZE; i++) {\n");
  expand_seq(seq_get_code(sequencer), "    ", file);
  fprintf(file, "\n");
  for (gl = order; gl; gl = g_list_next(gl)) {
    sample_producer *node = (sample_producer *)gl->data;
    expand(node, node->get_code(node), order, "    ", file);
    if (g_list_next(gl)) fprintf(file, "\n");
  }

  sampL = g_list_index(order, (gpointer)left);
  sampR = g_list_index(order, (gpointer)right);
  fprintf(file, "    if ((samp%d < CLIP) && (samp%d >-CLIP))\n", sampL, sampL);
  fprintf(file, "      buffer[2 * i] = (short)samp%d;\n", sampL);
  fprintf(file, "    else if (samp%d > 0)\n", sampL);
  fprintf(file, "      buffer[2 * i] = (short)MIX_MAX_AMP-(CLIP_A/(CLIP_B+samp%d));\n", sampL);
  fprintf(file, "    else\n");
  fprintf(file, "      buffer[2 * i] = (short)-(MIX_MAX_AMP-(CLIP_A/(CLIP_B-samp%d)));\n", sampL);
  fprintf(file, "    if ((samp%d < CLIP) && (samp%d >-CLIP))\n", sampR, sampR);
  fprintf(file, "      buffer[2 * i + 1] = (short)samp%d;\n", sampR);
  fprintf(file, "    else if (samp%d > 0)\n", sampR);
  fprintf(file, "      buffer[2 * i + 1] = (short)MIX_MAX_AMP-(CLIP_A/(CLIP_B+samp%d));\n", sampR);
  fprintf(file, "    else\n");
  fprintf(file, "      buffer[2 * i + 1] = (short)-(MIX_MAX_AMP-(CLIP_A/(CLIP_B-samp%d)));\n", sampR);
  fprintf(file, "  }\n");
  fprintf(file, "\n");

  fprintf(file, "#if __BYTE_ORDER == __BIG_ENDIAN\n");
  fprintf(file, "  swap_endian(buffer, 2 * TBASS_BUFF_SIZE);\n");
  fprintf(file, "#endif\n");
  fprintf(file, "  write(fd, buffer, 2 * TBASS_BUFF_SIZE * sizeof(short));\n");
  fprintf(file, "\n");

  expand_seq(seq_get_footer(sequencer), "  ", file);
  fprintf(file, "\n");
  for (gl = order; gl; gl = g_list_next(gl)) {
    sample_producer *node = (sample_producer *)gl->data;
    expand(node, node->get_footer(node), order, "  ", file);
    fprintf(file, "\n");
  }
  fprintf(file, "  return 1;\n");
  fprintf(file, "}\n");
}

/* dummy so fusebirth links */
int play_buffer()
{
  return 0;
}