<commit_before>/* FluidSynth - A Software Synthesizer
 *
 * Copyright (C) 2003  <NAME> and others.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *  
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include "fluid_chan.h"
#include "fluid_mod.h"
#include "fluid_synth.h"
#include "fluid_sfont.h"

#define SETCC(_c,_n,_v)  _c->cc[_n] = _v

/*
 * new_fluid_channel
 */
fluid_channel_t* 
new_fluid_channel(fluid_synth_t* synth, int num)
{
  fluid_channel_t* chan;

  chan = FLUID_NEW(fluid_channel_t);
  if (chan == NULL) {
    FLUID_LOG(FLUID_ERR, "Out of memory");
    return NULL;
  }

  chan->synth = synth;
  chan->channum = num;

  fluid_channel_init(chan);
  fluid_channel_init_ctrl(chan);

  return chan;
}

void 
fluid_channel_init(fluid_channel_t* chan)
{
  chan->prognum = (chan->channum == 9)? 0 : chan->channum;
  chan->banknum = (chan->channum == 9)? 128 : 0;
  chan->sfontnum = 0;
  chan->preset = fluid_synth_find_preset(chan->synth, chan->banknum, chan->prognum);
  chan->interp_method = FLUID_INTERP_DEFAULT;
  chan->tuning = NULL;
  chan->nrpn_select = 0;
}

void 
fluid_channel_init_ctrl(fluid_channel_t* chan)
{
  int i;

  chan->key_pressure = 0;
  chan->channel_pressure = 0;
  chan->pitch_bend = 0x2000; /* Range is 0x4000, pitch bend wheel starts in centered position */
  chan->pitch_wheel_sensitivity = 2; /* two semi-tones */
  chan->bank_msb = 0;

  for (i = 0; i < GEN_LAST; i++) {
    chan->gen[i] = 0.0f;
    chan->gen_abs[i] = 0;
  }

  for (i = 0; i < 128; i++) {
    SETCC(chan, i, 0);
  }
  
  /* Volume / initial attenuation (MSB & LSB) */
  SETCC(chan, 7, 127); 
  SETCC(chan, 39, 0); 

  /* Pan (MSB & LSB) */
  SETCC(chan, 10, 64); 
  SETCC(chan, 10, 64); 

  /* Expression (MSB & LSB) */
  SETCC(chan, 11, 127); 
  SETCC(chan, 43, 127); 
}

void 
fluid_channel_reset(fluid_channel_t* chan)
{
  fluid_channel_init(chan);
  fluid_channel_init_ctrl(chan);
}

/*
 * delete_fluid_channel
 */
int 
delete_fluid_channel(fluid_channel_t* chan)
{
  FLUID_FREE(chan);
  return FLUID_OK;
}

/*
 * fluid_channel_set_preset
 */
int 
fluid_channel_set_preset(fluid_channel_t* chan, fluid_preset_t* preset)
{
  fluid_preset_notify(chan->preset, FLUID_PRESET_UNSELECTED, chan->channum);
  fluid_preset_notify(preset, FLUID_PRESET_SELECTED, chan->channum);

  chan->preset = preset;
  return FLUID_OK;
}

/*
 * fluid_channel_get_preset
 */
fluid_preset_t* 
fluid_channel_get_preset(fluid_channel_t* chan)
{
  return chan->preset;
}

/*
 * fluid_channel_get_banknum
 */
unsigned int 
fluid_channel_get_banknum(fluid_channel_t* chan)
{
  return chan->banknum;
}

/*
 * fluid_channel_set_prognum
 */
int 
fluid_channel_set_prognum(fluid_channel_t* chan, int prognum)
{
  chan->prognum = prognum;
  return FLUID_OK;
}

/*
 * fluid_channel_get_prognum
 */
int 
fluid_channel_get_prognum(fluid_channel_t* chan)
{
  return chan->prognum;
}

/*
 * fluid_channel_set_banknum
 */
int 
fluid_channel_set_banknum(fluid_channel_t* chan, unsigned int banknum)
{
  chan->banknum = banknum;
  return FLUID_OK;
}

/*
 * fluid_channel_cc
 */
int 
fluid_channel_cc(fluid_channel_t* chan, int num, int value)
{
  chan->cc[num] = value;
    
  switch (num) {
    
  case SUSTAIN_SWITCH: 
    {
      if (value < 64) {
/*  	printf("** sustain off\n"); */
	fluid_synth_damp_voices(chan->synth, chan->channum);
      } else {
/*  	printf("** sustain on\n"); */
      }
    }
    break;
  
  case BANK_SELECT_MSB:
    {
      chan->bank_msb = (unsigned char) (value & 0x7f);
/*      printf("** bank select msb recieved: %d\n", value); */<commit_msg>Applied Ken Ellinwood's fix:   I fixed the handling of a MIDI bank select controller 0, e.g., bank   select MSB (or "coarse" bank select according to my spec).  Prior to   this fix a channel's bank number was only changed upon reception of   MIDI bank select controller 32, e.g, bank select LSB (or "fine"   bank-select according to my spec).<commit_after>/* FluidSynth - A Software Synthesizer
 *
 * Copyright (C) 2003  <NAME> and others.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *  
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include "fluid_chan.h"
#include "fluid_mod.h"
#include "fluid_synth.h"
#include "fluid_sfont.h"

#define SETCC(_c,_n,_v)  _c->cc[_n] = _v

/*
 * new_fluid_channel
 */
fluid_channel_t* 
new_fluid_channel(fluid_synth_t* synth, int num)
{
  fluid_channel_t* chan;

  chan = FLUID_NEW(fluid_channel_t);
  if (chan == NULL) {
    FLUID_LOG(FLUID_ERR, "Out of memory");
    return NULL;
  }

  chan->synth = synth;
  chan->channum = num;

  fluid_channel_init(chan);
  fluid_channel_init_ctrl(chan);

  return chan;
}

void 
fluid_channel_init(fluid_channel_t* chan)
{
  chan->prognum = (chan->channum == 9)? 0 : chan->channum;
  chan->banknum = (chan->channum == 9)? 128 : 0;
  chan->sfontnum = 0;
  chan->preset = fluid_synth_find_preset(chan->synth, chan->banknum, chan->prognum);
  chan->interp_method = FLUID_INTERP_DEFAULT;
  chan->tuning = NULL;
  chan->nrpn_select = 0;
}

void 
fluid_channel_init_ctrl(fluid_channel_t* chan)
{
  int i;

  chan->key_pressure = 0;
  chan->channel_pressure = 0;
  chan->pitch_bend = 0x2000; /* Range is 0x4000, pitch bend wheel starts in centered position */
  chan->pitch_wheel_sensitivity = 2; /* two semi-tones */
  chan->bank_msb = 0;

  for (i = 0; i < GEN_LAST; i++) {
    chan->gen[i] = 0.0f;
    chan->gen_abs[i] = 0;
  }

  for (i = 0; i < 128; i++) {
    SETCC(chan, i, 0);
  }
  
  /* Volume / initial attenuation (MSB & LSB) */
  SETCC(chan, 7, 127); 
  SETCC(chan, 39, 0); 

  /* Pan (MSB & LSB) */
  SETCC(chan, 10, 64); 
  SETCC(chan, 10, 64); 

  /* Expression (MSB & LSB) */
  SETCC(chan, 11, 127); 
  SETCC(chan, 43, 127); 
}

void 
fluid_channel_reset(fluid_channel_t* chan)
{
  fluid_channel_init(chan);
  fluid_channel_init_ctrl(chan);
}

/*
 * delete_fluid_channel
 */
int 
delete_fluid_channel(fluid_channel_t* chan)
{
  FLUID_FREE(chan);
  return FLUID_OK;
}

/*
 * fluid_channel_set_preset
 */
int 
fluid_channel_set_preset(fluid_channel_t* chan, fluid_preset_t* preset)
{
  fluid_preset_notify(chan->preset, FLUID_PRESET_UNSELECTED, chan->channum);
  fluid_preset_notify(preset, FLUID_PRESET_SELECTED, chan->channum);

  chan->preset = preset;
  return FLUID_OK;
}

/*
 * fluid_channel_get_preset
 */
fluid_preset_t* 
fluid_channel_get_preset(fluid_channel_t* chan)
{
  return chan->preset;
}

/*
 * fluid_channel_get_banknum
 */
unsigned int 
fluid_channel_get_banknum(fluid_channel_t* chan)
{
  return chan->banknum;
}

/*
 * fluid_channel_set_prognum
 */
int 
fluid_channel_set_prognum(fluid_channel_t* chan, int prognum)
{
  chan->prognum = prognum;
  return FLUID_OK;
}

/*
 * fluid_channel_get_prognum
 */
int 
fluid_channel_get_prognum(fluid_channel_t* chan)
{
  return chan->prognum;
}

/*
 * fluid_channel_set_banknum
 */
int 
fluid_channel_set_banknum(fluid_channel_t* chan, unsigned int banknum)
{
  chan->banknum = banknum;
  return FLUID_OK;
}

/*
 * fluid_channel_cc
 */
int 
fluid_channel_cc(fluid_channel_t* chan, int num, int value)
{
  chan->cc[num] = value;
    
  switch (num) {
    
  case SUSTAIN_SWITCH: 
    {
      if (value < 64) {
/*  	printf("** sustain off\n"); */
	fluid_synth_damp_voices(chan->synth, chan->channum);
      } else {
/*  	printf("** sustain on\n"); */
      }
    }
    break;
  
  case BANK_SELECT_MSB:
    {
      chan->bank_msb = (unsigned char) (value & 0x7f);
/*      printf("** bank select msb recieved: %d\n", value); */
      /* FIXME: is this correct? */
      fluid_channel_set_banknum(chan, (unsigned int)(value & 0x7f));  /* KLE */
{'fraction_non_alphanumeric': 0.12456040839478162, 'fraction_numerical': 0.01871809415768576, 'mean_word_length': 3.737238044062332, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5600696', 'n_tokens_mistral': 3648, 'n_tokens_neox': 3279, 'n_words': 813}