Example #1
0
File: lv2.c Project: aelse/setBfree
void initSynth(struct b_instance *inst, double rate) {
  // equivalent to ../src/main.c main()
  unsigned int defaultPreset[9] = {8,8,6, 0,0,0,0, 0,0};

  /* initAll() */
  initToneGenerator (inst->synth, inst->midicfg);
  initVibrato (inst->synth, inst->midicfg);
  initPreamp (inst->preamp, inst->midicfg);
  initReverb (inst->reverb, inst->midicfg, rate);
  initWhirl (inst->whirl, inst->midicfg, rate);
  initRunningConfig(inst->state, inst->midicfg);
  /* end - initAll() */

  initMidiTables(inst->midicfg);

  setDrawBars (inst, 0, defaultPreset);
#if 0
  setDrawBars (inst, 1, defaultPreset);
  setDrawBars (inst, 2, defaultPreset);
#endif

#ifdef DEBUGPRINT
  if (walkProgrammes(inst->progs, 0)) {
    listProgrammes (inst->progs, stderr);
  }
  listCCAssignments(inst->midicfg, stderr);
#endif
}
Example #2
0
/*
 * Main program.
 */
int main (int argc, char * argv []) {
  int i,c,k;
  int doDefaultConfig = TRUE;
  int doDefaultProgram = TRUE;
  int printCCTable = FALSE;
  int doDefaultCC = TRUE;
  char * configOverride [NOF_CFG_OVERS];
  int configOverEnd = 0;
  int loadProgram = -1;
  unsigned int randomPreset[9];
  unsigned int defaultPreset[9] = {8,8,8, 0,0,0,0, 0,0};
  unsigned int * presetSelect = defaultPreset;
  char *midnam = NULL;

  char * alternateProgrammeFile = NULL;
  char * alternateConfigFile = NULL;

  memset(&inst, 0, sizeof(b_instance));

  srand ((unsigned int) time (NULL));

  for (i=0;i<AUDIO_CHANNELS; i++)
    jack_port[i] = NULL;
  jack_ports = strdup("system:playback_");

  const char *optstring = "c:CdDhHl:M:p:PrU:V";
  const struct option long_options[] = {
    { "help",       no_argument,       0, 'H' },
    { "program",    required_argument, 0, 'p' },
    { "config",     required_argument, 0, 'c' },
    { "noconfig",   no_argument,       0, 'C' },
    { "dumpcc",     no_argument,       0, 'd' },
    { "noCC",       no_argument,       0, 'D' },
    { "midnam",     required_argument, 0, 'M' },
    { "noprogram",  no_argument,       0, 'P' },
    { "randomize",  no_argument,       0, 'r' },
    { "upper",      required_argument, 0, 'U' },
    { "version",    no_argument,       0, 'V' },
    { 0, 0, 0, 0 }
  };

  while ((c = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
    switch (c) {
      case 'c':
	alternateConfigFile = optarg;
	break;
      case 'C':
	doDefaultConfig = FALSE;
	break;
      case 'd':
	printCCTable = TRUE;
	break;
      case 'D':
	doDefaultCC = FALSE;
	break;
      case 'h':
	Usage(0);
	return (0);
	break;
      case 'H':
	Usage(1);
	return (0);
	break;
      case 'l':
	loadProgram = atoi(optarg);
	break;
      case 'M':
	midnam = optarg;
	break;
      case 'r':
	for (k = 0; k < 9; k++)
	  randomPreset[k] = rand () % 9;
	fprintf (stderr,
	    "Random Preset: "
	     "%d%d %d%d%d%d %d%d%d\n",
	     randomPreset[0],
	     randomPreset[1],
	     randomPreset[2],
	     randomPreset[3],
	     randomPreset[4],
	     randomPreset[5],
	     randomPreset[6],
	     randomPreset[7],
	     randomPreset[8]);
	presetSelect = randomPreset;
	break;
      case 'p':
	alternateProgrammeFile = optarg;
	break;
      case 'P':
	doDefaultProgram = FALSE;
	break;
      case 'U':
	parse_preset(defaultPreset, optarg);
	break;
      case 'V':
	PrintVersion();
	return(0);
      default:
	fprintf(stderr, "invalid argument.\n");
	Usage(0);
	return(1);
    }
  }

  for (i = optind; i < argc; ++i) {
    char * av = argv[i];
    if (strchr (av, '=') != NULL) {
      /* Remember this as a config parameter */
      if (configOverEnd < NOF_CFG_OVERS) {
	configOverride[configOverEnd++] = av;
      } else {
	fprintf (stderr,
	    "Too many configuration parameters (%d), please consider using a\n"
	    "configuration file instead of the commandline.\n",
	    NOF_CFG_OVERS);
	return(1);
      }
    }
  }

  /*
   * allocate data structures, instances.
   */
  allocAll();

  /*
   * evaluate configuration
   */

  if (getenv("XDG_CONFIG_HOME")) {
    size_t hl = strlen(getenv("XDG_CONFIG_HOME"));
    defaultConfigFile=(char*) malloc(hl+32);
    defaultProgrammeFile=(char*) malloc(hl+32);
    sprintf(defaultConfigFile, "%s/setBfree/default.cfg", getenv("XDG_CONFIG_HOME"));
    sprintf(defaultProgrammeFile, "%s/setBfree/default.pgm", getenv("XDG_CONFIG_HOME"));
  } else if (getenv("HOME")) {
    size_t hl = strlen(getenv("HOME"));
    defaultConfigFile=(char*) malloc(hl+30);
    defaultProgrammeFile=(char*) malloc(hl+30);
    sprintf(defaultConfigFile, "%s/.config/setBfree/default.cfg", getenv("HOME"));
    sprintf(defaultProgrammeFile, "%s/.config/setBfree/default.pgm", getenv("HOME"));
  }

  /*
   * Here we call modules that need to execute code in order to arrange
   * static initializations that is not practical to achieve in source code.
   */

  initControllerTable (inst.midicfg);
  if (doDefaultCC) {
    midiPrimeControllerMapping (inst.midicfg);
  }

  /*
   * Commandline arguments are parsed. If we are of a mind to try the
   * default configuration file we do that now.
   */

  if (doDefaultConfig == TRUE && defaultConfigFile) {
    if (access (defaultConfigFile, R_OK) == 0) {
      fprintf(stderr, "loading cfg: %s\n", defaultConfigFile);
      parseConfigurationFile (&inst, defaultConfigFile);
    }
  }

  if (alternateConfigFile) {
    if (access (alternateConfigFile, R_OK) == 0) {
      fprintf(stderr, "loading cfg: %s\n", alternateConfigFile);
      parseConfigurationFile (&inst, alternateConfigFile);
    }
  }

  /*
   * Then apply any configuration parameters collected from the commandline.
   * These must be applied last so that they can override the parameters
   * read from the files (if any).
   */

  for (i = 0; i < configOverEnd; i++) {
    parseConfigurationLine (&inst, "commandline argument",
			    0,
			    configOverride[i]);
  }

  /*
   * Having configured the initialization phase we can now actually do it.
   */
#ifndef _WIN32
  if (mlockall (MCL_CURRENT | MCL_FUTURE)) {
    fprintf(stderr, "Warning: Can not lock memory.\n");
  }
#endif

  initAll ();

  /*
   * We are initialized and now load the programme file.
   */

  if (doDefaultProgram == TRUE && defaultProgrammeFile) {
    if (access (defaultProgrammeFile, R_OK) == 0)
      loadProgrammeFile (inst.progs, defaultProgrammeFile);
  } else {
    walkProgrammes(inst.progs, 1); // clear built-in default program
  }
  if (alternateProgrammeFile != NULL)
    loadProgrammeFile (inst.progs, alternateProgrammeFile);

  if (walkProgrammes(inst.progs, 0)) {
    listProgrammes (inst.progs, stderr);
  }

  initMidiTables(inst.midicfg);

  if (printCCTable) {
    listCCAssignments(inst.midicfg, stderr);
  }

  if (midnam) {
    FILE *fp = fopen(midnam, "w");
    if (fp) {
      save_midname(&inst, fp);
      fclose(fp);
    } else {
      fprintf(stderr, "failed to write midnam to '%s'\n", midnam);
    }
  }

  /*
   * With the programmes eager and ready to go, we spawn off the MIDI
   * listener thread. The thread will initialize the MIDI device.
   */
#ifdef HAVE_ASEQ
  pthread_t t_midi;
  if (!use_jack_midi) {
    if (!aseq_open(midi_port)) {
      k = pthread_create(&t_midi, NULL, aseq_run, &inst);
      if (k != 0) {
	fprintf (stderr, "%d : %s\n", k, "pthread_create : MIDIInReader thread");
	return (1);
      }
    } else {
      return (1);
    }
  }
#endif

  setMIDINoteShift (inst.midicfg, 0);

  setDrawBars (&inst, 0, presetSelect);
#if 0 // initial values are assigned in tonegen.c initToneGenerator()
  setDrawBars (&inst, 1, presetSelect); /* 838 000 000 */
  setDrawBars (&inst, 2, presetSelect); /* 86 - */
#endif

  const int pgm_off = inst.progs->MIDIControllerPgmOffset;
  if (loadProgram >= 0 && loadProgram >= pgm_off) {
    installProgram(&inst, (loadProgram - pgm_off));
  }

#ifndef _WIN32
  signal (SIGHUP, catchsig);
  signal (SIGINT, catchsig);
#endif

  connect_jack_ports();

  synth_ready = 1;

  fprintf(stderr,"All systems go. press CTRL-C, or send SIGINT or SIGHUP to terminate\n");

  while (j_client)
  /* jack callback is doing this the work now */
#ifdef _WIN32
    Sleep (1000);
#else
    sleep (1);
#endif

  /* shutdown and cleanup */
#ifdef HAVE_ASEQ
  if (!use_jack_midi) {
    aseq_stop=1;
    pthread_join(t_midi, NULL);
    aseq_close();
  }
#endif

  free(defaultConfigFile);
  free(defaultProgrammeFile);

  free(j_output_bufferptrs);
  free(j_output_port);
  free(midi_port);
  free(jack_ports);
  for (i=0;i<AUDIO_CHANNELS; i++)
    free(jack_port[i]);

  freeAll();
#ifndef _WIN32
  munlockall();
#endif

  fprintf(stderr, "bye\n");
  return 0;
}