summaryrefslogtreecommitdiff
path: root/src/sms/synthesis.c
blob: d3caa6ce612af38b6cec7a3357e3e2c87887c59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* 
 * Copyright (c) 2008 MUSIC TECHNOLOGY GROUP (MTG)
 *                         UNIVERSITAT POMPEU FABRA 
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */
/*! \file synthesis.c
 * \brief main synthesis routines
 */
#include "sms.h"

/*! \brief synthesis of one frame of the deterministic component using the IFFT
 *
 * \param pSmsData pointer to SMS data structure frame
 * \param pSynthParams pointer to structure of synthesis parameters
 */
static void SineSynthIFFT(SMS_Data *pSmsData, SMS_SynthParams *pSynthParams)
{
    int sizeFft = pSynthParams->sizeHop << 1; 
    int iHalfSamplingRate = pSynthParams->iSamplingRate >> 1;
    int sizeMag = pSynthParams->sizeHop;
    int nBins = 8;
    int nTracks = pSmsData->nTracks;
    int iFirstBin, k, i, l, b;
    sfloat fMag=0.0, fFreq=0.0, fPhase=0.0, fLoc, fSin, fCos, fBinRemainder, 
           fTmp, fNewMag,  fIndex;
    sfloat fSamplingPeriod = 1.0 / pSynthParams->iSamplingRate;
    memset(pSynthParams->pSpectra, 0, sizeFft * sizeof(sfloat));
    for(i = 0; i < nTracks; i++)
    {
        if(((fMag = pSmsData->pFSinAmp[i]) > 0) &&
            ((fFreq = (pSmsData->pFSinFreq[i])) < iHalfSamplingRate))
        {
            /* \todo maybe this check can be removed if the SynthParams->prevFrame gets random
               phases in sms_initSynth? */
            if(pSynthParams->prevFrame.pFSinAmp[i] <= 0)
               pSynthParams->prevFrame.pFSinPha[i] = TWO_PI * sms_random();

            fMag = sms_dBToMag(fMag);
            fTmp = pSynthParams->prevFrame.pFSinPha[i] +
                   TWO_PI * fFreq * fSamplingPeriod * sizeMag;
            fPhase = fTmp - floor(fTmp * INV_TWO_PI) * TWO_PI;
            fLoc = sizeFft * fFreq  * fSamplingPeriod;
            iFirstBin = (int) fLoc - 3;
            fBinRemainder = fLoc - floor (fLoc);
            fSin = sms_sine(fPhase);
            fCos = sms_sine(fPhase + PI_2);
            for (k = 1, l = iFirstBin; k <= nBins; k++, l++)
            {
                fIndex = (k - fBinRemainder);
                if (fIndex > 7.999) fIndex = 0;
                fNewMag = fMag * sms_sinc (fIndex);
                if(l > 0 && l < sizeMag)
                {
                    pSynthParams->pSpectra[l*2+1] += fNewMag * fCos;
                    pSynthParams->pSpectra[l*2] += fNewMag * fSin;
                }
                else if(l == 0)
                {
                    pSynthParams->pSpectra[0] += 2 * fNewMag * fSin;
                }
                else if(l < 0)
                {
                    b = abs(l);
                    pSynthParams->pSpectra[b*2+1] -= fNewMag * fCos;
                    pSynthParams->pSpectra[b*2] += fNewMag * fSin;
                }
                else if(l > sizeMag)
                {
                    b = sizeMag - (l - sizeMag);
                    pSynthParams->pSpectra[b*2+1] -= fNewMag * fCos;
                    pSynthParams->pSpectra[b*2] += fNewMag * fSin;
                }
                else if(l == sizeMag)
                {
                    pSynthParams->pSpectra[1] += 2 * fNewMag * fSin;
                }
            }
        }
        pSynthParams->prevFrame.pFSinAmp[i] = fMag;
        pSynthParams->prevFrame.pFSinPha[i] = fPhase;
        pSynthParams->prevFrame.pFSinFreq[i] = fFreq;
    }

    sms_ifft(sizeFft, pSynthParams->pSpectra);

    for(i = 0, k = sizeMag; i < sizeMag; i++, k++) 
        pSynthParams->pSynthBuff[i] += pSynthParams->pSpectra[k] * pSynthParams->pFDetWindow[i];
    for(i= sizeMag, k = 0; i < sizeFft; i++, k++) 
        pSynthParams->pSynthBuff[i] +=  pSynthParams->pSpectra[k] * pSynthParams->pFDetWindow[i];
}

/*! \brief synthesis of one frame of the stochastic component by apprimating phases
 * 
 * computes a linearly interpolated spectral envelope to fit the correct number of output
 * audio samples. Phases are generated randomly.  
 *
 * \param pSmsData pointer to the current SMS frame
 * \param pSynthParams pointer to a strucure of synthesis parameters
 * \return 
 * \todo cleanup returns and various constant multipliers. check that approximation is ok
 */
static int StocSynthApprox(SMS_Data *pSmsData, SMS_SynthParams *pSynthParams)
{       
    int i, sizeSpec1Used;
    int sizeSpec1 = pSmsData->nCoeff;
    int sizeSpec2 = pSynthParams->sizeHop;
    int sizeFft = pSynthParams->sizeHop << 1; /* 50% overlap, so sizeFft is 2x sizeHop */

    /* if no gain or no coefficients return  */
    if(*(pSmsData->pFStocGain) <= 0)
        return 0;

    sizeSpec1Used = sizeSpec1 * pSynthParams->iSamplingRate / pSynthParams->iOriginalSRate;

    /* sizeSpec1Used cannot be more than what is available  \todo check by graph */
    if(sizeSpec1Used  > sizeSpec1) sizeSpec1Used = sizeSpec1;

    sms_spectralApprox(pSmsData->pFStocCoeff, sizeSpec1, sizeSpec1Used,
                       pSynthParams->pMagBuff, sizeSpec2, sizeSpec1Used,
                       pSynthParams->approxEnvelope);

    /* generate random phases */
    for(i = 0; i < sizeSpec2; i++)
        pSynthParams->pPhaseBuff[i] =  TWO_PI * sms_random();

    sms_invQuickSpectrumW(pSynthParams->pMagBuff, pSynthParams->pPhaseBuff,
                          sizeFft, pSynthParams->pSynthBuff, sizeFft,
                          pSynthParams->pFStocWindow, pSynthParams->pSpectra);
    return 1;
}

/*! \brief synthesizes one frame of the residual signal
 *
 * \param residualParams Parameters and memory for residual synthesis
 */
void sms_approxResidual(int sizeResidual, sfloat* residual,
                        int sizeApprox, sfloat* approx, 
                        SMS_ResidualParams *residualParams)
{
    int i;

    /* shift buffers */
    memcpy(residualParams->residual,
           residualParams->residual + residualParams->hopSize,
           sizeof(sfloat) * residualParams->hopSize);
    memcpy(residualParams->residual + residualParams->hopSize, residual,
           sizeof(sfloat) * residualParams->hopSize);

    memcpy(residualParams->approx,
           residualParams->approx + residualParams->hopSize,
           sizeof(sfloat) * residualParams->hopSize);
    memset(residualParams->approx + residualParams->hopSize, 0, 
           sizeof(sfloat) * residualParams->hopSize);

    sms_spectrumMag(residualParams->residualSize,
                    residualParams->residual,
                    residualParams->fftWindow,
                    residualParams->sizeStocMagSpectrum,
                    residualParams->stocMagSpectrum,
                    residualParams->fftBuffer);

    if(residualParams->sizeStocMagSpectrum != residualParams->nCoeffs)
    {
        sms_spectralApprox(residualParams->stocMagSpectrum,
                           residualParams->sizeStocMagSpectrum,
                           residualParams->sizeStocMagSpectrum,
                           residualParams->stocCoeffs,
                           residualParams->nCoeffs,
                           residualParams->nCoeffs,
                           residualParams->approxEnvelope);

        sms_spectralApprox(residualParams->stocCoeffs,
                           residualParams->nCoeffs,
                           residualParams->nCoeffs,
                           residualParams->stocMagSpectrum,
                           residualParams->sizeStocMagSpectrum,
                           residualParams->sizeStocMagSpectrum,
                           residualParams->approxEnvelope);
    }

    /* generate random phases */
    for(i = 0; i < residualParams->sizeStocMagSpectrum; i++)
    {
        residualParams->stocPhaseSpectrum[i] =  TWO_PI * sms_random();
    }

    /* IFFT with 50% overlap */
    sms_invQuickSpectrumW(residualParams->stocMagSpectrum,
                          residualParams->stocPhaseSpectrum,
                          residualParams->sizeStocMagSpectrum*2,
                          residualParams->approx,
                          residualParams->residualSize,
                          residualParams->ifftWindow,
                          residualParams->fftBuffer);

    /* output */
    for(i = 0; i < sizeApprox; i++)
    {
        approx[i] = residualParams->approx[i] * residualParams->windowScale;
    }
}

/*! \brief  synthesizes one frame of SMS data
 *
 * \param pSmsData     input SMS data
 * \param pFSynthesis  output sound buffer  
 * \param pSynthParams synthesis parameters
 */
void sms_synthesize(SMS_Data *pSmsData, sfloat *pFSynthesis,  SMS_SynthParams *pSynthParams)
{
    int i;
    int sizeHop = pSynthParams->sizeHop;

    memcpy(pSynthParams->pSynthBuff, (sfloat *)(pSynthParams->pSynthBuff+sizeHop), 
           sizeof(sfloat) * sizeHop);
    memset(pSynthParams->pSynthBuff+sizeHop, 0, sizeof(sfloat) * sizeHop);

    /* convert mags from linear to db */
    sms_arrayMagToDB(pSmsData->nTracks, pSmsData->pFSinAmp);

    /* decide which combo of synthesis methods to use */
    if(pSynthParams->iSynthesisType == SMS_STYPE_ALL)
    {
        if(pSynthParams->iDetSynthType == SMS_DET_IFFT)
        {
            SineSynthIFFT(pSmsData, pSynthParams);
        }
        else /*pSynthParams->iDetSynthType == SMS_DET_SIN*/
        {
            sms_sineSynthFrame(pSmsData, pSynthParams->pSynthBuff, pSynthParams->sizeHop,
                               &(pSynthParams->prevFrame), pSynthParams->iSamplingRate);
        }
        StocSynthApprox(pSmsData, pSynthParams);
    }
    else if(pSynthParams->iSynthesisType == SMS_STYPE_DET)
    {
        if(pSynthParams->iDetSynthType == SMS_DET_IFFT)
        {
            SineSynthIFFT(pSmsData, pSynthParams);
        }
        else /*pSynthParams->iDetSynthType == SMS_DET_SIN*/
        {
            sms_sineSynthFrame(pSmsData, pSynthParams->pSynthBuff, pSynthParams->sizeHop,
                               &(pSynthParams->prevFrame), pSynthParams->iSamplingRate);
        }
    }
    else /* pSynthParams->iSynthesisType == SMS_STYPE_STOC */
    {
        StocSynthApprox(pSmsData, pSynthParams);
    }

    /* de-emphasize the sound and normalize*/
    for(i = 0; i < sizeHop; i++)
    {
        pFSynthesis[i] = sms_deEmphasis(pSynthParams->pSynthBuff[i], pSynthParams);
    }
}