source: roaraudio/roard/midi.c @ 668:71ac426690da

Last change on this file since 668:71ac426690da was 668:71ac426690da, checked in by phi, 16 years ago

added license statements

File size: 2.3 KB
Line 
1//midi.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27int midi_init (void) {
28 int i;
29 char * files[] = {
30                   "/dev/console",
31#ifdef __linux__
32                   "/dev/tty0",
33                   "/dev/vc/0",
34#endif
35                   NULL
36                  };
37
38 g_console          = -1;
39 g_midi_cb_stoptime = 0;
40 g_midi_cb_playing  = 0;
41
42 for (i = 0; files[i] != NULL; i++) {
43  if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 )
44   break;
45 }
46
47 if ( g_console != -1 ) {
48  return 0;
49 } else {
50  return -1;
51 }
52}
53
54int midi_free (void) {
55 if ( g_console != -1 )
56  close(g_console);
57 return 0;
58}
59
60int midi_cb_play(float t, float freq, int override) {
61 float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels;
62
63/*
64#define MIDI_CB_NOOVERRIDE 0
65#define MIDI_CB_OVERRIDE   1
66*/
67 if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE )
68  return -1;
69
70 g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t);
71 midi_cb_start(freq);
72 g_midi_cb_playing = 1;
73
74 return 0;
75}
76
77int midi_cb_update (void) {
78 if ( !g_midi_cb_playing )
79  return 0;
80
81 if ( g_midi_cb_stoptime <= g_pos )
82  midi_cb_stop();
83
84 return 0;
85}
86
87int midi_cb_start(float freq) {
88// On linux this uses ioctl KIOCSOUND
89#ifdef __linux__
90 if ( g_console == -1 )
91  return -1;
92
93 if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 )
94  return -1;
95
96 return 0;
97#else
98 return -1;
99#endif
100}
101
102int midi_cb_stop (void) {
103#ifdef __linux__
104 g_midi_cb_playing = 0;
105 return midi_cb_start(0);
106#else
107 return -1;
108#endif
109}
110
111//ll
Note: See TracBrowser for help on using the repository browser.