<commit_before>// Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
#include "davorgan.genidl.hh"
#include <bse/bsemathsignal.hh>
#include <bse/bsemain.hh>
#include "bse/internal.hh"
#include <vector>

namespace Bse {
namespace Dav {

class Organ : public OrganBase {
  /* per mix_freq() tables */
  class Tables
  {
    vector<float> m_sine_table, m_triangle_table, m_pulse_table;
    uint	  m_ref_count, m_rate;
    Tables (uint urate) :
      m_sine_table (urate), m_triangle_table (urate), m_pulse_table (urate),
      m_ref_count (1), m_rate (urate)
    {
      double rate = urate, half = rate / 2, slope = rate / 10;
      int    i;
      /* Initialize sine table. */
      for (i = 0; i < rate; i++)
	m_sine_table[i] = sin (i / rate * 2.0 * PI) / 6.0;
      /* Initialize triangle table. */
      for (i = 0; i < rate / 2; i++)
	m_triangle_table[i] = (4 / rate * i - 1.0) / 6.0;
      for (; i < rate; i++)
	m_triangle_table[i] = (4 / rate * (rate - i) - 1.0) / 6.0;
      /* Initialize beveled pulse table:  _
       *                                 / \
       *                              \_/
       */
      for (i = 0; i < slope; i++)
	m_pulse_table[i] = -i / slope / 6.0;
      for (; i < half - slope; i++)
	m_pulse_table[i] = -1.0 / 6.0;
      for (; i < half + slope; i++)
	m_pulse_table[i] = ((i - half) / slope) / 6.0;
      for (; i < rate - slope; i++)
	m_pulse_table[i] = 1.0 / 6.0;
      for (; i < rate; i++)
	m_pulse_table[i] = ((rate - i) * 1.0 / slope) / 6.0;
    }
    ~Tables()
    {} // private destructor; use ref_counting
    static map<uint, Tables*> table_map;        // map of rate specific tables
    static std::mutex         table_mutex;
  public:
    static Tables*
    ref (uint rate)
    {
      static std::lock_guard<std::mutex> locker (table_mutex);
      if (table_map[rate])
	table_map[rate]->m_ref_count++;
      else
	table_map[rate] = new Tables (rate);
      return table_map[rate];
    }
    void
    unref()
    {
      return_unless (m_ref_count > 0);
      static std::lock_guard<std::mutex> locker (table_mutex);
      if (--m_ref_count == 0)
	{
	  table_map[m_rate] = 0;
	  delete this;
	}
    }
    const float*
    sine_table() const
    {
      return &m_sine_table[0];
    }<commit_msg>PLUGINS: davorgan: fix lock_guard to adhere to function scope<commit_after>// Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
#include "davorgan.genidl.hh"
#include <bse/bsemathsignal.hh>
#include <bse/bsemain.hh>
#include "bse/internal.hh"
#include <vector>

namespace Bse {
namespace Dav {

class Organ : public OrganBase {
  /* per mix_freq() tables */
  class Tables
  {
    vector<float> m_sine_table, m_triangle_table, m_pulse_table;
    uint	  m_ref_count, m_rate;
    Tables (uint urate) :
      m_sine_table (urate), m_triangle_table (urate), m_pulse_table (urate),
      m_ref_count (1), m_rate (urate)
    {
      double rate = urate, half = rate / 2, slope = rate / 10;
      int    i;
      /* Initialize sine table. */
      for (i = 0; i < rate; i++)
	m_sine_table[i] = sin (i / rate * 2.0 * PI) / 6.0;
      /* Initialize triangle table. */
      for (i = 0; i < rate / 2; i++)
	m_triangle_table[i] = (4 / rate * i - 1.0) / 6.0;
      for (; i < rate; i++)
	m_triangle_table[i] = (4 / rate * (rate - i) - 1.0) / 6.0;
      /* Initialize beveled pulse table:  _
       *                                 / \
       *                              \_/
       */
      for (i = 0; i < slope; i++)
	m_pulse_table[i] = -i / slope / 6.0;
      for (; i < half - slope; i++)
	m_pulse_table[i] = -1.0 / 6.0;
      for (; i < half + slope; i++)
	m_pulse_table[i] = ((i - half) / slope) / 6.0;
      for (; i < rate - slope; i++)
	m_pulse_table[i] = 1.0 / 6.0;
      for (; i < rate; i++)
	m_pulse_table[i] = ((rate - i) * 1.0 / slope) / 6.0;
    }
    ~Tables()
    {} // private destructor; use ref_counting
    static map<uint, Tables*> table_map;        // map of rate specific tables
    static std::mutex         table_mutex;
  public:
    static Tables*
    ref (uint rate)
    {
      std::lock_guard<std::mutex> locker (table_mutex);
      if (table_map[rate])
	table_map[rate]->m_ref_count++;
      else
	table_map[rate] = new Tables (rate);
      return table_map[rate];
    }
    void
    unref()
    {
      return_unless (m_ref_count > 0);
      std::lock_guard<std::mutex> locker (table_mutex);
      if (--m_ref_count == 0)
	{
	  table_map[m_rate] = 0;
	  delete this;
	}
    }
    const float*
    sine_table() const
    {
      return &m_sine_table[0];
    }
{'fraction_non_alphanumeric': 0.15266841644794402, 'fraction_numerical': 0.019247594050743656, 'mean_word_length': 2.0794612794612797, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7087737', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1712, 'n_words': 434}