static void handleTuneRequest_setDevice (const NoteMethods *methods) { if (methods != noteMethods) { closeTuneDevice(); noteMethods = methods; } }
static void handleTuneRequest (TuneRequest *req) { if (req) { switch (req->type) { case TUNE_REQ_SET_DEVICE: handleTuneRequest_setDevice(req->parameters.setDevice.methods); break; case TUNE_REQ_PLAY_NOTES: { const NoteElement *tune = req->parameters.playNotes.tune; currentlyPlayingNotes = tune; handleTuneRequest_playNotes(tune); currentlyPlayingNotes = NULL; break; } case TUNE_REQ_PLAY_TONES: { const ToneElement *tune = req->parameters.playTones.tune; currentlyPlayingTones = tune; handleTuneRequest_playTones(tune); currentlyPlayingTones = NULL; break; } case TUNE_REQ_WAIT: handleTuneRequest_wait(req->parameters.wait.time); break; case TUNE_REQ_SYNCHRONIZE: handleTuneRequest_synchronize(req->parameters.synchronize.monitor); break; } free(req); } else { closeTuneDevice(); } }
int setTuneDevice (TuneDevice device) { const NoteMethods *methods; switch (device) { default: methods = NULL; break; #ifdef ENABLE_BEEPER_SUPPORT case tdBeeper: methods = &beeperMethods; break; #endif /* ENABLE_BEEPER_SUPPORT */ #ifdef ENABLE_PCM_SUPPORT case tdPcm: methods = &pcmMethods; break; #endif /* ENABLE_PCM_SUPPORT */ #ifdef ENABLE_MIDI_SUPPORT case tdMidi: methods = &midiMethods; break; #endif /* ENABLE_MIDI_SUPPORT */ #ifdef ENABLE_FM_SUPPORT case tdFm: methods = &fmMethods; break; #endif /* ENABLE_FM_SUPPORT */ } if (!methods) return 0; if (methods != noteMethods) { closeTuneDevice(1); noteMethods = methods; } return 1; }
int main (int argc, char *argv[]) { { static const OptionsDescriptor descriptor = { OPTION_TABLE(programOptions), .applicationName = "tunetest", .argumentsSummary = "{note duration} ..." }; PROCESS_OPTIONS(descriptor, argc, argv); } resetPreferences(); if (opt_tuneDevice && *opt_tuneDevice) { unsigned int device; if (!validateChoice(&device, opt_tuneDevice, deviceNames)) { logMessage(LOG_ERR, "%s: %s", "invalid tune device", opt_tuneDevice); return PROG_EXIT_SYNTAX; } prefs.tuneDevice = device; } #ifdef HAVE_MIDI_SUPPORT if (opt_midiInstrument && *opt_midiInstrument) { unsigned char instrument; if (!validateInstrument(&instrument, opt_midiInstrument)) { logMessage(LOG_ERR, "%s: %s", "invalid musical instrument", opt_midiInstrument); return PROG_EXIT_SYNTAX; } prefs.midiInstrument = instrument; } #endif /* HAVE_MIDI_SUPPORT */ if (opt_outputVolume && *opt_outputVolume) { static const int minimum = 0; static const int maximum = 100; int volume; if (!validateInteger(&volume, opt_outputVolume, &minimum, &maximum)) { logMessage(LOG_ERR, "%s: %s", "invalid volume percentage", opt_outputVolume); return PROG_EXIT_SYNTAX; } switch (prefs.tuneDevice) { case tdPcm: prefs.pcmVolume = volume; break; case tdMidi: prefs.midiVolume = volume; break; case tdFm: prefs.fmVolume = volume; break; default: break; } } if (!argc) { logMessage(LOG_ERR, "missing tune."); return PROG_EXIT_SYNTAX; } if (argc % 2) { logMessage(LOG_ERR, "missing note duration."); return PROG_EXIT_SYNTAX; } { unsigned int count = argc / 2; TuneElement *elements = malloc((sizeof(*elements) * count) + 1); if (elements) { TuneElement *element = elements; while (argc) { int note; int duration; { static const int minimum = 0X01; static const int maximum = 0X7F; const char *argument = *argv++; if (!validateInteger(¬e, argument, &minimum, &maximum)) { logMessage(LOG_ERR, "%s: %s", "invalid note number", argument); return PROG_EXIT_SYNTAX; } --argc; } { static const int minimum = 1; static const int maximum = 255; const char *argument = *argv++; if (!validateInteger(&duration, argument, &minimum, &maximum)) { logMessage(LOG_ERR, "%s: %s", "invalid note duration", argument); return PROG_EXIT_SYNTAX; } --argc; } { TuneElement te = TUNE_NOTE(duration, note); *(element++) = te; } } { TuneElement te = TUNE_STOP(); *element = te; } } else { logMallocError(); return PROG_EXIT_FATAL; } if (!setTuneDevice(prefs.tuneDevice)) { logMessage(LOG_ERR, "unsupported tune device: %s", deviceNames[prefs.tuneDevice]); return PROG_EXIT_SEMANTIC; } { playTune(elements); closeTuneDevice(); } free(elements); } return PROG_EXIT_SUCCESS; }