+
+
+/******************************************************************************/
+DummyPort::DummyPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
+ : _dummy_backend (b)
+ , _name (name)
+ , _flags (flags)
+ , _rseed (0)
+ , _gen_cycle (false)
+{
+ _capture_latency_range.min = 0;
+ _capture_latency_range.max = 0;
+ _playback_latency_range.min = 0;
+ _playback_latency_range.max = 0;
+ _dummy_backend.port_connect_add_remove_callback();
+}
+
+DummyPort::~DummyPort () {
+ disconnect_all ();
+ _dummy_backend.port_connect_add_remove_callback();
+}
+
+
+int DummyPort::connect (DummyPort *port)
+{
+ if (!port) {
+ PBD::error << _("DummyPort::connect (): invalid (null) port") << endmsg;
+ return -1;
+ }
+
+ if (type () != port->type ()) {
+ PBD::error << _("DummyPort::connect (): wrong port-type") << endmsg;
+ return -1;
+ }
+
+ if (is_output () && port->is_output ()) {
+ PBD::error << _("DummyPort::connect (): cannot inter-connect output ports.") << endmsg;
+ return -1;
+ }
+
+ if (is_input () && port->is_input ()) {
+ PBD::error << _("DummyPort::connect (): cannot inter-connect input ports.") << endmsg;
+ return -1;
+ }
+
+ if (this == port) {
+ PBD::error << _("DummyPort::connect (): cannot self-connect ports.") << endmsg;
+ return -1;
+ }
+
+ if (is_connected (port)) {
+#if 0 // don't bother to warn about this for now. just ignore it
+ PBD::error << _("DummyPort::connect (): ports are already connected:")
+ << " (" << name () << ") -> (" << port->name () << ")"
+ << endmsg;
+#endif
+ return -1;
+ }
+
+ _connect (port, true);
+ return 0;
+}
+
+
+void DummyPort::_connect (DummyPort *port, bool callback)
+{
+ _connections.push_back (port);
+ if (callback) {
+ port->_connect (this, false);
+ _dummy_backend.port_connect_callback (name(), port->name(), true);
+ }
+}
+
+int DummyPort::disconnect (DummyPort *port)
+{
+ if (!port) {
+ PBD::error << _("DummyPort::disconnect (): invalid (null) port") << endmsg;
+ return -1;
+ }
+
+ if (!is_connected (port)) {
+ PBD::error << _("DummyPort::disconnect (): ports are not connected:")
+ << " (" << name () << ") -> (" << port->name () << ")"
+ << endmsg;
+ return -1;
+ }
+ _disconnect (port, true);
+ return 0;
+}
+
+void DummyPort::_disconnect (DummyPort *port, bool callback)
+{
+ std::vector<DummyPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
+
+ assert (it != _connections.end ());
+
+ _connections.erase (it);
+
+ if (callback) {
+ port->_disconnect (this, false);
+ _dummy_backend.port_connect_callback (name(), port->name(), false);
+ }
+}
+
+
+void DummyPort::disconnect_all ()
+{
+ while (!_connections.empty ()) {
+ _connections.back ()->_disconnect (this, false);
+ _dummy_backend.port_connect_callback (name(), _connections.back ()->name(), false);
+ _connections.pop_back ();
+ }
+}
+
+bool
+DummyPort::is_connected (const DummyPort *port) const
+{
+ return std::find (_connections.begin (), _connections.end (), port) != _connections.end ();
+}
+
+bool DummyPort::is_physically_connected () const
+{
+ for (std::vector<DummyPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
+ if ((*it)->is_physical ()) {
+ return true;
+ }
+ }
+ return false;
+}
+
+void DummyPort::setup_random_number_generator ()
+{
+#ifdef PLATFORM_WINDOWS
+ LARGE_INTEGER Count;
+ if (QueryPerformanceCounter (&Count)) {
+ _rseed = Count.QuadPart % UINT_MAX;
+ } else
+#endif
+ {
+ _rseed = g_get_monotonic_time() % UINT_MAX;
+ }
+ _rseed = (_rseed + (uint64_t)this) % UINT_MAX;
+}
+
+inline uint32_t
+DummyPort::randi ()
+{
+ // 31bit Park-Miller-Carta Pseudo-Random Number Generator
+ // http://www.firstpr.com.au/dsp/rand31/
+ uint32_t hi, lo;
+ lo = 16807 * (_rseed & 0xffff);
+ hi = 16807 * (_rseed >> 16);
+
+ lo += (hi & 0x7fff) << 16;
+ lo += hi >> 15;
+#if 1
+ lo = (lo & 0x7fffffff) + (lo >> 31);
+#else
+ if (lo > 0x7fffffff) { lo -= 0x7fffffff; }
+#endif
+ return (_rseed = lo);
+}
+
+inline float
+DummyPort::randf ()
+{
+ return (randi() / 1073741824.f) - 1.f;
+}
+
+/******************************************************************************/
+
+DummyAudioPort::DummyAudioPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
+ : DummyPort (b, name, flags)
+ , _gen_type (Silence)
+ , _b0 (0)
+ , _b1 (0)
+ , _b2 (0)
+ , _b3 (0)
+ , _b4 (0)
+ , _b5 (0)
+ , _b6 (0)
+ , _wavetable (0)
+ , _gen_period (0)
+ , _gen_offset (0)
+ , _gen_perio2 (0)
+ , _gen_count2 (0)
+ , _pass (false)
+ , _rn1 (0)
+{
+ memset (_buffer, 0, sizeof (_buffer));
+}
+
+DummyAudioPort::~DummyAudioPort () {
+ free(_wavetable);
+ _wavetable = 0;
+}
+
+void DummyAudioPort::setup_generator (GeneratorType const g, float const samplerate)
+{
+ DummyPort::setup_random_number_generator();
+ _gen_type = g;
+
+ switch (_gen_type) {
+ case PinkNoise:
+ case PonyNoise:
+ case UniformWhiteNoise:
+ case GaussianWhiteNoise:
+ case Silence:
+ break;
+ case KronekerDelta:
+ _gen_period = (5 + randi() % (int)(samplerate / 20.f));
+ break;
+ case SquareWave:
+ _gen_period = (5 + randi() % (int)(samplerate / 20.f)) & ~1;
+ break;
+ case SineWave:
+ _gen_period = 5 + randi() % (int)(samplerate / 20.f);
+ _wavetable = (Sample*) malloc (_gen_period * sizeof(Sample));
+ for (uint32_t i = 0 ; i < _gen_period; ++i) {
+ _wavetable[i] = .12589f * sinf(2.0f * M_PI * (float)i / (float)_gen_period); // -18dBFS
+ }
+ break;
+ case SquareSweep:
+ case SquareSweepSwell:
+ case SineSweep:
+ case SineSweepSwell:
+ {
+ _gen_period = 5 * samplerate + randi() % (int)(samplerate * 10.f);
+ _gen_period &= ~1;
+ _gen_perio2 = 1 | (int)ceilf (_gen_period * .89f); // Volume Swell period
+ const double f_min = 20.;
+ const double f_max = samplerate * .5;
+ const double g_p2 = _gen_period * .5;
+#ifdef LINEAR_SWEEP
+ const double b = (f_max - f_min) / (2. * samplerate * g_p2);
+ const double a = f_min / samplerate;
+#else
+ const double b = log (f_max / f_min) / g_p2;
+ const double a = f_min / (b * samplerate);
+#endif
+ const uint32_t g_p2i = rint(g_p2);
+ _wavetable = (Sample*) malloc (_gen_period * sizeof(Sample));
+ for (uint32_t i = 0 ; i < g_p2i; ++i) {
+#ifdef LINEAR_SWEEP
+ const double phase = i * (a + b * i);
+#else
+ const double phase = a * exp (b * i) - a;
+#endif
+ _wavetable[i] = (float)sin (2. * M_PI * (phase - floor (phase)));
+ }
+ for (uint32_t i = g_p2i; i < _gen_period; ++i) {
+ const uint32_t j = _gen_period - i;
+#ifdef LINEAR_SWEEP
+ const double phase = j * (a + b * j);
+#else
+ const double phase = a * exp (b * j) - a;
+#endif
+ _wavetable[i] = (float)sin (2. * M_PI * (phase - floor (phase)));
+ }
+ if (_gen_type == SquareSweep) {
+ for (uint32_t i = 0 ; i < _gen_period; ++i) {
+ _wavetable[i] = _wavetable[i] < 0 ? -.40709f : .40709f;
+ }
+ }
+ else if (_gen_type == SquareSweepSwell) {
+ for (uint32_t i = 0 ; i < _gen_period; ++i) {
+ _wavetable[i] = _wavetable[i] < 0 ? -1 : 1;
+ }
+ }
+ }
+ break;
+ case Loopback:
+ _wavetable = (Sample*) malloc (DummyAudioBackend::max_buffer_size() * sizeof(Sample));
+ break;
+ }
+}
+
+void DummyAudioPort::midi_to_wavetable (DummyMidiBuffer const * const src, size_t n_samples)
+{
+ memset(_wavetable, 0, n_samples * sizeof(float));
+ /* generate an audio spike for every midi message
+ * to verify layency-compensation alignment
+ * (here: midi-out playback-latency + audio-in capture-latency)
+ */
+ for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
+ const pframes_t t = (*it)->timestamp();
+ assert(t < n_samples);
+ // somewhat arbitrary mapping for quick visual feedback
+ float v = -.5f;
+ if ((*it)->size() == 3) {
+ const unsigned char *d = (*it)->const_data();
+ if ((d[0] & 0xf0) == 0x90) { // note on
+ v = .25f + d[2] / 512.f;
+ }
+ else if ((d[0] & 0xf0) == 0x80) { // note off
+ v = .3f - d[2] / 640.f;
+ }
+ else if ((d[0] & 0xf0) == 0xb0) { // CC
+ v = -.1f - d[2] / 256.f;
+ }
+ }
+ _wavetable[t] += v;
+ }
+}
+
+float DummyAudioPort::grandf ()
+{
+ // Gaussian White Noise
+ // http://www.musicdsp.org/archive.php?classid=0#109
+ float x1, x2, r;
+
+ if (_pass) {
+ _pass = false;
+ return _rn1;
+ }
+
+ do {
+ x1 = randf ();
+ x2 = randf ();
+ r = x1 * x1 + x2 * x2;
+ } while ((r >= 1.0f) || (r < 1e-22f));
+
+ r = sqrtf (-2.f * logf (r) / r);
+
+ _pass = true;
+ _rn1 = r * x2;
+ return r * x1;
+}
+
+void DummyAudioPort::generate (const pframes_t n_samples)
+{
+ Glib::Threads::Mutex::Lock lm (generator_lock);
+ if (_gen_cycle) {
+ return;
+ }
+
+ switch (_gen_type) {
+ case Silence:
+ memset (_buffer, 0, n_samples * sizeof (Sample));
+ break;
+ case SquareWave:
+ assert(_gen_period > 0);
+ for (pframes_t i = 0 ; i < n_samples; ++i) {
+ if (_gen_offset < _gen_period * .5f) {
+ _buffer[i] = .40709f; // -6dBFS
+ } else {
+ _buffer[i] = -.40709f;
+ }
+ _gen_offset = (_gen_offset + 1) % _gen_period;
+ }
+ break;
+ case KronekerDelta:
+ assert(_gen_period > 0);
+ memset (_buffer, 0, n_samples * sizeof (Sample));
+ for (pframes_t i = 0; i < n_samples; ++i) {
+ if (_gen_offset == 0) {
+ _buffer[i] = 1.0f;
+ }
+ _gen_offset = (_gen_offset + 1) % _gen_period;
+ }
+ break;
+ case SineSweepSwell:
+ case SquareSweepSwell:
+ assert(_wavetable && _gen_period > 0);
+ {
+ const float vols = 2.f / (float)_gen_perio2;
+ for (pframes_t i = 0; i < n_samples; ++i) {
+ const float g = fabsf (_gen_count2 * vols - 1.0);
+ _buffer[i] = g * _wavetable[_gen_offset];
+ _gen_offset = (_gen_offset + 1) % _gen_period;
+ _gen_count2 = (_gen_count2 + 1) % _gen_perio2;
+ }
+ }
+ break;
+ case Loopback:
+ _gen_period = n_samples; // XXX DummyBackend::_samples_per_period;
+ case SineWave:
+ case SineSweep:
+ case SquareSweep:
+ assert(_wavetable && _gen_period > 0);
+ {
+ pframes_t written = 0;
+ while (written < n_samples) {
+ const uint32_t remain = n_samples - written;
+ const uint32_t to_copy = std::min(remain, _gen_period - _gen_offset);
+ memcpy((void*)&_buffer[written],
+ (void*)&_wavetable[_gen_offset],
+ to_copy * sizeof(Sample));
+ written += to_copy;
+ _gen_offset = (_gen_offset + to_copy) % _gen_period;
+ }
+ }
+ break;
+ case UniformWhiteNoise:
+ for (pframes_t i = 0 ; i < n_samples; ++i) {
+ _buffer[i] = .158489f * randf();
+ }
+ break;
+ case GaussianWhiteNoise:
+ for (pframes_t i = 0 ; i < n_samples; ++i) {
+ _buffer[i] = .089125f * grandf();
+ }
+ break;
+ case PinkNoise:
+ for (pframes_t i = 0 ; i < n_samples; ++i) {
+ // Paul Kellet's refined method
+ // http://www.musicdsp.org/files/pink.txt
+ // NB. If 'white' consists of uniform random numbers,
+ // the pink noise will have an almost gaussian distribution.
+ const float white = .0498f * randf ();
+ _b0 = .99886f * _b0 + white * .0555179f;
+ _b1 = .99332f * _b1 + white * .0750759f;
+ _b2 = .96900f * _b2 + white * .1538520f;
+ _b3 = .86650f * _b3 + white * .3104856f;
+ _b4 = .55000f * _b4 + white * .5329522f;
+ _b5 = -.7616f * _b5 - white * .0168980f;
+ _buffer[i] = _b0 + _b1 + _b2 + _b3 + _b4 + _b5 + _b6 + white * 0.5362f;
+ _b6 = white * 0.115926f;
+ }
+ break;
+ case PonyNoise:
+ for (pframes_t i = 0 ; i < n_samples; ++i) {
+ const float white = 0.0498f * randf ();
+ // Paul Kellet's economy method
+ // http://www.musicdsp.org/files/pink.txt
+ _b0 = 0.99765f * _b0 + white * 0.0990460f;
+ _b1 = 0.96300f * _b1 + white * 0.2965164f;
+ _b2 = 0.57000f * _b2 + white * 1.0526913f;
+ _buffer[i] = _b0 + _b1 + _b2 + white * 0.1848f;
+ }
+ break;
+ }
+ _gen_cycle = true;
+}
+
+void* DummyAudioPort::get_buffer (pframes_t n_samples)
+{
+ if (is_input ()) {
+ std::vector<DummyPort*>::const_iterator it = get_connections ().begin ();
+ if (it == get_connections ().end ()) {
+ memset (_buffer, 0, n_samples * sizeof (Sample));
+ } else {
+ DummyAudioPort * source = static_cast<DummyAudioPort*>(*it);
+ assert (source && source->is_output ());
+ if (source->is_physical() && source->is_terminal()) {
+ source->get_buffer(n_samples); // generate signal.
+ }
+ memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
+ while (++it != get_connections ().end ()) {
+ source = static_cast<DummyAudioPort*>(*it);
+ assert (source && source->is_output ());
+ Sample* dst = buffer ();
+ if (source->is_physical() && source->is_terminal()) {
+ source->get_buffer(n_samples); // generate signal.
+ }
+ const Sample* src = source->const_buffer ();
+ for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
+ *dst += *src;
+ }
+ }
+ }
+ } else if (is_output () && is_physical () && is_terminal()) {
+ if (!_gen_cycle) {
+ generate(n_samples);
+ }
+ }
+ return _buffer;
+}
+
+
+DummyMidiPort::DummyMidiPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
+ : DummyPort (b, name, flags)
+ , _midi_seq_spb (0)
+ , _midi_seq_time (0)
+ , _midi_seq_pos (0)
+{
+ _buffer.clear ();
+ _loopback.clear ();
+}
+
+DummyMidiPort::~DummyMidiPort () {
+ _buffer.clear ();
+ _loopback.clear ();
+}
+
+struct MidiEventSorter {
+ bool operator() (const boost::shared_ptr<DummyMidiEvent>& a, const boost::shared_ptr<DummyMidiEvent>& b) {
+ return *a < *b;
+ }
+};
+
+void DummyMidiPort::set_loopback (DummyMidiBuffer const * const src)
+{
+ _loopback.clear ();
+ for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
+ _loopback.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
+ }
+}
+
+void DummyMidiPort::setup_generator (int seq_id, const float sr)
+{
+ DummyPort::setup_random_number_generator();
+ _midi_seq_dat = DummyMidiData::sequences[seq_id % NUM_MIDI_EVENT_GENERATORS];
+ _midi_seq_spb = sr * .5f; // 120 BPM, beat_time 1.0 per beat.
+ _midi_seq_pos = 0;
+ _midi_seq_time = 0;
+}
+
+void DummyMidiPort::midi_generate (const pframes_t n_samples)
+{
+ Glib::Threads::Mutex::Lock lm (generator_lock);
+ if (_gen_cycle) {
+ return;
+ }
+
+ _buffer.clear ();
+ _gen_cycle = true;
+
+ if (_midi_seq_spb == 0 || !_midi_seq_dat) {
+ for (DummyMidiBuffer::const_iterator it = _loopback.begin (); it != _loopback.end (); ++it) {
+ _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
+ }
+ return;
+ }
+
+ while (1) {
+ const int32_t ev_beat_time = _midi_seq_dat[_midi_seq_pos].beat_time * _midi_seq_spb - _midi_seq_time;
+ if (ev_beat_time < 0) {
+ break;
+ }
+ if ((pframes_t) ev_beat_time >= n_samples) {
+ break;
+ }
+ _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (
+ ev_beat_time,
+ _midi_seq_dat[_midi_seq_pos].event,
+ _midi_seq_dat[_midi_seq_pos].size
+ )));
+ ++_midi_seq_pos;
+
+ if (_midi_seq_dat[_midi_seq_pos].event[0] == 0xff && _midi_seq_dat[_midi_seq_pos].event[1] == 0xff) {
+ _midi_seq_time -= _midi_seq_dat[_midi_seq_pos].beat_time * _midi_seq_spb;
+ _midi_seq_pos = 0;
+ }
+ }
+ _midi_seq_time += n_samples;
+}
+
+
+void* DummyMidiPort::get_buffer (pframes_t n_samples)
+{
+ if (is_input ()) {
+ _buffer.clear ();
+ for (std::vector<DummyPort*>::const_iterator i = get_connections ().begin ();
+ i != get_connections ().end ();
+ ++i) {
+ DummyMidiPort * source = static_cast<DummyMidiPort*>(*i);
+ if (source->is_physical() && source->is_terminal()) {
+ source->get_buffer(n_samples); // generate signal.
+ }
+ const DummyMidiBuffer *src = source->const_buffer ();
+ for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
+ _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
+ }
+ }
+ std::sort (_buffer.begin (), _buffer.end (), MidiEventSorter());
+ } else if (is_output () && is_physical () && is_terminal()) {
+ if (!_gen_cycle) {
+ midi_generate(n_samples);
+ }
+ }
+ return &_buffer;
+}
+
+DummyMidiEvent::DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
+ : _size (size)
+ , _timestamp (timestamp)
+ , _data (0)
+{
+ if (size > 0) {
+ _data = (uint8_t*) malloc (size);
+ memcpy (_data, data, size);
+ }
+}
+
+DummyMidiEvent::DummyMidiEvent (const DummyMidiEvent& other)
+ : _size (other.size ())
+ , _timestamp (other.timestamp ())
+ , _data (0)
+{
+ if (other.size () && other.const_data ()) {
+ _data = (uint8_t*) malloc (other.size ());
+ memcpy (_data, other.const_data (), other.size ());
+ }
+};
+
+DummyMidiEvent::~DummyMidiEvent () {
+ free (_data);
+};