//midi.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 * * This file is part of roard a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * RoarAudio 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 software; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * */ #include "roard.h" int midi_init (void) { int i; char * files[] = { "/dev/console", #ifdef __linux__ "/dev/tty0", "/dev/vc/0", #endif NULL }; g_console = -1; g_midi_cb_stoptime = 0; g_midi_cb_playing = 0; for (i = 0; files[i] != NULL; i++) { if ( (g_console = open(files[i], O_WRONLY|O_NOCTTY, 0)) != -1 ) break; } if ( g_console != -1 ) { return 0; } else { return -1; } } int midi_free (void) { if ( g_console != -1 ) close(g_console); return 0; } int midi_cb_play(float t, float freq, int override) { float samples_per_sec /* S/s */ = g_sa->rate * g_sa->channels; /* #define MIDI_CB_NOOVERRIDE 0 #define MIDI_CB_OVERRIDE 1 */ if ( g_midi_cb_playing && override != MIDI_CB_OVERRIDE ) return -1; g_midi_cb_stoptime = ROAR_MATH_OVERFLOW_ADD(g_pos, samples_per_sec*t); midi_cb_start(freq); g_midi_cb_playing = 1; return 0; } int midi_cb_update (void) { if ( !g_midi_cb_playing ) return 0; if ( g_midi_cb_stoptime <= g_pos ) midi_cb_stop(); return 0; } int midi_cb_start(float freq) { // On linux this uses ioctl KIOCSOUND #ifdef __linux__ if ( g_console == -1 ) return -1; if ( ioctl(g_console, KIOCSOUND, freq == 0 ? 0 : (int)(1193180.0/freq)) == -1 ) return -1; return 0; #else return -1; #endif } int midi_cb_stop (void) { #ifdef __linux__ g_midi_cb_playing = 0; return midi_cb_start(0); #else return -1; #endif } //ll