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
|
// pvocdata.C
/******************************************************************************
*
* MiXViews - an X window system based sound & data editor/processor
*
* Copyright (c) 1993, 1994 Regents of the University of California
*
* Author: Douglas Scott
* Date: December 13, 1994
*
* Permission to use, copy and modify this software and its documentation
* for research and/or educational purposes and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation. The author reserves the right to distribute this
* software and its documentation. The University of California and the author
* make no representations about the suitability of this software for any
* purpose, and in no event shall University of California be liable for any
* damage, loss of data, or profits resulting from its use.
* It is provided "as is" without express or implied warranty.
*
******************************************************************************/
#ifdef __GNUG__
#pragma implementation
#endif
#include "localdefs.h"
#include "controller.h"
#include "envelope.h"
#include "cs_pvocheader.h"
#include "pvocheader.h"
#include "pvocdata.h"
#include "request.h"
// class members and methods
int PvocData::default_NumberOfBands = 512;
double PvocData::default_FrameRate = 200.0;
void
PvocData::setDefaultNumberOfBands(int bands) { default_NumberOfBands = bands; }
void
PvocData::setDefaultFrameRate(double rate) { default_FrameRate = rate; }
// object methods
Data *
PvocData::newData() { return new PvocData(this); }
Data *
PvocData::newData(int length) { return new PvocData(this, length); }
Data *
PvocData::clone(const Range &r) { return new PvocData(this, r); }
Data *
PvocData::clone(const Range &r, const Range &c) {
return new PvocData(this, r, c);
}
void
PvocData::print(FILE *out) {
int npoints = channels();
fprintf(out, "Phase Vocoder Data Dump\n");
for(int frame=0; frame < length(); frame++) {
fprintf(out, "Frame %d:\n", frame);
for(int i = 0; i < npoints; i=i+2) {
fprintf(out, "\tFreq:\t%0.2f\tAmp:\t%0.4f\n",
get(frame, i+1), get(frame, i));
}
}
fflush(out);
}
// returns specific range for freq bands, and max range for all amp bands
Range
PvocData::limits(int chan, boolean real) const {
Range limit;
if(chan % 2 != 0)
return Data::limits(chan);
else {
double peak = maxValue();
return limit.set(0.0, peak);
}
}
void
PvocData::information(Controller *controller) {
AlertRequest request("Phase Vocoder Data Information:");
request.setBell(false);
char str[128];
request.appendLabel("------------");
request.appendLabel("Filename: ", controller->fileName());
request.appendLabel("Length (frames): ", toString(str, length()));
request.appendLabel("Frequency Bands: ", toString(str, nBands()));
request.appendLabel("Frame Rate: ", toString(str, frameRate()));
request.appendLabel("Frame Offset (samples): ",
toString(str, frameOffset()));
request.appendLabel("File Size (Mb): ",
toString(str, sizeInBytes()/1000000.0));
request.appendLabel("Source Sample Rate: ", toString(str, sRate()));
controller->handleRequest(request);
}
const char *
PvocData::channelName(int chan) const {
static char string[64];
int band = chan / 2;
double freq = double(band)/nBands() * sRate()/2.0;
double pfreq = (freq < 1000.0) ? freq : freq/1000.0;
char freqstring[28];
sprintf(freqstring, freq < 1000.0 ? "%0.1f hz" : "%0.2fk hz", pfreq);
sprintf(string, (chan % 2 == 0) ? "Amps@ %s" : "Frqs@ %s", freqstring);
return string;
}
// This global scope function exists because the two Pvoc header classes are
// currently unrelated, so this cannot be a static member of the common class.
Header *
createPvocHeader(PvocData* pv, DataFile* file) {
Header* header = nil;
int magic = file ? Header::readMagicNumber(file) : 0;
switch(magic) {
case CS_PvocHeader::PV_MAGIC:
header = new CS_PvocHeader(
pv->channels(), pv->frameRate(), pv->sRate()
);
break;
case _SND:
default:
header = new PvocHeader(pv->channels(), int(pv->frameRate()));
break;
}
return header;
}
// protected
Header *
PvocData::createHeader(DataFile *file, boolean reading) {
Header* h = ::createPvocHeader(this, file);
if(!reading) configureHeader(h);
return h;
}
void
PvocData::readFromHeader(Header *h) {
Data::readFromHeader(h);
PvocHeader *hd = (PvocHeader *) h;
framerate = hd->sampleRate(); // stored SR is framerate
// determine srate of anal file from highest freq band
double maxFreqBand = Data::limits(channels() - 1).max();
sr = (maxFreqBand > 20000.0) ? 44100
: (maxFreqBand > 10000.0) ? 22050 : 8000;
}
int
PvocData::getBandNumber(double freq) const {
double nyquist = sRate() / 2.0;
return round((min(freq, nyquist) * (nBands() - 1)) / nyquist);
}
|