annotate audioio/ContinuousSynth.cpp @ 317:abfde177731f tonioni

Free, not delete
author Chris Cannam
date Thu, 09 Jan 2014 14:14:02 +0000
parents 65b75e23bbd5
children 8611eb7be689
rev   line source
Chris@313 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@313 2
Chris@313 3 /*
Chris@313 4 Sonic Visualiser
Chris@313 5 An audio file viewer and annotation editor.
Chris@313 6 Centre for Digital Music, Queen Mary, University of London.
Chris@313 7
Chris@313 8 This program is free software; you can redistribute it and/or
Chris@313 9 modify it under the terms of the GNU General Public License as
Chris@313 10 published by the Free Software Foundation; either version 2 of the
Chris@313 11 License, or (at your option) any later version. See the file
Chris@313 12 COPYING included with this distribution for more information.
Chris@313 13 */
Chris@313 14
Chris@313 15 #include "ContinuousSynth.h"
Chris@313 16
Chris@313 17 #include "base/Debug.h"
Chris@313 18
Chris@313 19 #include <cmath>
Chris@313 20
Chris@313 21 ContinuousSynth::ContinuousSynth(int channels, int sampleRate, int blockSize) :
Chris@313 22 m_channels(channels),
Chris@313 23 m_sampleRate(sampleRate),
Chris@313 24 m_blockSize(blockSize),
Chris@313 25 m_prevF0(-1.f),
Chris@313 26 m_phase(0.0)
Chris@313 27 {
Chris@313 28 }
Chris@313 29
Chris@313 30 ContinuousSynth::~ContinuousSynth()
Chris@313 31 {
Chris@313 32 }
Chris@313 33
Chris@313 34 void
Chris@313 35 ContinuousSynth::reset()
Chris@313 36 {
Chris@313 37 m_phase = 0;
Chris@313 38 }
Chris@313 39
Chris@313 40 void
Chris@313 41 ContinuousSynth::mix(float **toBuffers, float gain, float pan, float f0)
Chris@313 42 {
Chris@313 43 if (f0 == 0.f) f0 = m_prevF0;
Chris@313 44
Chris@313 45 bool wasOn = (m_prevF0 > 0.f);
Chris@313 46 bool nowOn = (f0 > 0.f);
Chris@313 47
Chris@313 48 if (!nowOn && !wasOn) {
Chris@313 49 m_phase = 0;
Chris@313 50 return;
Chris@313 51 }
Chris@313 52
Chris@315 53 int fadeLength = 100; // samples
Chris@313 54
Chris@313 55 float *levels = new float[m_channels];
Chris@313 56
Chris@313 57 for (int c = 0; c < m_channels; ++c) {
Chris@313 58 levels[c] = gain;
Chris@313 59 }
Chris@313 60 if (pan != 0.0 && m_channels == 2) {
Chris@313 61 levels[0] *= 1.0 - pan;
Chris@313 62 levels[1] *= pan + 1.0;
Chris@313 63 }
Chris@313 64
Chris@315 65 // cerr << "ContinuousSynth::mix: f0 = " << f0 << " (from " << m_prevF0 << "), phase = " << m_phase << endl;
Chris@313 66
Chris@313 67 for (int i = 0; i < m_blockSize; ++i) {
Chris@313 68
Chris@315 69 double fHere = (nowOn ? f0 : m_prevF0);
Chris@315 70
Chris@315 71 if (wasOn && nowOn && (f0 != m_prevF0) && (i < fadeLength)) {
Chris@315 72 // interpolate the frequency shift
Chris@315 73 fHere = m_prevF0 + ((f0 - m_prevF0) * i) / fadeLength;
Chris@315 74 }
Chris@315 75
Chris@315 76 double phasor = (fHere * 2 * M_PI) / m_sampleRate;
Chris@315 77
Chris@315 78 // cerr << "phasor = " << phasor << endl;
Chris@315 79
Chris@315 80 m_phase = m_phase + phasor;
Chris@313 81
Chris@315 82 double v = sin(m_phase);
Chris@313 83
Chris@315 84 if (!wasOn && i < fadeLength) {
Chris@315 85 // fade in
Chris@313 86 v = v * (i / double(fadeLength));
Chris@313 87 } else if (!nowOn) {
Chris@315 88 // fade out
Chris@313 89 if (i > fadeLength) v = 0;
Chris@313 90 else v = v * (1.0 - (i / double(fadeLength)));
Chris@313 91 }
Chris@313 92
Chris@313 93 for (int c = 0; c < m_channels; ++c) {
Chris@313 94 toBuffers[c][i] += levels[c] * v;
Chris@313 95 }
Chris@313 96 }
Chris@313 97
Chris@313 98 m_prevF0 = f0;
Chris@313 99
Chris@313 100 delete[] levels;
Chris@313 101 }
Chris@313 102