File: fluidsynth_simple.c

package info (click to toggle)
fluidsynth 2.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,268 kB
  • sloc: ansic: 45,303; cpp: 4,897; xml: 864; sh: 200; makefile: 74
file content (97 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download | duplicates (4)
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
/* FluidSynth Simple - An example of using fluidsynth
 *
 * This code is in the public domain.
 *
 * To compile:
 *   gcc -g -O -o fluidsynth_simple fluidsynth_simple.c -lfluidsynth
 *
 * To run
 *   fluidsynth_simple soundfont
 *
 * [Peter Hanappe]
 */


#include <stdio.h>
#include <fluidsynth.h>

int main(int argc, char **argv)
{
    fluid_settings_t *settings;
    fluid_synth_t *synth = NULL;
    fluid_audio_driver_t *adriver = NULL;
    int err = 0;

    if(argc != 2)
    {
        fprintf(stderr, "Usage: fluidsynth_simple [soundfont]\n");
        return 1;
    }

    /* Create the settings object. This example uses the default
     * values for the settings. */
    settings = new_fluid_settings();

    if(settings == NULL)
    {
        fprintf(stderr, "Failed to create the settings\n");
        err = 2;
        goto cleanup;
    }

    /* Create the synthesizer */
    synth = new_fluid_synth(settings);

    if(synth == NULL)
    {
        fprintf(stderr, "Failed to create the synthesizer\n");
        err = 3;
        goto cleanup;
    }

    /* Load the soundfont */
    if(fluid_synth_sfload(synth, argv[1], 1) == -1)
    {
        fprintf(stderr, "Failed to load the SoundFont\n");
        err = 4;
        goto cleanup;
    }

    /* Create the audio driver. As soon as the audio driver is
     * created, the synthesizer can be played. */
    adriver = new_fluid_audio_driver(settings, synth);

    if(adriver == NULL)
    {
        fprintf(stderr, "Failed to create the audio driver\n");
        err = 5;
        goto cleanup;
    }

    /* Play a note */
    fluid_synth_noteon(synth, 0, 60, 100);

    printf("Press \"Enter\" to stop: ");
    fgetc(stdin);
    printf("done\n");


cleanup:

    if(adriver)
    {
        delete_fluid_audio_driver(adriver);
    }

    if(synth)
    {
        delete_fluid_synth(synth);
    }

    if(settings)
    {
        delete_fluid_settings(settings);
    }

    return err;
}