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
|
/////////////////////////////////////////////////////////
// SndIO.cpp: implementation of the SndIO class
//
//
//
//
#include "SndIO.h"
SndIO::SndIO(short channels, short bits, SndObj** inputlist,
int vecsize, float sr){
int n;
m_channels = channels;
m_bits = bits;
m_sampsize = bits/8;
m_vecsize = vecsize;
m_vecpos = 0;
m_sr = sr;
if(m_channels){
if(!(m_IOobjs = new SndObj*[m_channels])){
m_error = 2;
cout << ErrorMessage();
return;
}
if(inputlist) // if an array of input objects exists
for(n=0;n<m_channels;n++){
m_IOobjs[n]=inputlist[n]; // set output channels
}
else for(n=0;n<m_channels;n++) m_IOobjs[n] = 0; // zero the pointers
// vecsize is in frames! m_samples is vecsize in samples
m_samples = m_vecsize*m_channels;
if(!(m_output = new float[m_samples])){
m_error = 1;
cout << ErrorMessage();
return;
}
}
else {
m_IOobjs = 0;
m_output = 0;
m_samples = 0;
}
m_error = 0;
}
SndIO::~SndIO(){
delete[] m_IOobjs;
delete[] m_output;
}
short
SndIO::Write(){
if(m_IOobjs){
for(m_vecpos = 0; m_vecpos < m_vecsize; m_vecpos++)
for(int n = 0; n < m_channels; n++)
if(m_IOobjs[n])
cout << m_IOobjs[n]->Output(m_vecpos) << "\n";
return 1;
}
else{
m_error = 4;
return 0;
}
}
short
SndIO::Read(){
for(m_vecpos = 0; m_vecpos < m_samples; m_vecpos+=m_channels)
for(int n = 0; n < m_channels; n++)
cin >> m_output[n+m_vecpos];
return 1;
}
char* SndIO::ErrorMessage(){
char* message;
switch(m_error){
case 0:
message = "No error\n";
break;
case 1:
message = "Failed to allocate vector memory\n";
break;
case 2:
message = "Failed to allocate input object memory\n";
break;
case 3:
message = "Sampling rate mismatch\n";
break;
case 4:
message = "No input objects \n";
break;
default:
message = "Undefined error\n";
break;
}
return message;
}
|