|
| 1 | +#include "ctape.h" |
| 2 | + |
| 3 | +CTape::CTape() |
| 4 | +{ |
| 5 | + TapeLength=0; //signal length on tape |
| 6 | + TapeData=NULL; //tape data |
| 7 | + Speed=1; //playback speed |
| 8 | + HeadPosition=0; //current position of the "head" of the recorder |
| 9 | + UseLeftChannel=true; //permission to use the left channel |
| 10 | + UseRightChannel=true; //permission to use the right channel |
| 11 | + Channels=0; |
| 12 | +} |
| 13 | + |
| 14 | +CTape::~CTape() |
| 15 | +{ |
| 16 | + Eject(); |
| 17 | +} |
| 18 | + |
| 19 | +bool CTape::LoadTape(char *FileName) |
| 20 | +{ |
| 21 | + Eject(); |
| 22 | + |
| 23 | + HMMIO hwav; |
| 24 | + MMCKINFO parent,child; |
| 25 | + WAVEFORMATEX wfmtx; |
| 26 | + parent.ckid=(FOURCC)0; |
| 27 | + parent.cksize=0; |
| 28 | + parent.fccType=(FOURCC)0; |
| 29 | + parent.dwDataOffset=0; |
| 30 | + parent.dwFlags=0; |
| 31 | + child=parent; |
| 32 | + if ((hwav=mmioOpen(FileName,NULL,MMIO_READ|MMIO_ALLOCBUF))==NULL) return(false); |
| 33 | + parent.fccType=mmioFOURCC('W','A','V','E'); |
| 34 | + if (mmioDescend(hwav,&parent,NULL,MMIO_FINDRIFF)) |
| 35 | + { |
| 36 | + mmioClose(hwav,0); |
| 37 | + return(false); |
| 38 | + } |
| 39 | + child.ckid=mmioFOURCC('f','m','t',' '); |
| 40 | + if (mmioDescend(hwav,&child,&parent,0)) |
| 41 | + { |
| 42 | + mmioClose(hwav,0); |
| 43 | + return(false); |
| 44 | + } |
| 45 | + if (mmioRead(hwav,(char *)&wfmtx,sizeof(wfmtx))!=sizeof(wfmtx)) |
| 46 | + { |
| 47 | + mmioClose(hwav,0); |
| 48 | + return(false); |
| 49 | + } |
| 50 | + if (wfmtx.wFormatTag!=WAVE_FORMAT_PCM) |
| 51 | + { |
| 52 | + mmioClose(hwav,0); |
| 53 | + return(false); |
| 54 | + } |
| 55 | + if (mmioAscend(hwav,&child,0)) |
| 56 | + { |
| 57 | + mmioClose(hwav,0); |
| 58 | + return(false); |
| 59 | + } |
| 60 | + child.ckid=mmioFOURCC('d','a','t','a'); |
| 61 | + if (mmioDescend(hwav,&child,&parent,MMIO_FINDCHUNK)) |
| 62 | + { |
| 63 | + mmioClose(hwav,0); |
| 64 | + return(false); |
| 65 | + } |
| 66 | + |
| 67 | + //check the file format |
| 68 | + Channels=wfmtx.nChannels; //number of channels |
| 69 | + if (wfmtx.nSamplesPerSec!=44100) return(false); //check sample rate 44100 |
| 70 | + if (wfmtx.nBlockAlign!=wfmtx.nChannels) return(false); //only 8 bit |
| 71 | + |
| 72 | + //load sound data |
| 73 | + TapeData=new unsigned char[child.cksize]; |
| 74 | + TapeLength=child.cksize; |
| 75 | + mmioRead(hwav,(char *)TapeData,child.cksize); |
| 76 | + TapeLength/=Channels; //make the length of one channel |
| 77 | + mmioClose(hwav,0); |
| 78 | + return(true); |
| 79 | +} |
| 80 | + |
| 81 | +bool CTape::SetSpeed(long double speed) |
| 82 | +{ |
| 83 | + if (Speed<=0) return(false); |
| 84 | + Speed=speed; |
| 85 | + return(true); |
| 86 | +} |
| 87 | + |
| 88 | +bool CTape::ReadTape(unsigned char &volume) |
| 89 | +{ |
| 90 | + if (TapeData==NULL) return(false); |
| 91 | + unsigned long pos=(unsigned long)HeadPosition; |
| 92 | + if (pos>=TapeLength) return(false); |
| 93 | + unsigned long left=pos; |
| 94 | + unsigned long right=pos+1; |
| 95 | + if (right>=TapeLength) right=pos; |
| 96 | + |
| 97 | + //do a linear interpolation of the signal between neighboring points |
| 98 | + if (Channels==1) //for mono |
| 99 | + { |
| 100 | + long double k=((long double)(TapeData[right])-(long double)(TapeData[left])); |
| 101 | + double v=(double)(TapeData[left])+(HeadPosition-(long double)(pos))*k; |
| 102 | + volume=(unsigned char)v; |
| 103 | + } |
| 104 | + if (Channels==2) //for stereo |
| 105 | + { |
| 106 | + double ch_level=0; |
| 107 | + int ch_summ=0; |
| 108 | + if (UseLeftChannel==true) |
| 109 | + { |
| 110 | + long double k=((long double)(TapeData[right*2])-(long double)(TapeData[left*2])); |
| 111 | + ch_level+=(double)(TapeData[left*2])+(HeadPosition-(long double)(pos))*k; |
| 112 | + ch_summ++; |
| 113 | + } |
| 114 | + if (UseRightChannel==true) |
| 115 | + { |
| 116 | + long double k=((long double)(TapeData[right*2+1])-(long double)(TapeData[left*2+1])); |
| 117 | + ch_level+=(double)(TapeData[left*2+1])+(HeadPosition-(long double)(pos))*k; |
| 118 | + ch_summ++; |
| 119 | + } |
| 120 | + if (ch_summ==0) ch_summ=1; |
| 121 | + ch_level/=ch_summ; |
| 122 | + volume=(unsigned char)ch_level; |
| 123 | + } |
| 124 | + return(true); |
| 125 | +} |
| 126 | + |
| 127 | +bool CTape::MoveHead(void) |
| 128 | +{ |
| 129 | + HeadPosition+=Speed; |
| 130 | + unsigned long pos=(unsigned long)HeadPosition; |
| 131 | + if (pos>=TapeLength) return(false); |
| 132 | + return(true); |
| 133 | +} |
| 134 | + |
| 135 | +bool CTape::ResetHead(void) |
| 136 | +{ |
| 137 | + HeadPosition=0; |
| 138 | + return(true); |
| 139 | +} |
| 140 | + |
| 141 | +unsigned long CTape::GetTapeLength(void) |
| 142 | +{ |
| 143 | + return(TapeLength); |
| 144 | +} |
| 145 | + |
| 146 | +void CTape::Eject(void) |
| 147 | +{ |
| 148 | + if (TapeData!=NULL) delete(TapeData); |
| 149 | + HeadPosition=0; |
| 150 | + TapeData=NULL; |
| 151 | + TapeLength=0; |
| 152 | +} |
| 153 | + |
| 154 | +bool CTape::ApplyFilter(void) |
| 155 | +{ |
| 156 | + /* |
| 157 | + unsigned long n; |
| 158 | + int Filter=TapeData[0]; |
| 159 | + for(n=0;n<TapeLength;n++) |
| 160 | + { |
| 161 | + Filter=Filter+TapeData[n]; |
| 162 | + Filter/=2; |
| 163 | + TapeData[n]=Filter; |
| 164 | + } |
| 165 | + */ |
| 166 | + return(true); |
| 167 | +} |
| 168 | + |
| 169 | +long double CTape::GetSpeed(void) |
| 170 | +{ |
| 171 | + return(Speed); |
| 172 | +} |
| 173 | + |
| 174 | +bool CTape::EndOfTape(void) |
| 175 | +{ |
| 176 | + unsigned char volume; |
| 177 | + if (ReadTape(volume)==false) return(true); //tape finished |
| 178 | + return(false); //tape not finished |
| 179 | +} |
| 180 | + |
| 181 | +unsigned char CTape::GetLevel(int Step) |
| 182 | +{ |
| 183 | + unsigned long h_p=(unsigned long)HeadPosition; |
| 184 | + int n; |
| 185 | + unsigned long level=0; |
| 186 | + int max=0; |
| 187 | + int min=255; |
| 188 | + if (h_p+Step>=TapeLength) h_p=TapeLength-Step; |
| 189 | + if (TapeLength<Step) |
| 190 | + { |
| 191 | + h_p=0; |
| 192 | + Step=TapeLength; |
| 193 | + } |
| 194 | + for(n=0;n<Step;n++,h_p++) |
| 195 | + { |
| 196 | + unsigned char volume; |
| 197 | + if (Channels==1) volume=TapeData[h_p]; //for mono |
| 198 | + if (Channels==2) |
| 199 | + { |
| 200 | + int ch_level=0; |
| 201 | + int ch_summ=0; |
| 202 | + if (UseLeftChannel==true) |
| 203 | + { |
| 204 | + ch_level+=TapeData[h_p*2]; |
| 205 | + ch_summ++; |
| 206 | + } |
| 207 | + if (UseRightChannel==true) |
| 208 | + { |
| 209 | + ch_level+=TapeData[h_p*2+1]; |
| 210 | + ch_summ++; |
| 211 | + } |
| 212 | + if (ch_summ==0) ch_summ=1; |
| 213 | + ch_level/=ch_summ; |
| 214 | + volume=ch_level; |
| 215 | + } |
| 216 | + level+=volume; |
| 217 | + if (volume>max) max=volume; |
| 218 | + if (volume<min) min=volume; |
| 219 | + } |
| 220 | + if (n!=0) level/=n; |
| 221 | + int center=(max+min)/2; |
| 222 | + level=(level+center)/2; |
| 223 | + return((unsigned char)level); |
| 224 | +} |
| 225 | + |
| 226 | +bool CTape::SetUseChannel(bool use_left_channel,bool use_right_channel) |
| 227 | +{ |
| 228 | + UseLeftChannel=use_left_channel; |
| 229 | + UseRightChannel=use_right_channel; |
| 230 | + return(true); |
| 231 | +} |
| 232 | + |
0 commit comments