File: PVA.cpp

package info (click to toggle)
sndobj 2.6.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,368 kB
  • ctags: 5,467
  • sloc: ansic: 55,155; cpp: 19,258; python: 348; makefile: 125
file content (167 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download | duplicates (2)
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
 
////////////////////////////////////////////////////////////////////////
// This file is part of the SndObj library
//
// 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 
//
// Copyright (c)Victor Lazzarini, 1997-2004
// See License.txt for a disclaimer of all warranties
// and licensing information

/////////////////////////////////////////////////
// PVA.cpp: Phase Vocoder Analysis class
//
//           Victor Lazzarini, 2003
/////////////////////////////////////////////////
#include "PVA.h"

#include <cstring>

PVA::PVA(){
  m_rotcount = 0;
  m_phases = new float[m_halfsize];
  memset(m_phases, 0, sizeof(float)*m_halfsize);
  m_factor = m_sr/(m_hopsize*TWOPI);
}


PVA::PVA(Table* window, SndObj* input, float scale,
	 int fftsize, int hopsize, float sr)
  :FFT(window, input, scale, fftsize, hopsize, sr)
{
  m_rotcount = 0;
  m_phases = new float[m_halfsize];
  memset(m_phases, 0, sizeof(float)*m_halfsize);
  m_factor = m_sr/(m_hopsize*TWOPI);
}


PVA::~PVA(){
  delete[] m_phases;

}


int
PVA::Set(char* mess, float value){

  switch(FindMsg(mess)){

  case 22:
    SetFFTSize((int) value);
    return 1;

  case 23:
    SetHopSize((int) value);
    return 1;
	
  default:
    return	FFT::Set(mess, value);

  }
}
void
PVA::SetFFTSize(int fftsize){
  m_rotcount = 0;
  FFT::SetFFTSize(fftsize);
}

void
PVA::SetHopSize(int hopsize){
  m_rotcount = 0;
  m_factor = m_sr/(hopsize*TWOPI);
  FFT::SetFFTSize(hopsize);
}

void
PVA::pvanalysis(float* signal){

  double re, im, pha, diff; 
  int i2;
  
  rfftw_one(m_plan, signal, m_ffttmp);

  m_output[0] = m_ffttmp[0]/m_norm;
  m_output[1] = m_ffttmp[m_halfsize]/m_norm;
  
  for(int i=2; i<m_fftsize; i+=2){
    i2 = i/2;
    re = m_ffttmp[i2]/m_norm;
    im = m_ffttmp[m_fftsize-(i2)]/m_norm;

    if((m_output[i] = sqrt((re*re)+(im*im)))==0.f){
      diff = 0.f; 
    }
    else {
      pha = atan2(im,re);
      diff = pha - m_phases[i2];
      m_phases[i2] = (float) pha;

      while(diff > PI) diff -= TWOPI;
      while(diff < -PI) diff += TWOPI;
    }
    m_output[i+1] = (float) diff*m_factor + i2*m_fund;

  }

}


short
PVA::DoProcess(){

  if(!m_error){
    if(m_input){
      if(m_enable){
	int i; float sig = 0.f;
	for(m_vecpos = 0; m_vecpos < m_hopsize; m_vecpos++) {
	  // signal input
	  sig = m_input->Output(m_vecpos);		
	  // distribute to the signal fftframes and apply the window
	  // according to a time pointer (kept by counter[n])
	  // input is also rotated according to the input time.
	  for(i=0;i < m_frames; i++){
	    m_sigframe[i][m_rotcount]= (float) sig*m_table->Lookup(m_counter[i]);
	    m_counter[i]++;		   
	  }  
	  m_rotcount++;
	} 
	m_rotcount %= m_fftsize;
	// every vecsize samples
	// set the current fftframe to be transformed
	m_cur--; if(m_cur<0) m_cur = m_frames-1;  
 
	// phase vocoder analysis
	pvanalysis(m_sigframe[m_cur]);

	// zero the current fftframe time pointer
	m_counter[m_cur] = 0;
	return 1;

      } else { // if disabled, reset the fftframes
	for(m_vecpos =0; m_vecpos < m_hopsize; m_vecpos++)
	  m_output[m_vecpos] = 0.f;
	return 1;
      }

    } else {
      m_error = 3;
      return 0;
    }
  }
  else 
    return 0;
}