From 6d2a680a5c3ef0d20d10547e008aa70d2cee342d Mon Sep 17 00:00:00 2001 From: pouillot Date: Sat, 30 Mar 2013 14:27:47 +0000 Subject: [PATCH] Re #802 You can now choose to use or ignore partials phase during synthesis git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/subprojects/soundeditor/trunk@5373 30fe4595-0a0c-4342-8851-515496e4dcbd --- atsa/synthesis.c | 30 +++++++------ qatsh/ATSSynthesis.cpp | 27 ++++++------ qatsh/ATSSynthesis.h | 6 +-- qatsh/DocumentWindow.cpp | 2 +- qatsh/SynthesisParamsWidget.cpp | 6 +-- qatsh/SynthesisParamsWidget.ui | 74 +++++++++++++++++++++++++++++---- qatsh/TableAndToolsWidget.cpp | 1 - 7 files changed, 100 insertions(+), 46 deletions(-) diff --git a/atsa/synthesis.c b/atsa/synthesis.c index ef26d4e..2b748bd 100644 --- a/atsa/synthesis.c +++ b/atsa/synthesis.c @@ -142,11 +142,9 @@ void make_sine_table() //////////////////////////////////////////////////////////////////// double ioscilator(double amp, double freq, double pha, double sr, double *oscpt) { - const double incr = freq * (double)SINE_TABLE_LEN / sr; - // Phase management = 3 options (uncomment only one) : // 1) No phase = 0 phase. - const double osc = *oscpt; + //const double osc = *oscpt; // 2) Random phase. //static const int max_pha_shift = 300; //(SINE_TABLE_LEN / 2) / 1000; @@ -156,8 +154,8 @@ double ioscilator(double amp, double freq, double pha, double sr, double *oscpt) //const double osc = fmod(*oscpt + SINE_TABLE_LEN + pha_shift, SINE_TABLE_LEN); // 3) Shipped phase (linear interpolation). - //const int pha_shift = (int)(pha * (double)SINE_TABLE_LEN / TWO_PI); - //const double osc = fmod(*oscpt + SINE_TABLE_LEN + pha_shift, SINE_TABLE_LEN); + const int pha_shift = (int)(pha * (double)SINE_TABLE_LEN / TWO_PI); + const double osc = fmod(*oscpt + SINE_TABLE_LEN + pha_shift, SINE_TABLE_LEN); double output; int curr_ind, next_ind; @@ -173,6 +171,7 @@ double ioscilator(double amp, double freq, double pha, double sr, double *oscpt) * (osc - curr_ind)); // Update oscillator index. + const double incr = freq * (double)SINE_TABLE_LEN / sr; *oscpt = fmod(*oscpt + incr, SINE_TABLE_LEN); return output; @@ -262,12 +261,11 @@ double locate_frame(ATS_SOUND *ats_sound, double from_frame, double time) } */ //////////////////////////////////////////////////////////////////// -//Synthesizes a Buffer NOT using phase interpolation int synth_deterministic_only(double ampl_p, double ampl_c, double ampl_n, double freq_p, double freq_c, double freq_n, double pha_p, double pha_c, double pha_n, double time_offset, double duration, double sample_rate, - double *oscpt, double* sample_buf) + short use_phase, double *oscpt, double* sample_buf) { int s, frame_samps; double ampl, freq, pha, frame_offset; @@ -281,7 +279,7 @@ int synth_deterministic_only(double ampl_p, double ampl_c, double ampl_n, frame_offset = (time_offset + s / sample_rate) / duration; ampl = LIN3_INTERP(frame_offset, ampl_p, ampl_c, ampl_n); freq = LIN3_INTERP(frame_offset, freq_p, freq_c, freq_n); - pha = 0; // Temporary: LIN3_INTERP(frame_offset, pha_p, pha_c, pha_n); + pha = (use_phase ? LIN3_INTERP(frame_offset, pha_p, pha_c, pha_n) : 0.); sample_buf[s] += ioscilator(ampl, freq, pha, sample_rate, oscpt); } @@ -304,7 +302,7 @@ int synth_residual_only(double ampl_p, double ampl_c, double ampl_n, for(s = 0; s < frame_samps; s++) { frame_offset = (time_offset + s / sample_rate) / duration; ampl = LIN3_INTERP(frame_offset, ampl_p, ampl_c, ampl_n); - pha = 0.; // Temporary. Should we use a random phase here ? + pha = 0.; // Shouldn't we use a random phase here ? sample_buf[s] += ioscilator(ampl, freq, pha, sample_rate, oscpt) * randi(rdata); } @@ -316,7 +314,7 @@ int synth_both(double ampl_p, double ampl_c, double ampl_n, double pha_p, double pha_c, double pha_n, double resid_p, double resid_c, double resid_n, double time_offset, double duration, double sample_rate, - double *oscpt, RANDI* rdata, double* sample_buf) + short use_phase, double *oscpt, RANDI* rdata, double* sample_buf) { int s, frame_samps; double ampl, freq, pha, resid, rfreq, frame_offset; @@ -336,7 +334,7 @@ int synth_both(double ampl_p, double ampl_c, double ampl_n, frame_offset = (time_offset + s / sample_rate) / duration; ampl = LIN3_INTERP(frame_offset, ampl_p, ampl_c, ampl_n); freq = LIN3_INTERP(frame_offset, freq_p, freq_c, freq_n); - pha = 0; // Temporary: LIN3_INTERP(frame_offset, pha_p, pha_c, pha_n); + pha = (use_phase ? LIN3_INTERP(frame_offset, pha_p, pha_c, pha_n) : 0.); resid = LIN3_INTERP(frame_offset, resid_p, resid_c, resid_n); rfreq = LIN3_INTERP(frame_offset, rfreq_p, rfreq_c, rfreq_n); sample_buf[s] += @@ -392,7 +390,7 @@ void do_synthesis(ATS_SOUND *ats_sound, SPARAMS* sparams, CURVE* timenv, int *se dur = sparams->end - sparams->beg; todo=0; bframe = 0.; - fprintf(stdout, "do_synthesis: nbp=%d, dur=%f, dy=%f\n", nbp, dur, dy); + fprintf(stdout, "do_synthesis: nbp=%d, dur=%f, dy=%f, pha=%d\n", nbp, dur, dy, sparams->upha); for(p=0; p < nbp - 1; ++p){ @@ -514,7 +512,7 @@ void do_synthesis(ATS_SOUND *ats_sound, SPARAMS* sparams, CURVE* timenv, int *se if(sparams->ramp == 0.) { //deterministic synthesis only for(d = 0; d < ats_sound->partials; d++) { - if (selected && !selected[d]) + if (sparams->allorsel && selected && !selected[d]) continue; ampl_p = ats_sound->amp[d][f_p] * sparams->amp; ampl_c = ats_sound->amp[d][f] * sparams->amp; @@ -527,7 +525,7 @@ void do_synthesis(ATS_SOUND *ats_sound, SPARAMS* sparams, CURVE* timenv, int *se pha_n = ats_sound->pha[d][f_n]; frame_samps = synth_deterministic_only(ampl_p, ampl_c, ampl_n, freq_p, freq_c, freq_n, pha_p, pha_c, pha_n, - time_offset, duration, sparams->sr, + time_offset, duration, sparams->sr, sparams->upha, ospt+d, *out_samps + n_samps); } } else if(sparams->amp == 0.) { @@ -547,7 +545,7 @@ void do_synthesis(ATS_SOUND *ats_sound, SPARAMS* sparams, CURVE* timenv, int *se } else { //residual and deterministic synthesis for(d = 0; d < ats_sound->partials; d++) { - if (selected && !selected[d]) + if (sparams->allorsel && selected && !selected[d]) continue; ampl_p = ats_sound->amp[d][f_p] * sparams->amp; ampl_c = ats_sound->amp[d][f] * sparams->amp; @@ -566,7 +564,7 @@ void do_synthesis(ATS_SOUND *ats_sound, SPARAMS* sparams, CURVE* timenv, int *se * sparams->ramp; frame_samps = synth_both(ampl_p, ampl_c, ampl_n, freq_p, freq_c, freq_n, pha_p, pha_c, pha_n, resid_p, resid_c, resid_n, - time_offset, duration, sparams->sr, + time_offset, duration, sparams->sr, sparams->upha, ospt+d, &rarray[p], *out_samps + n_samps); } } diff --git a/qatsh/ATSSynthesis.cpp b/qatsh/ATSSynthesis.cpp index d46fa87..65ce94c 100644 --- a/qatsh/ATSSynthesis.cpp +++ b/qatsh/ATSSynthesis.cpp @@ -14,9 +14,9 @@ ATSSynthesis::ATSSynthesis(double dStartTime, double dDuration, - bool bAllParts, bool bUsePartsPhase, - double dPartsAmplFactor, double dResidAmplFactor, - double dFreqFactor, double dTimeFactor, + bool bAllParts, bool bUsePartsPhase, + double dPartsAmplFactor, double dResidAmplFactor, + double dFreqFactor, double dTimeFactor, double dSampRate) { setStartTime(dStartTime); @@ -48,14 +48,14 @@ ATSSynthesis& ATSSynthesis::operator=(const ATSSynthesis& src) bool ATSSynthesis::operator==(const ATSSynthesis& src) const { return startTime() == src.startTime() - && duration() == src.duration() - && useOnlySelectedPartials() == src.useOnlySelectedPartials() - && usePartialsPhase() == src.usePartialsPhase() - && partialsAmplitudeFactor() == src.partialsAmplitudeFactor() - && residualAmplitudeFactor() == src.residualAmplitudeFactor() - && frequencyFactor() == src.frequencyFactor() - && timeFactor() == src.timeFactor() - && samplingRate() == src.samplingRate(); + && duration() == src.duration() + && useOnlySelectedPartials() == src.useOnlySelectedPartials() + && usePartialsPhase() == src.usePartialsPhase() + && partialsAmplitudeFactor() == src.partialsAmplitudeFactor() + && residualAmplitudeFactor() == src.residualAmplitudeFactor() + && frequencyFactor() == src.frequencyFactor() + && timeFactor() == src.timeFactor() + && samplingRate() == src.samplingRate(); } bool ATSSynthesis::operator!=(const ATSSynthesis& src) const @@ -92,15 +92,14 @@ SampledSound* ATSSynthesis::operator()(ATSSound* pATSSound, const std::vectordata(), &_params, &timeEnvelop, aSelectedPartials, &adSamples[0], &nSamples); curve_free(&timeEnvelop); - if (aSelectedPartials) - delete [] aSelectedPartials; + delete [] aSelectedPartials; // Build a SampleSound from these samples (monophonic => 1 channel). SampledSound* pSampSound = 0; if (adSamples[0] && nSamples > 0) { pSampSound = new SampledSound(1, nSamples, (int)samplingRate(), adSamples); - free(adSamples[0]); + free(adSamples[0]); } return pSampSound; diff --git a/qatsh/ATSSynthesis.h b/qatsh/ATSSynthesis.h index 7cee325..27ddb43 100644 --- a/qatsh/ATSSynthesis.h +++ b/qatsh/ATSSynthesis.h @@ -20,9 +20,9 @@ class ATSSynthesis { public: ATSSynthesis(double dStartTime=0.0, double dDuration=0.0, - bool bAllParts=true, bool bUsePartsPhase=false, - double dPartsAmplFactor=1.0, double dResidAmplFactor=1.0, - double dFreqFactor=1.0, double dTimeFactor=1.0, + bool bAllParts=true, bool bUsePartsPhase=false, + double dPartsAmplFactor=1.0, double dResidAmplFactor=1.0, + double dFreqFactor=1.0, double dTimeFactor=1.0, double dSampRate=44100); SampledSound* operator()(ATSSound* pATSSound, const std::vector& vecSelPartIndexes); diff --git a/qatsh/DocumentWindow.cpp b/qatsh/DocumentWindow.cpp index 3376f32..5b63091 100644 --- a/qatsh/DocumentWindow.cpp +++ b/qatsh/DocumentWindow.cpp @@ -281,7 +281,7 @@ bool DocumentWindow::synthesiseFile(const QString &qsFileName, // Free now useless sampled sound. delete pSampSound; - // Save a copy of the synthesis inputs for later. + // Save a copy of the synthesis inputs for later. _qsLastSyntResFileName = qsFileName; if (!_pLastSyntParams) _pLastSyntParams = new ATSSynthesis(); diff --git a/qatsh/SynthesisParamsWidget.cpp b/qatsh/SynthesisParamsWidget.cpp index e43465e..3237db3 100644 --- a/qatsh/SynthesisParamsWidget.cpp +++ b/qatsh/SynthesisParamsWidget.cpp @@ -26,8 +26,6 @@ SynthesisParamsWidget::SynthesisParamsWidget(QWidget *parent) SynthesisParamsWidget::~SynthesisParamsWidget() { delete _pui; - - if (_pSynthesis) delete _pSynthesis; } @@ -51,6 +49,7 @@ void SynthesisParamsWidget::onRestoreDefaults() _pui->qsbStartTime->setValue(0.0); _pui->qsbDuration->setValue(_dMaxDuration); _pui->qrbAllPartials->setChecked(!synt.useOnlySelectedPartials()); + _pui->qrbUsePhase->setChecked(synt.usePartialsPhase()); _pui->qsbPartAmplFactor->setValue(synt.partialsAmplitudeFactor()); _pui->qsbResidAmplFactor->setValue(synt.residualAmplitudeFactor()); _pui->qsbFreqFactor->setValue(synt.frequencyFactor()); @@ -76,11 +75,12 @@ void SynthesisParamsWidget::setMaxDuration(double dMaxDuration) void SynthesisParamsWidget::setupSynthesis() { if (!_pSynthesis) - _pSynthesis = new ATSSynthesis(); + _pSynthesis = new ATSSynthesis(); _pSynthesis->setStartTime(_pui->qsbStartTime->value()); _pSynthesis->setDuration(_pui->qsbDuration->value()); _pSynthesis->setUseOnlySelectedPartials(!_pui->qrbAllPartials->isChecked()); + _pSynthesis->setUsePartialsPhase(_pui->qrbUsePhase->isChecked()); _pSynthesis->setPartialsAmplitudeFactor(_pui->qsbPartAmplFactor->value()); _pSynthesis->setResidualAmplitudeFactor(_pui->qsbResidAmplFactor->value()); _pSynthesis->setFrequencyFactor(_pui->qsbFreqFactor->value()); diff --git a/qatsh/SynthesisParamsWidget.ui b/qatsh/SynthesisParamsWidget.ui index 8e96d47..94f8e18 100644 --- a/qatsh/SynthesisParamsWidget.ui +++ b/qatsh/SynthesisParamsWidget.ui @@ -7,7 +7,7 @@ 0 0 302 - 191 + 230 @@ -101,6 +101,9 @@ true + + bgPartials + @@ -111,16 +114,67 @@ Selection + + bgPartials + + + + + 0 + 0 + + + + + 0 + 0 + + + + Use partials phase + + + + + + + true + + + Yes + + + bgPhase + + + + + + + true + + + No + + + true + + + bgPhase + + + + Partials amplitude factor - + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -136,14 +190,14 @@ - + Residual amplitude factor - + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -159,14 +213,14 @@ - + Frequency factor - + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -185,14 +239,14 @@ - + Sampling rate - + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -217,4 +271,8 @@ + + + + diff --git a/qatsh/TableAndToolsWidget.cpp b/qatsh/TableAndToolsWidget.cpp index 322746d..05e4cf5 100644 --- a/qatsh/TableAndToolsWidget.cpp +++ b/qatsh/TableAndToolsWidget.cpp @@ -57,7 +57,6 @@ TableAndToolsWidget::TableAndToolsWidget(QWidget *parent) TableAndToolsWidget::~TableAndToolsWidget() { delete _pui; - if (_pProcessor) delete _pProcessor; }