File: audio.cpp

package info (click to toggle)
zynaddsubfx 3.0.6-7.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 65,848 kB
  • sloc: cpp: 124,436; ansic: 39,936; objc: 2,496; makefile: 1,369; python: 567; sh: 566; ruby: 178; javascript: 50
file content (85 lines) | stat: -rw-r--r-- 2,222 bytes parent folder | download | duplicates (6)
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <jack/jack.h>
#include <jack/midiport.h>

extern float Fs;
extern float &freq;
extern bool  &gate;
void process_output(float *f, unsigned nframes);
int current_note = 0;

void dsp_dispatch(const char *msg);


//JACK stuff
jack_port_t   *port, *iport;
jack_client_t *client;
int process(unsigned nframes, void *args);
void synth_init(void);
void audio_cleanup(void)
{
    puts("Exiting jack...");
    jack_deactivate(client);
    jack_client_close(client);
}
void audio_init(void)
{
    synth_init();
    client = jack_client_open("rtosc-demo2", JackNullOption, NULL, NULL);
    if(!client)
        errx(1, "jack_client_open() failure");

    jack_set_process_callback(client, process, NULL);

    port = jack_port_register(client, "output",
            JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput | JackPortIsTerminal, 0);

    //Make midi port
    iport = jack_port_register(client, "input",
            JACK_DEFAULT_MIDI_TYPE, JackPortIsInput | JackPortIsTerminal, 0);

    if(!port)
        errx(1, "jack_port_register() failure");

    if(jack_activate(client))
        errx(1, "jack_activate() failure");

    atexit(audio_cleanup);
}

void process_control(unsigned char control[3]);
int process(unsigned nframes, void *)
{
    Fs = jack_get_sample_rate(client);

    //Handle midi first
    void *midi_buf = jack_port_get_buffer(iport, nframes);
    jack_midi_event_t ev;
    jack_nframes_t event_idx = 0;
    while(jack_midi_event_get(&ev, midi_buf, event_idx++) == 0) {
        switch(ev.buffer[0]&0xf0) {
            case 0x90: //Note On
                freq = 440.0f * powf(2.0f, (ev.buffer[1]-69.0f)/12.0f);
                current_note = ev.buffer[1];
                gate = 1;
                break;
            case 0x80: //Note Off
                if(current_note == ev.buffer[1])
                    current_note = gate =0;
                break;
            case 0xB0: //Controller
                process_control(ev.buffer);
                break;
        }
    }

    //Get all samples
    float *smps = (float*) jack_port_get_buffer(port, nframes);
    process_output(smps, nframes);

    return 0;
}