Skip to content

Commit

Permalink
For issue #100, fixed a calculation error and playback at 4096 is rea…
Browse files Browse the repository at this point in the history
…sonable, not yet perfect.
  • Loading branch information
ahlstromcj committed Sep 30, 2022
1 parent ba14d3b commit 742a955
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 184 deletions.
78 changes: 34 additions & 44 deletions libseq66/include/util/ring_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* \library seq66 application
* \author Chris Ahlstrom
* \date 2022-09-19
* \updates 2022-09-27
* \updates 2022-09-30
* \license GNU GPLv2 or above
*/

Expand All @@ -54,7 +54,7 @@ class ring_buffer
public:

using value_type = TYPE;
using reference = TYPE & ;
using reference = TYPE &;
using const_reference = const TYPE &;
using size_type = std::size_t;
using container = std::vector<value_type>;
Expand Down Expand Up @@ -115,16 +115,6 @@ class ring_buffer
return count() == 0;
}

/*
* Useful?
*/

bool default_slot (const_reference item)
{
static TYPE empty_value;
return item == empty_value;
}

int dropped () const
{
return m_dropped;
Expand All @@ -136,8 +126,8 @@ class ring_buffer
size_type read_space () const;
size_type read (reference dest);
size_type write (const_reference src);

void push_back (const value_type & value);

void pop_front ()
{
if (m_contents_size > 0)
Expand Down Expand Up @@ -169,39 +159,19 @@ class ring_buffer

reference back ()
{
return m_buffer[previous_tail()]; // return m_buffer[m_tail];
return m_buffer[previous_tail()];
}

const_reference back () const
{
return m_buffer[previous_tail()]; // return m_buffer[m_tail];
return m_buffer[previous_tail()];
}

private: // helper functions

void initialize ();

void increment_head ()
{
if (m_contents_size > 0)
{
++m_head;
--m_contents_size;
if (m_head == m_buffer_size)
m_head = 0; /* wrap around */
}
}

void increment_tail ()
{
++m_tail;
++m_contents_size;
if (m_contents_size > m_contents_max)
m_contents_max = m_contents_size;

if (m_tail == m_buffer_size)
m_tail = 0; /* wrap around */
}
void increment_head ();
void increment_tail ();

size_t previous_tail ()
{
Expand Down Expand Up @@ -232,7 +202,6 @@ ring_buffer<TYPE>::ring_buffer (size_type sz) :
;

size_type psize = size_t(1 << power_of_two);
m_buffer.reserve(psize);
m_buffer_size = psize;
m_size_mask = psize - 1; /* 0xFF... for index safety */
initialize();
Expand Down Expand Up @@ -266,6 +235,32 @@ ring_buffer<TYPE>::initialize ()
m_buffer.push_back(empty_value); /* prepare buffer for usage */
}

template<typename TYPE>
void
ring_buffer<TYPE>::increment_head ()
{
if (m_contents_size > 0)
{
++m_head;
--m_contents_size;
if (m_head == m_buffer_size)
m_head = 0; /* wrap around */
}
}

template<typename TYPE>
void
ring_buffer<TYPE>::increment_tail ()
{
++m_tail;
++m_contents_size;
if (m_contents_size > m_contents_max) /* for checking */
m_contents_max = m_contents_size;

if (m_tail == m_buffer_size)
m_tail = 0; /* wrap around */
}

/**
* Lock the data block of `rb' using the system call 'mlock'.
* Not nearly ready for prime time!
Expand Down Expand Up @@ -310,12 +305,7 @@ template<typename TYPE>
void
ring_buffer<TYPE>::write_advance ()
{
#if defined USE_OLD_CODE
++m_tail;
m_tail &= m_size_mask;
#else
increment_tail();
#endif
}

/**
Expand Down Expand Up @@ -420,7 +410,7 @@ ring_buffer<TYPE>::push_back (const value_type & item)
increment_head();
m_buffer[m_tail] = item;
increment_tail();
++m_dropped;
++m_dropped; /* for future use in expansion */
}
}

Expand Down
18 changes: 9 additions & 9 deletions libseq66/src/midi/jack_assistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ jack_transport_callback (jack_nframes_t /*nframes*/, void * arg)
(void) jack_assistant::save_jack_parameters(pos, psize);

#if defined SEQ66_PLATFORM_DEBUG_TMI
static jack_time_t s_last = 0;
jack_time_t timeus = ::jack_get_time();
unsigned delta = unsigned(timeus - s_last);
if (pos.frame > 0)
{
printf("[debug] us %u, delta %u, frame %u\n",
unsigned(timeus), delta, unsigned(pos.frame));
}
s_last = timeus;
static jack_time_t s_last = 0;
jack_time_t timeus = ::jack_get_time();
unsigned delta = unsigned(timeus - s_last);
if (pos.frame > 0)
{
printf("[debug] us %u, delta %u, frame %u\n",
unsigned(timeus), delta, unsigned(pos.frame));
}
s_last = timeus;
#endif
if (p.is_running())
{
Expand Down
74 changes: 61 additions & 13 deletions seq_rtmidi/include/midi_jack_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* \library seq66 application
* \author Chris Ahlstrom
* \date 2017-01-02
* \updates 2022-09-26
* \updates 2022-09-29
* \license See above.
*
* GitHub issue #165: enabled a build and run with no JACK support.
Expand Down Expand Up @@ -74,6 +74,10 @@ class midi_jack_data
static double sm_jack_beats_per_minute; /* the BPM for the song */
static double sm_jack_frame_factor; /* frames per PPQN tick */

static jack_nframes_t sm_cycle_frame_count; /* progress callback param */
static jack_time_t sm_cycle_time_us; /* time between callbacks */
static jack_time_t sm_pulse_time_us; /* duration of a MIDI pulse */

/**
* Holds the JACK sequencer client pointer so that it can be used by the
* midibus objects. This is actually an opaque pointer; there is no way
Expand Down Expand Up @@ -143,57 +147,101 @@ class midi_jack_data
* Frame offset-related functions.
*/

static bool recalculate_frame_factor (const jack_position_t & pos);
static jack_nframes_t jack_frame_offset (jack_nframes_t F, midipulse p);
static jack_nframes_t jack_frame_estimate (midipulse p);
static bool recalculate_frame_factor
(
const jack_position_t & pos,
jack_nframes_t F
);
static jack_nframes_t frame_offset (jack_nframes_t F, midipulse p);
static jack_nframes_t time_offset
(
jack_nframes_t F, midipulse p,
jack_time_t Tpop, jack_time_t Tpush
);
static jack_nframes_t frame_estimate (midipulse p);
static double cycle (jack_nframes_t f, jack_nframes_t F);
static double pulse_cycle (midipulse p, jack_nframes_t F);

static jack_nframes_t jack_frame_rate ()
static jack_nframes_t frame_rate ()
{
return sm_jack_frame_rate;
}

static jack_nframes_t jack_start_frame ()
static jack_nframes_t start_frame ()
{
return sm_jack_start_frame;
}

static double jack_ticks_per_beat ()
static double ticks_per_beat ()
{
return sm_jack_ticks_per_beat;
}

static double jack_beats_per_minute ()
static double beats_per_minute ()
{
return sm_jack_beats_per_minute;
}

static double jack_frame_factor ()
static double frame_factor ()
{
return sm_jack_frame_factor;
}

static void jack_frame_rate (jack_nframes_t nf)
static jack_nframes_t cycle_frame_count ()
{
return sm_cycle_frame_count;
}

static jack_time_t cycle_time_us ()
{
return sm_cycle_time_us;
}

static jack_time_t pulse_time_us ()
{
return sm_pulse_time_us;
}

static void frame_rate (jack_nframes_t nf)
{
sm_jack_frame_rate = nf;
}

static void jack_start_frame (jack_nframes_t nf)
static void start_frame (jack_nframes_t nf)
{
sm_jack_start_frame = nf;
}

static void jack_ticks_per_beat (double tpb)
static void ticks_per_beat (double tpb)
{
sm_jack_ticks_per_beat = tpb;
}

static void jack_beats_per_minute (double bp)
static void beats_per_minute (double bp)
{
sm_jack_beats_per_minute = bp;
}

static void frame_factor (double ff)
{
sm_jack_frame_factor = ff;
}

static void cycle_frame_count (jack_nframes_t cfc)
{
sm_cycle_frame_count = cfc;
}

static void cycle_time_us (jack_time_t jt)
{
sm_cycle_time_us = jt;
}

static void pulse_time_us (jack_time_t jt)
{
sm_pulse_time_us = jt;
}

/*
* Basic member access. Getters and setters.
*/
Expand Down
31 changes: 27 additions & 4 deletions seq_rtmidi/include/rtmidi_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* \library seq66 application
* \author Gary P. Scavone; severe refactoring by Chris Ahlstrom
* \date 2016-11-20
* \updates 2022-09-22
* \updates 2022-09-30
* \license See above.
*
* The lack of hiding of these types within a class is a little to be
Expand Down Expand Up @@ -173,8 +173,15 @@ class midi_message
container m_bytes;

/**
* Holds the (optional) timestamp of the MIDI message. Non-zero only
* in the JACK implementation.
* Holds the time at which the message was sent to the ring-buffer, in
* microseconds.
*/

microsec m_push_time_us;

/**
* Holds the (optional) timestamp of the MIDI message. Non-zero only
* in the JACK implementation at present.
*/

midipulse m_timestamp;
Expand All @@ -183,6 +190,9 @@ class midi_message

midi_message (midipulse ts = 0);
midi_message (const midibyte * mbs, std::size_t sz);
midi_message (const midi_message & rhs) = default;
midi_message & operator = (const midi_message & rhs) = default;
~midi_message () = default;

midibyte & operator [] (std::size_t i)
{
Expand Down Expand Up @@ -226,12 +236,25 @@ class midi_message
m_bytes.push_back(b);
}

microsec push_time_us () const
{
return m_push_time_us;
}

void push_time_us (microsec ptus)
{
m_push_time_us = ptus;
}

midipulse timestamp () const
{
return m_timestamp;
}

void timestamp (midipulse t);
void timestamp (midipulse t)
{
m_timestamp = t;
}

bool is_sysex () const
{
Expand Down
Loading

0 comments on commit 742a955

Please sign in to comment.