Skip to content

Commit

Permalink
Refactoring: Unique<>, nullptr; fix bug with tempo = 120_Hz
Browse files Browse the repository at this point in the history
  • Loading branch information
mcvsama committed Jan 28, 2016
1 parent 056c7ba commit 3bc0cb2
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 114 deletions.
17 changes: 8 additions & 9 deletions src/haruhi/dsp/delay_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ DelayLine::set_max_delay (std::size_t max_delay) noexcept
{
assert (max_delay > 0);

delete _data;
_data = new Sample[sizeof (Sample) * max_delay];
_data.resize (max_delay);
_max_delay = max_delay;

if (_delay > _max_delay)
Expand All @@ -70,14 +69,14 @@ DelayLine::write (Sample const* data) noexcept
{
if (_wpos + _size < _max_delay)
{
std::copy (data, data + _size, _data + _wpos);
std::copy (data, data + _size, _data.begin() + _wpos);
_wpos = (_wpos + _size) % _max_delay;
}
else
{
const std::size_t n = _wpos + _size - _max_delay;
std::copy (data, data + n, _data + _wpos);
std::copy (data + n, data + _size, _data);
std::copy (data, data + n, _data.begin() + _wpos);
std::copy (data + n, data + _size, _data.begin());
_wpos = _size - n;
}
}
Expand All @@ -94,18 +93,18 @@ DelayLine::read (Sample* data) noexcept
if (pos < _size)
{
const std::size_t n = _size - pos;
std::copy (_data + _max_delay - n, _data + _max_delay, data);
std::copy (_data, _data + pos, data + n);
std::copy (_data.begin() + _max_delay - n, _data.begin() + _max_delay, data);
std::copy (_data.begin(), _data.begin() + pos, data + n);
}
else
std::copy (_data + pos - _size, _data + pos, data);
std::copy (_data.begin() + pos - _size, _data.begin() + pos, data);
}


void
DelayLine::clear() noexcept
{
std::fill (_data, _data + _max_delay, 0.0f);
std::fill (_data.begin(), _data.begin() + _max_delay, 0.0);
}

} // namespace DSP
Expand Down
10 changes: 5 additions & 5 deletions src/haruhi/dsp/delay_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ class DelayLine
clear() noexcept;

private:
Sample* _data = 0;
std::size_t _max_delay = 64; // Whole buffer size (number of samples).
std::size_t _size = 1; // Number of samples read/written on each round.
std::size_t _delay = 0;
std::size_t _wpos = 0;
std::vector<Sample> _data;
std::size_t _max_delay = 64; // Whole buffer size (number of samples).
std::size_t _size = 1; // Number of samples read/written on each round.
std::size_t _delay = 0;
std::size_t _wpos = 0;
};


Expand Down
20 changes: 9 additions & 11 deletions src/haruhi/dsp/fft_filler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ FFTFiller::fill (Wavetable* wavetable, unsigned int samples)

_was_interrupted = false;

typedef std::map<float, Sample*> Tables;

if (samples < 4096)
throw Exception ("samples number must be at least 4096");

Tables tables;
Wavetable::Tables tables;

wavetable->drop_tables();
wavetable->set_wavetables_size (samples);
Expand All @@ -59,14 +57,7 @@ FFTFiller::fill (Wavetable* wavetable, unsigned int samples)
unsigned int const tables_num = 36; // New table about every 4 semitones
float const expand_coeff = 1.25; // Min max frequency: ~19kHz
for (unsigned int i = 0; i < tables_num; ++i)
tables[0.5f - 0.5f * (std::pow (expand_coeff, i) - 1) / std::pow (expand_coeff, i)] = new Sample[samples];

// Create wavetables:
for (auto& t: tables)
{
CHECK_INTERRUPT;
wavetable->add_table (t.second, t.first);
}
tables[0.5f - 0.5f * (std::pow (expand_coeff, i) - 1) / std::pow (expand_coeff, i)].resize (samples);

FFT::Vector source (samples);
FFT::Vector target (samples);
Expand Down Expand Up @@ -113,6 +104,13 @@ FFTFiller::fill (Wavetable* wavetable, unsigned int samples)
t.second[i] = target[i].real();
}

// Create wavetables:
for (auto& t: tables)
{
CHECK_INTERRUPT;
wavetable->add_table (std::move (t.second), t.first);
}

#undef CHECK_INTERRUPT
}

Expand Down
6 changes: 2 additions & 4 deletions src/haruhi/dsp/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ template<unsigned int tOrder, int tResponseType>
mixed_advance_iir (Sample* x, Sample* y, int position) noexcept;

private:
ImpulseResponseType* _impulse_response = 0;
ImpulseResponseType* _impulse_response = nullptr;
typename ImpulseResponseType::Serial _last_serial = 0;
// Previous samples buffer, stored in reverse order (index 0 contains last sample, 1 one before last, etc):
Sample _px[Order];
Expand All @@ -95,9 +95,7 @@ template<unsigned int tOrder, int tResponseType>

template<unsigned int O, int R>
inline
Filter<O, R>::Filter (ImpulseResponseType* impulse_response) noexcept:
_impulse_response (0),
_last_serial (0)
Filter<O, R>::Filter (ImpulseResponseType* impulse_response) noexcept
{
assign_impulse_response (impulse_response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/haruhi/dsp/harmonics_wave.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HarmonicsWave: public Wave
/**
* Create wave that adds harmonics to inner wave.
*/
HarmonicsWave (Wave* inner_wave = 0, bool auto_delete = false) noexcept;
HarmonicsWave (Wave* inner_wave = nullptr, bool auto_delete = false) noexcept;

/**
* Returns sample from base function with added harmonics.
Expand Down
2 changes: 1 addition & 1 deletion src/haruhi/dsp/modulated_wave.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ModulatedWave: public Wave
/**
* \param mod_index indicates number of times modulator frequency is greater than wave's.
*/
ModulatedWave (Wave* inner_wave = 0, Wave* modulator = 0, Type mod_type = Ring, float mod_amplitude = 0.0f, unsigned int mod_index = 1, bool auto_delete_wave = false, bool auto_delete_modulator = false) noexcept;
ModulatedWave (Wave* inner_wave = nullptr, Wave* modulator = nullptr, Type mod_type = Ring, float mod_amplitude = 0.0f, unsigned int mod_index = 1, bool auto_delete_wave = false, bool auto_delete_modulator = false) noexcept;

/**
* Dtor
Expand Down
2 changes: 1 addition & 1 deletion src/haruhi/dsp/wave.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Wave: private Noncopyable

private:
bool _immutable = true;
Wave* _inner_wave = 0;
Wave* _inner_wave = nullptr;
bool _auto_delete = false;
};

Expand Down
23 changes: 2 additions & 21 deletions src/haruhi/dsp/wavetable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,16 @@ namespace Haruhi {

namespace DSP {

Wavetable::Wavetable():
_tables(),
_size (0)
{
}


Wavetable::~Wavetable()
{
drop_tables();
}


void
Wavetable::add_table (Sample* samples, float max_frequency)
Wavetable::add_table (std::vector<Sample>&& samples, float max_frequency)
{
Tables::iterator t = _tables.find (max_frequency);
// Delete old array, if we're overwriting table:
if (t != _tables.end())
delete[] t->second;
_tables[max_frequency] = samples;
_tables[max_frequency] = std::move (samples);
}


void
Wavetable::drop_tables() noexcept
{
for (auto& t: _tables)
delete[] t.second;
_tables.clear();
}

Expand Down
16 changes: 6 additions & 10 deletions src/haruhi/dsp/wavetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Wavetable
public:
// Maps frequency to array of samples. Frequency is max frequency for which
// given table can be used. Use lower_bound method to find correct table.
typedef std::map<float, Sample*> Tables;
typedef std::map<float, std::vector<Sample>> Tables;

/**
* All filler classes (that is classes that fill wavetables with samples)
Expand All @@ -63,17 +63,13 @@ class Wavetable
};

public:
Wavetable();

~Wavetable();

/**
* Adds wavetable.
* \param table Array of samples. Wavetable takes ownership of the pointer.
* \param max_frequency Maximum frequency for which this array can be used.
*/
void
add_table (Sample* samples, float max_frequency);
add_table (std::vector<Sample>&& samples, float max_frequency);

/**
* Deletes previously allocated tables using delete operator.
Expand All @@ -100,13 +96,13 @@ class Wavetable
* Returns wavetable index to use for given frequency.
* There must be at least one table in set.
*/
Sample const*
std::vector<Sample> const&
table_for_frequency (float frequency) const noexcept;

private:
Tables _tables;
// Number of samples in each table:
std::size_t _size;
std::size_t _size = 0;
};


Expand Down Expand Up @@ -134,7 +130,7 @@ Wavetable::set_wavetables_size (std::size_t size) noexcept
inline Sample
Wavetable::operator() (Sample phase, Sample frequency) const noexcept
{
Sample const* table = table_for_frequency (frequency);
auto& table = table_for_frequency (frequency);
const float p = mod1 (phase) * _size;
const int k = static_cast<int> (p);
const Sample v1 = table[k];
Expand All @@ -144,7 +140,7 @@ Wavetable::operator() (Sample phase, Sample frequency) const noexcept
}


inline Sample const*
inline std::vector<Sample> const&
Wavetable::table_for_frequency (float frequency) const noexcept
{
Tables::const_iterator t = _tables.lower_bound (frequency);
Expand Down
40 changes: 20 additions & 20 deletions src/haruhi/graph/audio_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ inline void
AudioBuffer::fill (Buffer const* other) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::copy_buffer (begin(), buf->begin(), size());
}
Expand Down Expand Up @@ -221,9 +221,9 @@ inline void
AudioBuffer::add (Buffer const* other) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::add_buffers (begin(), buf->begin(), size());
}
Expand All @@ -233,9 +233,9 @@ inline void
AudioBuffer::add (Buffer const* other, Sample attenuate_other) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::add_buffers (begin(), buf->begin(), attenuate_other, size());
}
Expand All @@ -245,9 +245,9 @@ inline void
AudioBuffer::sub (Buffer const* other) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::sub_buffers (begin(), buf->begin(), size());
}
Expand All @@ -257,9 +257,9 @@ inline void
AudioBuffer::attenuate (Buffer const* other) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::multiply_buffers (begin(), buf->begin(), size());
}
Expand All @@ -276,9 +276,9 @@ inline void
AudioBuffer::attenuate (Buffer const* other, Sample value) noexcept
{
assert (other->type() == AudioBuffer::TYPE);
AudioBuffer const* buf = static_cast<AudioBuffer const*> (other);
assert (begin() != 0);
assert (buf->begin() != 0);
auto buf = static_cast<AudioBuffer const*> (other);
assert (begin() != nullptr);
assert (buf->begin() != nullptr);
assert (buf->size() == size());
SIMD::multiply_buffers_and_by_scalar (begin(), buf->begin(), size(), value);
}
Expand Down Expand Up @@ -330,10 +330,10 @@ inline Sample*
AudioBuffer::allocate (std::size_t samples)
{
if (samples == 0)
return 0;
return nullptr;
void* ret;
if (posix_memalign (&ret, 32, sizeof (Sample) * samples) != 0)
return 0;
return nullptr;
return static_cast<Sample*> (ret);
}

Expand Down
2 changes: 1 addition & 1 deletion src/haruhi/graph/event_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void
EventBuffer::mixin (Buffer const* other)
{
assert (other->type() == EventBuffer::TYPE);
EventBuffer const* other_buffer = static_cast<EventBuffer const*> (other);
auto other_buffer = static_cast<EventBuffer const*> (other);
_events.insert (_events.end(), other_buffer->_events.begin(), other_buffer->_events.end());
}

Expand Down
16 changes: 3 additions & 13 deletions src/haruhi/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@

namespace Haruhi {

Graph::Graph():
RecursiveMutex(),
_inside_processing_round (false),
_next_tempo_tick (0),
_buffer_size (0),
_sample_rate (0_Hz),
_tempo (120.0_Hz),
_master_tune (440.0_Hz),
_audio_backend (0),
_event_backend (0)
Graph::Graph()
{
set_buffer_size (1);
}
Expand Down Expand Up @@ -200,11 +191,10 @@ Graph::set_master_tune (Frequency master_tune)


void
Graph::notify (Notification* notification)
Graph::notify (Unique<Notification> notification)
{
for (Unit* u: _units)
u->notify (notification);
delete notification;
u->notify (notification.get());
}


Expand Down
Loading

0 comments on commit 3bc0cb2

Please sign in to comment.