File: nro.c

package info (click to toggle)
bristol 0.60.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,652 kB
  • sloc: ansic: 124,457; sh: 10,579; makefile: 111
file content (1341 lines) | stat: -rw-r--r-- 35,902 bytes parent folder | download | duplicates (3)
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

/*
 *  Diverse Bristol audio routines.
 *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
 *
 *
 *   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 3 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; if not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * These are intended to be non-resampling oscillators. They will use an 
 * algorithm to generate the samples and based on the algorithm they will be
 * correct, ie, no resampling will be required. There will be an 'adjustment'
 * at the start/end of each cycle where correction is needed to hit a frequency
 * since no frequency will be an integral fraction of a wave. This same point
 * will be used to seed the next full wave.
 *
 * There are only a few waveforms that are being targetted:
 *
 * 	sine
 * 	tri
 * 	square/pulse/pwm
 * 	saw
 * 	ramp
 *
 * The saw and ramp code is naturally the same however some of the code may 
 * use these as LFO. All of them should support sync, possibly excepting the
 * sine?
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "bristol.h"
#include "nro.h"

#define OPNAME "NRO-OSC"
#define OPDESCRIPTION "Non Resampling Digitally Controlled Oscillator"
#define PCOUNT 16 /* As of 0.30.6 this was the max */
#define IOCOUNT 5

/* IO indeces */
#define NRO_IN_IND 0
#define NRO_OUT_IND 1
#define NRO_PWM_IND 2
#define NRO_SYNC_IND 3
#define NRO_SYNC_OUT 4

/*
 * Param indices
 *
 * Tune fine +/- semitone
 * Tune coarse +/- 7 or 12 semitones
 * Transpose in notes
 * Detune
 *
 * 5 harmonics: 16/8/4/2/sub
 * 4 waves (sqr/saw/tri/sine)
 *
 * Pulse width
 *
 * Reload tendencies
 */
#define FINETUNE 0
#define COARSETUNE 1
#define TRANSPOSE 2
#define DETUNE 3

#define H16 4
#define H8 5
#define H4 6
#define H2 7
#define H32 8

#define SQUARE 9
#define SAW 10
#define TRI 11
#define SINE 12

#define PW 13
#define DRAIN 14

#define EMULATION 15

static float note_diff;

/* Detuning for the different tendencies, controlled by DETUNE */
static float dt[TENDENCY_COUNT] = {
 0.043,
 0.073,
 0.029,
 0.031,
 0.007,
 0.011,
 0.079,
 0.083,
 0.019,
 0.059,
 0.061,
 0.071,
 0.017,
 0.023,
 0.013,
 0.089,
 0.097, 
 0.047,
 0.053,
 0.037,
 0.0,
};

/*
 * Reset any local memory information.
 */
static int destroy(bristolOP *operator)
{
#ifdef BRISTOL_DBG
	printf("nrodestroy(%x)\n", operator);
#endif

	/*
	 * Unmalloc anything we added to this structure
	 */
	bristolfree(((bristolNRO *) operator)->zb);
	bristolfree(((bristolNRO *) operator)->fb);
	bristolfree(((bristolNRO *) operator)->fbm);
	bristolfree(((bristolNRO *) operator)->pwmb);
	bristolfree(((bristolNRO *) operator)->sb);

	bristolfree(operator->specs);

	/*
	 * Free any local memory. We should also free ourselves, since we did the
	 * initial allocation.
	 */
	cleanup(operator);
	return(0);
}

/*
 * Reset any local memory information.
 */
static int reset(bristolOP *operator, bristolOPParams *param)
{
#ifdef BRISTOL_DBG
	printf("nroreset(%x, %x)\n", operator, param);
#endif

	param->param[0].float_val = 1.0;
	param->param[1].float_val = 1.0;
	param->param[2].float_val = 0.0;
	param->param[3].float_val = 0.0;
	param->param[4].float_val = 1.0;
	param->param[5].float_val = 0.0;
	param->param[6].float_val = 0.0;
	param->param[7].float_val = 1.0;
	param->param[8].float_val = 1.0;
	param->param[9].float_val = 1.0;
	param->param[11].float_val = 0.0;
	param->param[12].float_val = 0.0;
	param->param[13].float_val = 0.5;
	param->param[14].float_val = 0.98;
	param->param[15].int_val = 0.0;

	return(0);
}

/*
 * Alter an internal parameter of an operator.
 */
static int param(bristolOP *operator, bristolOPParams *param,
	unsigned char index, float value)
{
#ifdef BRISTOL_DBG
	printf("	nroparam(%x, %x, %i, %x)\n", operator, param, index,
		((int) (value * CONTROLLER_RANGE)));
#endif

	switch (index) {
		case FINETUNE: /* Fine tuning +/- 1/2 tone */
			{
				float tune, notes = 1.0;

				tune = (value - 0.5) * 2;

				/*
				 * Up or down one note
				 */
				if (tune > 0)
					notes *= note_diff;
				else
					notes /= note_diff;

				if (tune > 0)
					param->param[index].float_val =
						1.0 + (notes - 1) * tune;
				else
					param->param[index].float_val =
						1.0 - (1 - notes) * -tune;
			}
			break;
		case COARSETUNE: /* Coarse tuning in "small" amounts */
			{
				float tune, notes = 1.0;
				int i;

				tune = (value - 0.5) * 2;

				/*
				 * Up or down 11 notes.
				 */
				for (i = 0; i < 11;i++)
				{
					if (tune > 0)
						notes *= note_diff;
					else
						notes /= note_diff;
				}

				if (tune > 0)
					param->param[index].float_val =
						1.0 + (notes - 1) * tune;
				else
					param->param[index].float_val =
						1.0 - (1 - notes) * -tune;

				/* This might be superfluous? */
				((bristolNRO *) operator->specs)->seq++;
			}
			break;
		case TRANSPOSE:
			/* Transpose in semitone steps */
			{
				int semitone = value * CONTROLLER_RANGE;
				float transpose;

				if (semitone == param->param[index].int_val)
					break;

				param->param[index].int_val = semitone;

				transpose = 1.0;

				while (semitone-- > 0)
					transpose *= (note_diff);

				/* This check is now superfluous since we check the semi */
				if (transpose != param->param[index].float_val)
				{
					param->param[index].float_val = transpose;
					((bristolNRO *) operator->specs)->seq++;
				}
			}
			break;
		case DETUNE: /* Detuning */
			param->param[index].float_val = value;
			break;

		case H16: /* Harmonics */
		case H8: /* Harmonics */
		case H4: /* Harmonics */
		case H2: /* Harmonics */
		case H32: /* Harmonics */
			param->param[index].float_val = value * 4;
			break;

		case SQUARE: /* Waveforms */
		case SAW: /* Waveforms */
		case TRI: /* Waveforms */
		case SINE: /* Waveforms */
			param->param[index].float_val = value;
			break;

		case PW: /* Pulse Width */
			if ((param->param[index].float_val = value) < 0.1)
				param->param[index].float_val = 0.1;
			else if ((param->param[index].float_val = value) > 0.9)
				param->param[index].float_val = 0.9;
			break;

		case DRAIN: /* signal draining */
			param->param[index].float_val = (1.0 - value);
			break;

		case EMULATION: /* Some of the wave formats might be a bit specific? */
			param->param[index].int_val = value * CONTROLLER_RANGE;
			break;
	}

	return(0);
}

/*
 * Take the sync buffer and look for zero crossings. When they are found
 * do a resample to find out where the crossing happened as sample level
 * resolution results in distortion and shimmer
 *
 * Resampling, or rather interpolation to zero, means we have to look at 
 * the respective above and below zero values.
 *
 * This is NOT applied to the sync buffer, we apply it to our signal to 
 * generate a sync buffer for returning to the caller.
 */
static float
genSyncPoints(register float *dst, register float *src, register float lsv, register int count)
{
	for (; count > 0; count--, lsv = *src++)
		*dst++ = ((lsv < 0) && (*src >= 0))? lsv / (lsv - *src) : 0;

	return(lsv);
}

/*
static void
addbuf(register float *buf, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
		*buf++ += value;
	}
}

static void
fillbuf(register float *buf, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
		*buf++ = value;
	}
}
*/

static void
addbuf2(register float *dst, register float *src, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
		*dst++ = *src++ + value;
	}
}

static void
addpwm(register float *dst, register float *src, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
		if ((*dst = *src++ + value) > 0.9) *dst = 0.9; dst++;
	}
}

static void
mult2buf(register float *dst, register float *src, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
		*dst++ = value * *src++;
	}
}

static void
multbuf(register float *buf, register float value, register int c)
{
	for (; c > 0; c-=8)
	{
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
		*buf++ *= value;
	}
}

/* freq buffer, mod buffers 1 and 2, sync buffer, output buf and count */
static void
genSineTendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
	for (; c > 0; c--, fb++, sb++, mb++, ob++, t->cfc++)
	{
		if ((t->ls += *fb * t->tr) > 2 * M_PI) t->ls -= 2 * M_PI;
		*ob += sinf(t->ls) * gt->vpo * t->gn;
	}
}

/* freq buffer, mod buffers 1 and 2, sync buffer, output buf and count */
static void
genSquareTendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
	/*
	 * Square will have a fast ramp up/down and then a decaying plateau. We
	 * should accept a bit of overshoot on the fast ramp and consider clamping
	 * it back on the first following sample?
	 *
	 * We should have a single algorithm which is ramp from -VPO to +VPO which 
	 * is then inverted as necessary depending on state:
	 *
	 * 	From current state fast ramp to target, correct overshoot, then drain.
	 *
	 * The period at the +ve height is a function of the PWM buffer content,
	 * the period at the -ve height is then a function of the frequency.
	 *
	 * Both of these are fixed for any given cycle to maintain frequency.
	 */
	for (; c > 0; c--, fb++, sb++, mb++, ob++, t->cfc++)
	{
		if (t->climit > 0) {
			if (*sb != 0) {
				t->cfc = 1.0 - *sb;
				if ((t->ls -= gt->ramp * t->cfc) < gt->llimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->llimit;
			} else if (t->cfc > *fb * t->tr * *mb) {
				if ((t->ls -= gt->ramp * (t->cfc - *fb * t->tr * *mb))
					< gt->llimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->llimit;
			} else {
			 	switch (t->flags) {
					case TEND_STATE_RAMP_1:
						if ((t->ls += gt->ramp) > gt->ulimit)
			 				t->flags = TEND_STATE_RAMP_2;
						break;
					case TEND_STATE_RAMP_2:
						t->ls = gt->ulimit;
		 				t->flags = TEND_STATE_RAMP_3;
						break;
					default:
						t->ls *= gt->drain;
						break;
				}
			}
		} else {
			if (*sb != 0) {
				t->cfc = 1.0 - *sb;
//				t->cfc -= *fb * t->tr;
				if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->ulimit;
			} else if (t->cfc > *fb * t->tr) {
				t->cfc -= *fb * t->tr;
				if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->ulimit;
			} else {
			 	switch (t->flags) {
					case TEND_STATE_RAMP_1:
						if ((t->ls -= gt->ramp) < gt->llimit)
			 				t->flags = TEND_STATE_RAMP_2;
						break;
					case TEND_STATE_RAMP_2:
						t->ls = gt->llimit;
		 				t->flags = TEND_STATE_RAMP_3;
						break;
					default:
						t->ls *= gt->drain;
						break;
				}
			}
		}
		*ob += t->ls * t->gn;
	}
}

/* freq buffer, mod buffers 1 and 2, sync buffer, output buf and count */
static void
genSquare2Tendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
	/*
	 * Square will have a fast ramp up/down and then a decaying plateau. We
	 * should accept a bit of overshoot on the fast ramp and consider clamping
	 * it back on the first following sample?
	 *
	 * We should have a single algorithm which is ramp from -VPO to +VPO which 
	 * is then inverted as necessary depending on state:
	 *
	 * 	From current state fast ramp to target, correct overshoot, then drain.
	 *
	 * The period at the +ve height is a function of the PWM buffer content,
	 * the period at the -ve height is then a function of the frequency.
	 *
	 * Both of these are fixed for any given cycle to maintain frequency.
	 */
	for (; c > 0; c--, fb++, sb++, mb++, ob++, t->cfc++)
	{
		if (t->climit > 0) {
			if (*sb != 0) {
				t->cfc = 1.0 - *sb;
				if ((t->ls -= gt->ramp * t->cfc) < gt->llimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->llimit;
			} else if (t->cfc > *fb * t->tr * *mb) {
				if ((t->ls -= gt->ramp * (t->cfc - *fb * t->tr * *mb))
					< gt->llimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->llimit;
			} else {
			 	switch (t->flags) {
					case TEND_STATE_RAMP_1:
						if ((t->ls += gt->ramp) > gt->ulimit)
			 				t->flags = TEND_STATE_RAMP_2;
						break;
					case TEND_STATE_RAMP_2:
						t->ls = gt->ulimit;
		 				t->flags = TEND_STATE_RAMP_3;
						break;
					default:
						t->ls -= (t->climit + t->ls) * gt->drain;
						break;
				}
			}
		} else {
			if (*sb != 0) {
				t->cfc = 1.0 - *sb;
//				t->cfc -= *fb * t->tr;
				if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->ulimit;
			} else if (t->cfc > *fb * t->tr) {
				t->cfc -= *fb * t->tr;
				if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 					t->flags = TEND_STATE_RAMP_2;
				else
				 	t->flags = TEND_STATE_RAMP_1;
				t->climit = gt->ulimit;
			} else {
			 	switch (t->flags) {
					case TEND_STATE_RAMP_1:
						if ((t->ls -= gt->ramp) < gt->llimit)
			 				t->flags = TEND_STATE_RAMP_2;
						break;
					case TEND_STATE_RAMP_2:
						t->ls = gt->llimit;
		 				t->flags = TEND_STATE_RAMP_3;
						break;
					default:
						t->ls -= (t->climit + t->ls) * gt->drain;
						break;
				}
			}
		}
		*ob += t->ls * t->gn;
	}
}

static void
genSawTendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
//printf("genSaw %f %f\n", *fb, t->cfc);
	/*
	 * From current ramp down slowly. On full cycle ramp up fast then correct.
	 */
	for (; c > 0; c--, fb++, sb++, mb++, ob++, t->cfc++)
	{
		if (*sb != 0) {
			t->cfc = 1.0 - *sb;
//			t->ls += gt->ramp * t->cfc;
			if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 				t->flags = TEND_STATE_RAMP_2;
			else
		 		t->flags = TEND_STATE_RAMP_1;
		} else if (t->cfc > *fb * t->tr) {
			t->cfc -= *fb * t->tr;
			if ((t->ls += gt->ramp * t->cfc) > gt->ulimit)
 				t->flags = TEND_STATE_RAMP_2;
			else
			 	t->flags = TEND_STATE_RAMP_1;
		} else {
		 	switch (t->flags) {
				case TEND_STATE_RAMP_1:
					if ((t->ls += gt->ramp) > gt->ulimit)
		 				t->flags = TEND_STATE_RAMP_2;
					break;
				case TEND_STATE_RAMP_2:
					t->ls = gt->ulimit;
	 				t->flags = TEND_STATE_RAMP_3;
					break;
				default:
					t->ls += (gt->llimit - t->ls) * gt->damp;
					break;
			}
		}
		*ob += t->ls * t->gn;
	}
}

static void
genRampTendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
	genSawTendency(gt, t, fb, mb, sb, ob, c);
	multbuf(ob, -1.0, c);
}

/* freq buffer, mod buffers 1 and 2, sync buffer, output buf and count */
static void
genTriTendency(bristolNRO *gt, bristolNROlocal *t, float *fb, float *mb, float *sb, float *ob, int c)
{
//printf("genTri %i\n", t->t);
	for (; c > 0; c--, fb++, sb++, ob++, t->cfc++)
	{
		/*
		 * See if the frequency indicates direction, change.
		 */
		if (*sb != 0) {
			/*
			 * cfc is reset to value unrelated to fb
			 */
			t->cfc = 1.0 - *sb;
			t->climit = t->climit > 0? gt->llimit:gt->ulimit;
			t->ls = t->climit * gt->damp * t->cfc;
		} else if (t->cfc > *fb * t->tr) {
			/*
			 * Calculate where the correction actually hits. This is probably
			 * actually resampling, we evaluate the peak reached at the point
			 * of switching direction then we calculate the tendency towards
			 * the new target for the remainder from that point.
			 */
			t->cfc -= *fb * t->tr;
			t->ls += (t->climit - t->ls) * gt->damp * t->cfc;
			t->climit = t->climit > 0? gt->llimit:gt->ulimit;
			t->ls += (t->climit - t->ls) * gt->damp * (1.0 - t->cfc);
		} else 
			*ob += (t->ls += (t->climit - t->ls) * gt->damp);
		*ob += t->ls * t->gn;
	}
}

/*
 * Convert to samples per cycle for whole waveforms, ramp and saw for example,
 * but also square since it take the frequency and appliesa PWM modulation.
 */
static void
convertF2S(float *src, float *dst, float trans, float sr, int c)
{
	if (trans <= 0)
		trans = 1;

	for (; c > 0; c-=8)
	{
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
	}
}

/*
 * Number of samples per half cycle for the waves that have two identical 
 * half cycles (phase inverted). Tri uses this.
 */
static void
convertF2HS(float *src, float *dst, float trans, float sr, int c)
{
	if ((trans *= 2) <= 0)
		trans = 2;

	for (; c > 0; c-=8)
	{
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
		*dst++ = sr / (*src++ * trans);
	}
}

static void
convertF2Pi(float *src, float *dst, float trans, float sr, int c)
{
	if (trans >= 0)
		trans *= 2 * M_PI / sr;
	else
		trans = 2 * M_PI / sr;

	/*
	 * Convert to a number of PI. We have the frequency and sample rate from 
	 * which we can calculate which part of M_PI represents this sample.
	 */
	for (; c > 0; c-=8)
	{
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
		*dst++ = trans * *src++;
	}
}

/* This should perhaps eventually come from a file? */
void
initNROglobals(bristolNRO *gt, int sr, int sc)
{
	float t;

/*	printf("	init global tendencies (%i/%i)\n", sr, sc);*/

	gt->conv[BRISTOL_TEND_TRI]		= convertF2HS; /* Test sync */
	gt->gen[BRISTOL_TEND_TRI]		= genTriTendency;

	gt->conv[BRISTOL_TEND_SINE]		= convertF2Pi; /* OK */
	gt->gen[BRISTOL_TEND_SINE]		= genSineTendency;

	gt->conv[BRISTOL_TEND_SQUARE]	= convertF2S; /* Test sync */
	gt->gen[BRISTOL_TEND_SQUARE]	= genSquareTendency;

	gt->conv[BRISTOL_TEND_SAW]		= convertF2S; /* Test sync */
	gt->gen[BRISTOL_TEND_SAW]		= genSawTendency;

	gt->conv[BRISTOL_TEND_RAMP]		= convertF2S; /* Test */
	gt->gen[BRISTOL_TEND_RAMP]		= genRampTendency;

	gt->conv[BRISTOL_TEND_SQR2]		= convertF2S; /* Test */
	gt->gen[BRISTOL_TEND_SQR2]		= genSquare2Tendency;

	gt->ulimit = BRISTOL_VPO;
	gt->llimit = -BRISTOL_VPO;
	gt->vpo = BRISTOL_VPO;

	/*
	 * We want a rise time in here in us to be reformed into a sample delta.
	 *
	 * If we took 10us (already very long) at 44100 kHz we would get just a 
	 * half a sample. At 2x oversampling we get roughly 10us per sample (its
	 * actually just over 11) so then we could look at some reasonable values
	 * here. For the time being, with a native sampling rate, we will have to 
	 * look at above 50us rise times, otherwise we will introduce undesirable
	 * artifacts into the sound.
	 *
	 * 2 * VPO / (us * sr)
	 * 24 / (0.000070 * 44100) = 8
	 *
	 * We should check that the configured time here is not subsample at what-
	 * ever rate we are working.
	 */
	t = 0.000070;
	gt->ramp = BRISTOL_VPO * 2 / (t * sr);
	gt->ramp = 8;

	/*
	 * Damp and drain are voltage leakage functions basically. The signal will
	 * tend to zero or the desired target. We took some empirical values to 
	 * start with so for now just make sure these empiricals do not change
	 * with sample rate. Now this is a power function so VPO * pow(t, 12) = x
	 * where x is fixed at 8.
	 * 12 * pow(t, sr / 3600) = 8
	 * pow(t, sr/3600) = 2/3
		 t^sr/2600 = 2/3
		 sr/2600* log(t) = log (2/3)
		 log(t) = log(2/3) * 2600 / 23
	 */
	gt->damp = 0.06353;
	gt->drain = 0.98;

	gt->sr = sr;
	gt->sc = sc;
}

void
initNROlocal(bristolNRO *gt, bristolNROlocal *t, int form, float pw, int flags)
{
	int i;

//	t[0].pw = t[1].pw = t[2].pw = t[3].pw = t[4].pw = pw;

	if ((gt->seq == t->seq)
		&& ((flags & (BRISTOL_KEYON|BRISTOL_KEYREON)) == 0))
		return;

	for (i = 0; i < TENDENCY_COUNT; i++)
	{
		t[i].climit = gt->ulimit;
		t[i].cfc = 0;
		t[i].ls = 0;
		t[i].lsv = gt->ulimit;
	}

	switch (form) {
		case TEND_FORMAT_P8:
		default:
			if (t->seq != 0) {
				t->seq = gt->seq;
				return;
			}

			t[0].form = t[1].form = t[2].form = t[3].form = t[4].form
				= t[TENDENCY_MAX].form = BRISTOL_TEND_SQUARE;
			t[5].form = t[6].form = t[7].form = t[8].form = t[9].form
				= BRISTOL_TEND_SAW;
			t[10].form = t[11].form = t[12].form = t[13].form = t[14].form
				= BRISTOL_TEND_TRI;
			t[15].form = t[16].form = t[17].form = t[18].form = t[19].form
				= BRISTOL_TEND_SINE;

			t[0].tr = t[5].tr = t[10].tr = t[15].tr = 1;
			t[1].tr = t[6].tr = t[11].tr = t[16].tr = t[TENDENCY_MAX].tr = 2;
			t[2].tr = t[7].tr = t[12].tr = t[17].tr = 4;
			t[3].tr = t[8].tr = t[13].tr = t[18].tr = 8;
			t[4].tr = t[9].tr = t[14].tr = t[19].tr = 0.5;

			t[0].gn = t[5].gn = t[10].gn = t[15].gn = t[TENDENCY_MAX].gn = 1.0;
			t[1].gn = t[6].gn = t[11].gn = t[16].gn = 1.0;
			t[2].gn = t[7].gn = t[12].gn = t[17].gn = 1.0;
			t[3].gn = t[8].gn = t[13].gn = t[18].gn = 1.0;
			t[4].gn = t[9].gn = t[14].gn = t[19].gn = 1.0;

			t[TENDENCY_MAX].pw = 0.5;

			break;
		case TEND_FORMAT_BME:
			/*
			 * Here we only want one (or a small few?) strands but the algorithm
			 * is SQR2, sharp leading edge that fades not back to zero but back
			 * to the other extreme. It then starts moving from square to tri.
			 */
			t[0].form = BRISTOL_TEND_SQR2;
			t[0].gn = 1.0;
			break;
	}

	t->seq = gt->seq;
}

static int operate(bristolOP *operator,
	bristolVoice *voice,
	bristolOPParams *p,
	void *lcl)
{
	bristolNROlocal *t = lcl;
	bristolNRO *gt = (bristolNRO *) operator->specs;

	gt->drain = p->param[DRAIN].float_val;

	addpwm(gt->pwmb, gt->spec.io[NRO_PWM_IND].buf,
		p->param[PW].float_val, gt->sc);

	/* Convert the sync buffer into zero crossings */
	t[TENDENCY_MAX].lsv
		= genSyncPoints(gt->sb, gt->spec.io[NRO_SYNC_IND].buf,
			t[TENDENCY_MAX].lsv, gt->sc);

	/* Put in a default frequency buffer */
	mult2buf(gt->fb, gt->spec.io[NRO_IN_IND].buf, voice->baudio->gtune *
		p->param[FINETUNE].float_val * p->param[COARSETUNE].float_val, gt->sc);

	initNROlocal(gt, t,  p->param[EMULATION].int_val, p->param[PW].float_val,
		voice->flags);

	if (p->param[EMULATION].int_val == TEND_FORMAT_BME)
	{
		gt->conv[t[0].form](gt->fb, gt->fbm, 2.0, gt->sr, gt->sc);

		if ((t[0].gn = p->param[H16].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[0].tr = 8 / p->param[TRANSPOSE].float_val
				+ dt[0] * p->param[DETUNE].float_val;

			gt->gen[t[0].form](gt, &t[0],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[1].gn = p->param[H8].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[1].tr = 4 / p->param[TRANSPOSE].float_val
				+ dt[1] * p->param[DETUNE].float_val;

			gt->gen[t[1].form](gt, &t[1],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[2].gn = p->param[H4].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[2].tr = 2 / p->param[TRANSPOSE].float_val
				+ dt[2] * p->param[DETUNE].float_val;

			gt->gen[t[2].form](gt, &t[2],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		return(0);
	}

	/*
	 * Calculate the gain and transpositions then generate waves.
	 */
	if (p->param[SQUARE].float_val != 0)
	{
		gt->conv[t[0].form](gt->fb, gt->fbm, 2.0, gt->sr, gt->sc);

		if ((t[0].gn = p->param[H16].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[0].tr = 8 / p->param[TRANSPOSE].float_val
				+ dt[0] * p->param[DETUNE].float_val;

			gt->gen[t[0].form](gt, &t[0],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[1].gn = p->param[H8].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[1].tr = 4 / p->param[TRANSPOSE].float_val
				+ dt[1] * p->param[DETUNE].float_val;

			gt->gen[t[1].form](gt, &t[1],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[2].gn = p->param[H4].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[2].tr = 2 / p->param[TRANSPOSE].float_val
				+ dt[2] * p->param[DETUNE].float_val;

			gt->gen[t[2].form](gt, &t[2],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[3].gn = p->param[H2].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[3].tr = 1 / p->param[TRANSPOSE].float_val
				+ dt[3] * p->param[DETUNE].float_val;

			gt->gen[t[3].form](gt, &t[3],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

		if ((t[4].gn = p->param[H32].float_val * p->param[SQUARE].float_val)
			!= 0)
		{
			t[4].tr = 16 / p->param[TRANSPOSE].float_val
				+ dt[4] * p->param[DETUNE].float_val;

			gt->gen[t[4].form](gt, &t[4],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}
	}

//	mult2buf(gt->fb, gt->fbm, 1.001, gt->sc); /* transpositions */
	if (p->param[SAW].float_val != 0)
	{
		gt->conv[t[5].form](gt->fb, gt->fbm, 2.0, gt->sr, gt->sc);

		if ((t[5].gn = p->param[H16].float_val * p->param[SAW].float_val) != 0)
		{
			t[5].tr = 8 / p->param[TRANSPOSE].float_val
				+ dt[5] * p->param[DETUNE].float_val;

			gt->gen[t[5].form](gt, &t[5],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}
		if ((t[6].gn = p->param[H8].float_val * p->param[SAW].float_val) != 0)
		{
			t[6].tr = 4 / p->param[TRANSPOSE].float_val
				+ dt[6] * p->param[DETUNE].float_val;

			gt->gen[t[6].form](gt, &t[6],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}
		if ((t[7].gn = p->param[H4].float_val * p->param[SAW].float_val) != 0)
		{
			t[7].tr = 2 / p->param[TRANSPOSE].float_val
				+ dt[7] * p->param[DETUNE].float_val;

			gt->gen[t[7].form](gt, &t[7],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}
		if ((t[8].gn = p->param[H2].float_val * p->param[SAW].float_val) != 0)
		{
			t[8].tr = 1 / p->param[TRANSPOSE].float_val
				+ dt[8] * p->param[DETUNE].float_val;

			gt->gen[t[8].form](gt, &t[8],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}
		if ((t[9].gn = p->param[H32].float_val * p->param[SAW].float_val) != 0)
		{
			t[9].tr = 16 / p->param[TRANSPOSE].float_val
				+ dt[9] * p->param[DETUNE].float_val;

			gt->gen[t[9].form](gt, &t[9],
				gt->fbm,
				gt->pwmb,
				gt->sb,
				gt->spec.io[NRO_OUT_IND].buf,
				gt->sc);
		}

	}

	t[TENDENCY_MAX].tr = 4 / p->param[TRANSPOSE].float_val;

	addbuf2(gt->pwmb, gt->zb, 0.5, gt->sc);

	/* finally generate sync out signal */
	gt->conv[0](gt->fb, gt->fbm, 2.0, gt->sr, gt->sc);
	if (gt->spec.io[NRO_SYNC_OUT].buf)
		gt->gen[0](gt, &t[TENDENCY_MAX],
			gt->fbm,
			gt->pwmb, /* No PWM thanks to the addbuf2() */
			gt->zb, /* no sync */
			gt->spec.io[NRO_SYNC_OUT].buf,
			gt->sc);

	return(0);
}

bristolOP *
nroinit(bristolOP **operator, int index, int samplerate, int samplecount)
{
	bristolNRO *specs;

#ifdef BRISTOL_DBG
	printf("bit1oscinit(%x(%x), %i, %i, %i)\n",
		operator, *operator, index, samplerate, samplecount);
#endif

	*operator = bristolOPinit(operator, index, samplecount);
	note_diff = pow(2, ((float) 1)/12);;

	/*
	 * Then the local parameters specific to this operator. These will be
	 * the same for each operator, but must be inited in the local code.
	 */
	(*operator)->operate = operate;
	(*operator)->destroy = destroy;
	(*operator)->reset = reset;
	(*operator)->param = param;

	specs = (bristolNRO *) bristolmalloc0(sizeof(bristolNRO));
	(*operator)->specs = (bristolOPSpec *) specs;
	(*operator)->size = sizeof(bristolNRO);

	specs->sb = bristolmalloc0(sizeof(float) * samplecount);
	specs->fb = bristolmalloc0(sizeof(float) * samplecount);
	specs->fbm = bristolmalloc0(sizeof(float) * samplecount);
	specs->pwmb = bristolmalloc0(sizeof(float) * samplecount);
	specs->zb = bristolmalloc0(sizeof(float) * samplecount);

	initNROglobals(specs, samplerate, samplecount);

	/*
	 * These are specific to this operator, and will need to be altered for
	 * each operator.
	 */
	specs->spec.opname = OPNAME;
	specs->spec.description = OPDESCRIPTION;
	specs->spec.pcount = PCOUNT;
	specs->spec.iocount = IOCOUNT;
	/* Up to 20 independent oscillations - 5 harmonics, 4 waves? */
	specs->spec.localsize = sizeof(bristolNROlocal) * TENDENCY_COUNT;

	/*
	 * Now fill in the bit1osc specs for this operator. These are specific to an
	 * oscillator.
	 *
	 * Tune fine +/- semitone
	 * Tune coarse +/- 7 or 12 semitones
	 * Transpose in notes
	 * Detune
	 *
	 * 5 harmonics: 16/8/4/2/sub
	 * 4 waves (sqr/saw/tri/sine)
	 *
	 * Pulse width
	 *
	 * Reload tendencies
	 */
	specs->spec.param[0].pname = "tune";
	specs->spec.param[0].description = "fine semitone tuning";
	specs->spec.param[0].type = BRISTOL_FLOAT;
	specs->spec.param[0].low = 0;
	specs->spec.param[0].high = 1;
	specs->spec.param[0].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[1].pname = "tune";
	specs->spec.param[1].description = "coarse 12 semitone tuning";
	specs->spec.param[1].type = BRISTOL_FLOAT;
	specs->spec.param[1].low = 0;
	specs->spec.param[1].high = 1;
	specs->spec.param[1].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[2].pname = "tune";
	specs->spec.param[2].description = "transpose in semitones";
	specs->spec.param[2].type = BRISTOL_FLOAT;
	specs->spec.param[2].low = 0;
	specs->spec.param[2].high = 1;
	specs->spec.param[2].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[3].pname = "detune";
	specs->spec.param[3].description = "harmonic disonance";
	specs->spec.param[3].type = BRISTOL_FLOAT;
	specs->spec.param[3].low = 0;
	specs->spec.param[3].high = 1;
	specs->spec.param[3].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[4].pname = "16' harmonic";
	specs->spec.param[4].description = "16' harmonic volume";
	specs->spec.param[4].type = BRISTOL_FLOAT;
	specs->spec.param[4].low = 0;
	specs->spec.param[4].high = 1;
	specs->spec.param[4].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[5].pname = "8' harmonic";
	specs->spec.param[5].description = "8' harmonic volume";
	specs->spec.param[5].type = BRISTOL_FLOAT;
	specs->spec.param[5].low = 0;
	specs->spec.param[5].high = 1;
	specs->spec.param[5].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[6].pname = "4' harmonic";
	specs->spec.param[6].description = "4' harmonic volume";
	specs->spec.param[6].type = BRISTOL_FLOAT;
	specs->spec.param[6].low = 0;
	specs->spec.param[6].high = 1;
	specs->spec.param[6].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[7].pname = "2' harmonic";
	specs->spec.param[7].description = "2' harmonic volume";
	specs->spec.param[7].type = BRISTOL_FLOAT;
	specs->spec.param[7].low = 0;
	specs->spec.param[7].high = 1;
	specs->spec.param[7].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[8].pname = "sub-harmonic";
	specs->spec.param[8].description = "sub-harmonic volume";
	specs->spec.param[8].type = BRISTOL_FLOAT;
	specs->spec.param[8].low = 0;
	specs->spec.param[8].high = 1;
	specs->spec.param[8].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[9].pname = "square";
	specs->spec.param[9].description = "square signal level";
	specs->spec.param[9].type = BRISTOL_FLOAT;
	specs->spec.param[9].low = 0;
	specs->spec.param[9].high = 1;
	specs->spec.param[9].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[10].pname = "ramp";
	specs->spec.param[10].description = "ramp signal level";
	specs->spec.param[10].type = BRISTOL_FLOAT;
	specs->spec.param[10].low = 0;
	specs->spec.param[10].high = 1;
	specs->spec.param[10].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[11].pname = "tri";
	specs->spec.param[11].description = "tri signal level";
	specs->spec.param[11].type = BRISTOL_FLOAT;
	specs->spec.param[11].low = 0;
	specs->spec.param[11].high = 1;
	specs->spec.param[11].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[12].pname = "sine";
	specs->spec.param[12].description = "sine signal level";
	specs->spec.param[12].type = BRISTOL_FLOAT;
	specs->spec.param[12].low = 0;
	specs->spec.param[12].high = 1;
	specs->spec.param[12].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[13].pname = "pulse width";
	specs->spec.param[13].description = "square wave duty cycle";
	specs->spec.param[13].type = BRISTOL_FLOAT;
	specs->spec.param[13].low = 0;
	specs->spec.param[13].high = 1;
	specs->spec.param[13].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[14].pname = "drain";
	specs->spec.param[14].description = "signal draining";
	specs->spec.param[14].type = BRISTOL_FLOAT;
	specs->spec.param[14].low = 0;
	specs->spec.param[14].high = 1;
	specs->spec.param[14].flags = BRISTOL_ROTARY|BRISTOL_SLIDER;

	specs->spec.param[15].pname = "Type";
	specs->spec.param[15].description = "Oscillator type";
	specs->spec.param[15].type = BRISTOL_INT;
	specs->spec.param[15].low = 0;
	specs->spec.param[15].high = TENDENCY_CONV;
	specs->spec.param[15].flags = BRISTOL_BUTTON;

	/*
	 * Now fill in the bit1osc IO specs.
	 */
	specs->spec.io[0].ioname = "input";
	specs->spec.io[0].description = "input rate modulation signal";
	specs->spec.io[0].samplerate = samplerate;
	specs->spec.io[0].samplecount = samplecount;
	specs->spec.io[0].flags = BRISTOL_DC|BRISTOL_INPUT;

	specs->spec.io[1].ioname = "output";
	specs->spec.io[1].description = "oscillator output signal";
	specs->spec.io[1].samplerate = samplerate;
	specs->spec.io[1].samplecount = samplecount;
	specs->spec.io[1].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	specs->spec.io[2].ioname = "PW Modulating Input";
	specs->spec.io[2].description = "input of signal for PWM process";
	specs->spec.io[2].samplerate = samplerate;
	specs->spec.io[2].samplecount = samplecount;
	specs->spec.io[2].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	specs->spec.io[3].ioname = "Sync Input";
	specs->spec.io[3].description = "input to phase sync process";
	specs->spec.io[3].samplerate = samplerate;
	specs->spec.io[3].samplecount = samplecount;
	specs->spec.io[3].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	specs->spec.io[4].ioname = "Sync Output";
	specs->spec.io[4].description = "simple output signal for driving sync";
	specs->spec.io[4].samplerate = samplerate;
	specs->spec.io[4].samplecount = samplecount;
	specs->spec.io[4].flags = BRISTOL_AC|BRISTOL_OUTPUT;

	return(*operator);
}