/** * csoundplayback.c * * CSound playback functions, * runs csound with -g -W flags (creates wav file) * for Denemo, a GTK Frontend to GNU Lilypond * * (c) 2002-2005 Adam Tee */ #include "csoundplayback.h" #include "exportcsound.h" #include "prefops.h" #include "utils.h" #include "external.h" #include #include #include #include #include #include #ifdef HAVE_SYS_WAIT_H # include #endif #ifdef HAVE_WAIT_H #include #endif #include /** * Local function prototypes */ void selorcfile (GtkWidget * widget, gpointer data); void chooseorcfile (GtkWidget * widget, struct cs_callback *data); /** * CSound playback to set the csound argument and * orchestra file to use */ void csoundplayback (GtkAction * action, DenemoGUI * gui) { GString *filename; GString *pidpath; GString *outputfile; GString *csoundline; gboolean extplayer; GPid playerpid = -1; FILE *fp; int got = 0; pidpath = get_temp_filename (ext_pidfiles[EXT_CSOUND]); fp = fopen (pidpath->str, "r+"); if (ftrylockfile (fp)) /* FIXME: is it multiprocs? */ { #ifdef DEBUG fprintf (stderr, "ext_csound: another instance is working\n"); #endif /* better chance next time */ fclose (fp); g_string_free (pidpath, TRUE); return; } /* ok, it's our turn: */ got = fscanf (fp, "%d", (int *) &playerpid); rewind (fp); #ifdef DEBUG fprintf (stderr, "ext_csound: got player %d\n", playerpid); #endif /* ensure we got something */ if (got && (playerpid > 0)) { #ifdef DEBUG fprintf (stderr, "ext_csound: killing player: %d\n", playerpid); #endif kill (playerpid, SIGTERM); g_spawn_close_pid (playerpid); /* fix leaks on win32 */ /* sleep(1); wait /dev/dsp do be *really* closed */ } filename = get_temp_filename ("denemocsoundplayback.sco"); exportcsound (filename->str, gui->si, gui->si->start, gui->si->end); extplayer = gui->prefs->rtcs; if (!extplayer) outputfile = g_string_new ("devaudio "); else outputfile = get_temp_filename ("denemocsoundplayback.wav "); csoundline = g_string_new (gui->prefs->csoundcommand->str); g_string_append (csoundline, " -o "); g_string_append (csoundline, outputfile->str); if (extplayer) g_string_append (csoundline, " -W "); g_string_append (csoundline, gui->prefs->csoundorcfile->str); g_string_append (csoundline, "\t"); g_string_append (csoundline, filename->str); /* the *real* moment: */ playerpid = spawn_external (csoundline); #ifdef DEBUG fprintf (stderr, "ext_csound: spawned %d\n", playerpid); #endif if ((playerpid > 0) && extplayer) /* play _after_ render */ { wait (playerpid); #ifdef DEBUG fprintf (stderr, "csound pid %d finished\n", playerpid); #endif g_string_free (csoundline, TRUE); csoundline = g_string_new (gui->prefs->audioplayer->str); g_string_append (csoundline, "\t"); g_string_append (csoundline, outputfile->str); playerpid = spawn_external (csoundline); if (playerpid > 0) fprintf (fp, "%d", playerpid); funlockfile (fp); /* FIXME: is it multiprocs? */ fclose (fp); g_string_free (csoundline, TRUE); g_string_free (outputfile, TRUE); } else if (playerpid > 0) { /* we were on devaudio */ fprintf (fp, "%d", playerpid); funlockfile (fp); /* FIXME: is it multiprocs ? */ fclose (fp); g_string_free (csoundline, TRUE); } } /** * Select the csound orchestra to use during playback */ void chooseorcfile (GtkWidget * widget, struct cs_callback *data) { GtkWidget *fs; GtkFileFilter *filter; DenemoGUI *gui = data->gui; GtkWidget *entry = data->entry; fs = gtk_file_chooser_dialog_new (_("Choose CSound Orchestra File"), GTK_WINDOW (data->dialog), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); filter = gtk_file_filter_new (); gtk_file_filter_set_name (filter, "CSound Orchestra File"); gtk_file_filter_add_pattern (filter, "*.orc"); gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fs), filter); gtk_widget_show_all (fs); if (gtk_dialog_run (GTK_DIALOG (fs)) == GTK_RESPONSE_ACCEPT) { g_string_assign (gui->prefs->csoundorcfile, gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fs))); gtk_entry_set_text (GTK_ENTRY (entry), gui->prefs->csoundorcfile->str); } gtk_widget_destroy (fs); }