File: voice_allocator.c

package info (click to toggle)
galan 0.3.0%2Bbeta4-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,380 kB
  • ctags: 4,208
  • sloc: ansic: 25,491; sh: 8,514; makefile: 174
file content (183 lines) | stat: -rw-r--r-- 4,315 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
/* gAlan - Graphical Audio Language
 * Copyright (C) 1999 Tony Garnock-Jones
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>

#include <gdk/gdk.h>
#include <gtk/gtk.h>

#include "global.h"
#include "generator.h"
#include "comp.h"
#include "control.h"
#include "gencomp.h"
#include "msgbox.h"


#define NUM_VOICES 8

#define GENERATOR_CLASS_NAME	"voice-allocator"
#define GENERATOR_CLASS_PATH	"Misc/Voice Alocator"


typedef struct Data {
    gint    note[NUM_VOICES];
    gint    alloc_ring;
} Data;

PRIVATE int init_instance(Generator *g) {

  int i;
  Data *data = safe_malloc( sizeof( Data ) );

  for( i=0; i<NUM_VOICES; i++ )
	  data->note[i] = -1;

  data->alloc_ring = 0;
  
  g->data = data;
  
  return 1;
}

PRIVATE void destroy_instance(Generator *g) {

    free( g->data );
}

PRIVATE void unpickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {

  int i;
  ObjectStoreDatum *array;
  Data *data = safe_malloc(sizeof(Data));

  data->alloc_ring = 0;
  array = objectstore_item_get(item, "note");

  for (i = 0; i < NUM_VOICES; i++)
      data->note[i] =
	objectstore_datum_integer_value(
	  objectstore_datum_array_get(array, i )
	);

  g->data = data;
}

PRIVATE void pickle_instance(Generator *g, ObjectStoreItem *item, ObjectStore *db) {

  Data *data = g->data;
  ObjectStoreDatum *array = objectstore_datum_new_array(NUM_VOICES );
  int i;

  objectstore_item_set(item, "note", array);

  for (i = 0; i < NUM_VOICES; i++)
      objectstore_datum_array_set(array, i,
				  objectstore_datum_new_integer(data->note[i]));
}

PRIVATE void evt_note_on_handler(Generator *g, AEvent *event) {

  Data *data = g->data;
  
  gint note = event->d.number;
  int i;

  for( i=0; i<NUM_VOICES; i++ ) {
      if( data->note[i] == note ) {
	  gen_send_events(g, i*2, -1, event);
	  return;
      }
  }

  for( i=data->alloc_ring; i<NUM_VOICES+data->alloc_ring; i++ ) {
      if( data->note[i%NUM_VOICES] == -1 ) {
	  data->note[i%NUM_VOICES] = note;
	  gen_send_events(g, (i%NUM_VOICES)*2, -1, event);
	  data->alloc_ring++;
	  return;
      }
  }

  printf( "No more free voices and no lru function yet: dropping note\n" ); 
}

PRIVATE void evt_note_off_handler(Generator *g, AEvent *event) {

  Data *data = g->data;
  
  gint note = event->d.number;
  int i;

  for( i=0; i<NUM_VOICES; i++ ) {


      if( data->note[i] == note ) {
	  data->note[i] = -1;
	  gen_send_events(g, i*2+1, -1, event);
	  return;
      }
  }

      printf( "Spurious Note off\n" ); 
}

PRIVATE void evt_reset_handler(Generator *g, AEvent *event) {

  Data *data = g->data;
  
  int i;

  for( i=0; i<NUM_VOICES; i++ ) {
	  data->note[i] = -1;
	  gen_send_events(g, i*2+1, -1, event);
  }
}

PRIVATE ControlDescriptor controls[] = {
  { CONTROL_KIND_NONE, }
};



PRIVATE void setup_class(void) {
    int i;

    GeneratorClass *k = gen_new_generatorclass(GENERATOR_CLASS_NAME, FALSE, 3, NUM_VOICES * 2,
	    NULL, NULL, controls,
	    init_instance, destroy_instance,
	    unpickle_instance, pickle_instance);

    gen_configure_event_input(k, 0, "Note On", evt_note_on_handler);
    gen_configure_event_input(k, 1, "Note Off", evt_note_off_handler);
    gen_configure_event_input(k, 2, "Reset", evt_reset_handler);

    for( i=0; i<NUM_VOICES; i++ ) {
	gen_configure_event_output(k, i*2, g_strdup_printf( "Note On %d", i ) );
	gen_configure_event_output(k, i*2+1, g_strdup_printf( "Note Off %d", i ) );
    }

    gencomp_register_generatorclass(k, FALSE, GENERATOR_CLASS_PATH, NULL, NULL);
}

PUBLIC void init_plugin_voice_allocator(void) {
  setup_class();
}