Ejemplo n.º 1
0
int Timidity_Init(int rate, int format, int channels, int samples)
{
  const char *env = getenv("TIMIDITY_CFG");
  if (!env || read_config_file(env)<0) {
    if (read_config_file(CONFIG_FILE)<0) {
      if (read_config_file(CONFIG_FILE_ETC)<0) {
        return(-1);
      }
    }
  }

  if (channels < 1 || channels == 3 || channels == 5 || channels > 6) return(-1);

  num_ochannels = channels;

  /* Set play mode parameters */
  play_mode->rate = rate;
  play_mode->encoding = 0;
  if ( (format&0xFF) == 16 ) {
    play_mode->encoding |= PE_16BIT;
  }
  if ( (format&0x8000) ) {
    play_mode->encoding |= PE_SIGNED;
  }
  if ( channels == 1 ) {
    play_mode->encoding |= PE_MONO;
  } 
  switch (format) {
    case AUDIO_S8:
      s32tobuf = s32tos8;
      break;
    case AUDIO_U8:
      s32tobuf = s32tou8;
      break;
    case AUDIO_S16LSB:
      s32tobuf = s32tos16l;
      break;
    case AUDIO_S16MSB:
      s32tobuf = s32tos16b;
      break;
    case AUDIO_U16LSB:
      s32tobuf = s32tou16l;
      break;
    case AUDIO_U16MSB:
      s32tobuf = s32tou16b;
      break;
    default:
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
      return(-1);
  }
  AUDIO_BUFFER_SIZE = samples;

  /* Allocate memory for mixing (WARNING:  Memory leak!) */
  resample_buffer = safe_malloc(AUDIO_BUFFER_SIZE*sizeof(resample_t)+100);
  common_buffer = safe_malloc(AUDIO_BUFFER_SIZE*num_ochannels*sizeof(int32));

  init_tables();

  if (ctl->open(0, 0)) {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
    return(-1);
  }

  if (!control_ratio) {
    control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
    if(control_ratio<1)
      control_ratio=1;
    else if (control_ratio > MAX_CONTROL_RATIO)
      control_ratio=MAX_CONTROL_RATIO;
  }
  if (*def_instr_name)
    set_default_instrument(def_instr_name);
  return(0);
}
Ejemplo n.º 2
0
int Timidity_Init_Simple(int rate, int samples, sint32 encoding)
{
	std::string configfile;
	/* see if the pentagram config file specifies an alternate timidity.cfg */
#ifndef PENTAGRAM_IN_EXULT
	SettingManager* settings = SettingManager::get_instance();
	if (!settings->get("timiditycfg", configfile))
		configfile = CONFIG_FILE;
#else
	config->value("config/audio/midi/timiditycfg", configfile, CONFIG_FILE);
#endif

	if (read_config_file(configfile.c_str())<0) {
		return(-1);
	}

	/* Check to see if the encoding is 'valid' */

	// Only 16 bit can be byte swapped
	if ((encoding & PE_BYTESWAP) && !(encoding & PE_16BIT))
		return(-1);

	// u-Law can only be mono or stereo 
	if ((encoding & PE_ULAW) && (encoding & ~(PE_ULAW|PE_MONO)))
		return(-1);

	/* Set play mode parameters */
	play_mode->rate = rate;
	play_mode->encoding = encoding;
	switch (play_mode->encoding) {
		case 0:
		case PE_MONO:
			s32tobuf = s32tou8;
			break;

		case PE_SIGNED:
		case PE_SIGNED|PE_MONO:
			s32tobuf = s32tos8;
			break;

		case PE_ULAW:
		case PE_ULAW|PE_MONO:
			s32tobuf = s32toulaw;
			break;

		case PE_16BIT:
		case PE_16BIT|PE_MONO:
			s32tobuf = s32tou16;
			break;

		case PE_16BIT|PE_SIGNED:
		case PE_16BIT|PE_SIGNED|PE_MONO:
			s32tobuf = s32tos16;
			break;

		case PE_BYTESWAP|PE_16BIT:
		case PE_BYTESWAP|PE_16BIT|PE_MONO:
			s32tobuf = s32tou16x;
			break;

		case PE_BYTESWAP|PE_16BIT|PE_SIGNED:
		case PE_BYTESWAP|PE_16BIT|PE_SIGNED|PE_MONO:
			s32tobuf = s32tos16x;
			break;

		default:
			ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
			return(-1);
	}

	AUDIO_BUFFER_SIZE = samples;

	/* Allocate memory for mixing (WARNING:  Memory leak!) */
	resample_buffer = safe_Malloc<sample_t>(AUDIO_BUFFER_SIZE);
	common_buffer = safe_Malloc<sint32>(AUDIO_BUFFER_SIZE*2);

	init_tables();

	if (ctl->open(0, 0)) {
		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
		return(-1);
	}

	if (!control_ratio) {
		control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
		if(control_ratio<1)
			control_ratio=1;
		else if (control_ratio > MAX_CONTROL_RATIO)
			control_ratio=MAX_CONTROL_RATIO;
	}
	if (*def_instr_name)
		set_default_instrument(def_instr_name);
	return(0);
}
Ejemplo n.º 3
0
static void do_song_load(MidIStream *stream, MidSongOptions *options, MidSong **out)
{
  MidSong *song;
  int i;

  *out = NULL;
  if (!stream) return;

  if (options->rate < MIN_OUTPUT_RATE || options->rate > MAX_OUTPUT_RATE) {
    DEBUG_MSG("Bad sample rate %d\n",options->rate);
    return;
  }
  if (options->channels != 1 && options->channels != 2) {
    DEBUG_MSG("Bad channels %u\n",options->channels);
    return;
  }
  switch (options->format) {
  case MID_AUDIO_S8:
  case MID_AUDIO_U8:
  case MID_AUDIO_S16LSB:
  case MID_AUDIO_S16MSB:
  case MID_AUDIO_U16LSB:
  case MID_AUDIO_U16MSB: break; /* supported */
  default:
    DEBUG_MSG("Bad audio format 0x%x\n",options->format);
    return;
  }

  /* Allocate memory for the song */
  song = (MidSong *)timi_calloc(sizeof(MidSong));
  if (!song) return;

  for (i = 0; i < 128; i++) {
    if (master_tonebank[i]) {
      song->tonebank[i] = (MidToneBank *) timi_calloc(sizeof(MidToneBank));
      if (!song->tonebank[i]) goto fail;
      song->tonebank[i]->tone = master_tonebank[i]->tone;
    }
    if (master_drumset[i]) {
      song->drumset[i] = (MidToneBank *) timi_calloc(sizeof(MidToneBank));
      if (!song->drumset[i]) goto fail;
      song->drumset[i]->tone = master_drumset[i]->tone;
    }
  }

  song->amplification = DEFAULT_AMPLIFICATION;
  song->voices = DEFAULT_VOICES;
  song->drumchannels = DEFAULT_DRUMCHANNELS;

  song->rate = options->rate;
  song->encoding = 0;
  if (options->format & 0x0010)
      song->encoding |= PE_16BIT;
  if (options->format & 0x8000)
      song->encoding |= PE_SIGNED;
  if (options->channels == 1)
      song->encoding |= PE_MONO;
  switch (options->format) {
  case MID_AUDIO_S8:
    song->write = timi_s32tos8;
    break;
  case MID_AUDIO_U8:
    song->write = timi_s32tou8;
    break;
  case MID_AUDIO_S16LSB:
    song->write = timi_s32tos16l;
    break;
  case MID_AUDIO_S16MSB:
    song->write = timi_s32tos16b;
    break;
  case MID_AUDIO_U16LSB:
    song->write = timi_s32tou16l;
    break;
  case MID_AUDIO_U16MSB:
    song->write = timi_s32tou16b;
    break;
  }

  song->buffer_size = options->buffer_size;
  song->resample_buffer = (sample_t *) timi_calloc(options->buffer_size * sizeof(sample_t));
  if (!song->resample_buffer) goto fail;
  song->common_buffer = (sint32 *) timi_calloc(options->buffer_size * 2 * sizeof(sint32));
  if (!song->common_buffer) goto fail;

  song->bytes_per_sample = 2;
  if (song->encoding & PE_16BIT)
    song->bytes_per_sample *= 2;
  if (song->encoding & PE_MONO)
    song->bytes_per_sample /= 2;

  song->control_ratio = options->rate / CONTROLS_PER_SECOND;
  if (song->control_ratio < 1)
      song->control_ratio = 1;
  else if (song->control_ratio > MAX_CONTROL_RATIO)
      song->control_ratio = MAX_CONTROL_RATIO;

  song->lost_notes = 0;
  song->cut_notes = 0;

  song->events = read_midi_file(stream, song, &(song->groomed_event_count),
				&song->samples);

  /* Make sure everything is okay */
  if (!song->events)
    goto fail;

  song->default_instrument = NULL;
  song->default_program = DEFAULT_PROGRAM;

  if (*def_instr_name)
    set_default_instrument(song, def_instr_name);

  load_missing_instruments(song);

  if (! song->oom)
      *out = song;
  else {
fail: mid_song_free (song);
  }
}
Ejemplo n.º 4
0
int Timidity_Init(int rate, int format, int channels, int samples, const char* config)
{
	int ret;
	ret = read_config_file(config);
	if (ret < 0) {
		return(ret);
	}

	/* Set play mode parameters */
	play_mode->rate = rate;
	play_mode->encoding = 0;
	if ( (format&0xFF) == 16 ) {
		play_mode->encoding |= PE_16BIT;
	}
	if ( (format&0x8000) ) {
		play_mode->encoding |= PE_SIGNED;
	}
	if ( channels == 1 ) {
		play_mode->encoding |= PE_MONO;
	} 
	switch (format) {
	case AUDIO_S8:
		s32tobuf = s32tos8;
		break;
	case AUDIO_U8:
		s32tobuf = s32tou8;
		break;
	case AUDIO_S16LSB:
		s32tobuf = s32tos16l;
		break;
	case AUDIO_S16MSB:
		s32tobuf = s32tos16b;
		break;
	case AUDIO_U16LSB:
		s32tobuf = s32tou16l;
		break;
	case AUDIO_U16MSB:
		s32tobuf = s32tou16b;
		break;
	default:
        printf("Unsupported audio format");
		return(-1);
	}
	AUDIO_BUFFER_SIZE = samples;

	/* Allocate memory for mixing (WARNING:  Memory leak!) */
	resample_buffer = (sample_t*)safe_malloc(AUDIO_BUFFER_SIZE*sizeof(sample_t));
	common_buffer = (int32_t*)safe_malloc(AUDIO_BUFFER_SIZE*2*sizeof(int32_t));

	init_tables();

	if (ctl->open(0, 0)) {
        std::cout << "Couldn't open " << ctl->id_name << "\n";
		return(-1);
	}

	if (!control_ratio) {
		control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
		if(control_ratio<1)
			control_ratio=1;
		else if (control_ratio > MAX_CONTROL_RATIO)
			control_ratio=MAX_CONTROL_RATIO;
	}
	if (!def_instr_name.empty())
		set_default_instrument(const_cast<char*>(def_instr_name.c_str()));
	return(0);
}