aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/midiformat.c
blob: fa0612ba8e322c8ffe09f7d8c870501b1e04617e (plain)
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
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include "m_pd.h"

typedef struct _midiformat
{
    t_object  x_ob;
    t_float   x_channel;
} t_midiformat;

static t_class *midiformat_class;

static int midiformat_channel(t_midiformat *x)
{
    int ch = (int)x->x_channel;
    return (ch > 0 ? (ch - 1) & 0x0F : 0);
}

static void midiformat_note(t_midiformat *x, t_symbol *s, int ac, t_atom *av)
{
    if (ac >= 2 && av[0].a_type == A_FLOAT && av[1].a_type == A_FLOAT)
    {
	int pitch = (int)av[0].a_w.w_float;  /* CHECKED: anything goes */
	int velocity = (int)av[1].a_w.w_float;
	outlet_float(((t_object *)x)->ob_outlet, 0x90 | midiformat_channel(x));
	outlet_float(((t_object *)x)->ob_outlet, pitch);
	outlet_float(((t_object *)x)->ob_outlet, velocity);
    }
}

static void midiformat_polytouch(t_midiformat *x,
				 t_symbol *s, int ac, t_atom *av)
{
    if (ac >= 2 && av[0].a_type == A_FLOAT && av[1].a_type == A_FLOAT)
    {
	int touch = (int)av[0].a_w.w_float;
	int key = (int)av[1].a_w.w_float;
	outlet_float(((t_object *)x)->ob_outlet, 0xA0 | midiformat_channel(x));
	outlet_float(((t_object *)x)->ob_outlet, key);
	outlet_float(((t_object *)x)->ob_outlet, touch);
    }
}

static void midiformat_controller(t_midiformat *x,
				  t_symbol *s, int ac, t_atom *av)
{
    if (ac >= 2 && av[0].a_type == A_FLOAT && av[1].a_type == A_FLOAT)
    {
	int val = (int)av[0].a_w.w_float;
	int ctl = (int)av[1].a_w.w_float;
	outlet_float(((t_object *)x)->ob_outlet, 0xB0 | midiformat_channel(x));
	outlet_float(((t_object *)x)->ob_outlet, ctl);
	outlet_float(((t_object *)x)->ob_outlet, val);
    }
}

static void midiformat_program(t_midiformat *x, t_floatarg f)
{
    int pgm = (int)f;
    outlet_float(((t_object *)x)->ob_outlet, 0xC0 | midiformat_channel(x));
    outlet_float(((t_object *)x)->ob_outlet, pgm);
}

static void midiformat_touch(t_midiformat *x, t_floatarg f)
{
    int touch = (int)f;
    outlet_float(((t_object *)x)->ob_outlet, 0xD0 | midiformat_channel(x));
    outlet_float(((t_object *)x)->ob_outlet, touch);
}

static void midiformat_bend(t_midiformat *x, t_floatarg f)
{
    int val = (int)f;
    outlet_float(((t_object *)x)->ob_outlet, 0xE0 | midiformat_channel(x));
    outlet_float(((t_object *)x)->ob_outlet, 0);
    outlet_float(((t_object *)x)->ob_outlet, val);
}

static void *midiformat_new(t_floatarg f)
{
    t_midiformat *x = (t_midiformat *)pd_new(midiformat_class);
    x->x_channel = f;
    inlet_new((t_object *)x, (t_pd *)x, &s_list, gensym("lst1"));
    inlet_new((t_object *)x, (t_pd *)x, &s_list, gensym("lst2"));
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft3"));
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft4"));
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft5"));
    floatinlet_new((t_object *)x, &x->x_channel);
    outlet_new((t_object *)x, &s_float);
    return (x);
}

void midiformat_setup(void)
{
    midiformat_class = class_new(gensym("midiformat"), 
				 (t_newmethod)midiformat_new, 0,
				 sizeof(t_midiformat), 0,
				 A_DEFFLOAT, 0);
    class_addlist(midiformat_class, midiformat_note);
    class_addmethod(midiformat_class, (t_method)midiformat_polytouch,
		    gensym("lst1"), A_GIMME, 0);
    class_addmethod(midiformat_class, (t_method)midiformat_controller,
		    gensym("lst2"), A_GIMME, 0);
    class_addmethod(midiformat_class, (t_method)midiformat_program,
		    gensym("ft3"), A_FLOAT, 0);
    class_addmethod(midiformat_class, (t_method)midiformat_touch,
		    gensym("ft4"), A_FLOAT, 0);
    class_addmethod(midiformat_class, (t_method)midiformat_bend,
		    gensym("ft5"), A_FLOAT, 0);
}