Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gs.cpp

Go to the documentation of this file.
00001 
00002 //#include <FL/Fl.H>
00003 //#include <FL/Fl_Widget.H>
00004 //#include <FL/fl_draw.H>
00005 #include <stdio.h>
00006 //#include <qpixmap.h>
00007 #include "gs.h"
00008 
00009 #include <sys/ioctl.h>
00010 #include <unistd.h>
00011 #include <fcntl.h>
00012 #include <sys/soundcard.h>
00013 
00014 #include <errno.h>
00015 #include <string.h>
00016 
00017 #include <stdlib.h>
00018 
00019 //#include <qpainter.h>
00020 
00021 
00022 gs::gs()
00023 {
00024   
00025   finger[0] = OPEN;
00026   finger[1] = OPEN;
00027   finger[2] = OPEN;
00028   finger[3] = OPEN;
00029   finger[4] = OPEN;
00030   finger[5] = OPEN;
00031 
00032   tuning[0] = 0;
00033   tuning[1] = 0;
00034   tuning[2] = 0;
00035   tuning[3] = 0;
00036   tuning[4] = 0;
00037   tuning[5] = 0;
00038 
00039   initial_fret = 0;
00040 
00041   audio_fd = -1;
00042   
00043   pb_rate0 = 0;
00044   pb_rate1 = 0;
00045   pb_rate2 = 0;
00046   pb_rate3 = 0;
00047   pb_rate4 = 0;
00048   pb_rate5 = 0;
00049   pb_rate6 = 0;
00050 
00051   pb_oct0 = 0;
00052   pb_oct1 = 0;
00053   pb_oct2 = 0;
00054   pb_oct3 = 0;
00055   pb_oct4 = 0;
00056   pb_oct5 = 0;
00057   pb_oct6 = 0;
00058 
00059   // initialise reverb buffer
00060   reverb = (signed short *)malloc(1024 * sizeof(signed short));
00061 
00062   for (reverb_ptr=0;reverb_ptr<1024;reverb_ptr++){
00063     reverb[reverb_ptr] = 0;
00064   }
00065   reverb_ptr = 0;
00066   reverb_max = 1024;
00067 
00068   // load sampled 'E' string
00069   int samplen = 25000;
00070 
00071   signed short *dsp_buf = (signed short *)malloc(samplen * sizeof(signed short));
00072   signed short *dsp_buf_ptr = dsp_buf;
00073 
00074   int raw_fd;
00075 
00076   QString path = getenv( "OPIEDIR" );
00077   path.append( "/share/powerchord/acguitar.raw" );
00078 
00079   raw_fd = open( (const char*) path, O_RDONLY);
00080 
00081   if (raw_fd < 0){
00082       fprintf(stderr, "Failed to open raw file (%s)\n", strerror(errno));
00083       exit(-1);
00084   }
00085 
00086   int totread = 0;
00087   int i;
00088 
00089   while (totread < samplen*2){
00090       int want = samplen*2 - totread;
00091 
00092       int numread = read(raw_fd, dsp_buf_ptr, want);
00093       fprintf(stderr, "read %d bytes\n", numread);
00094       totread += numread;
00095       dsp_buf_ptr += numread/2;
00096 
00097       if (numread == 0){
00098           fprintf(stderr, "failed to read bytes\n");
00099           exit(-1);
00100       }    
00101   }
00102 
00103   close(raw_fd);
00104 
00105   // scale down a bit for mixing
00106   for (i=0;i<samplen;i++){
00107     dsp_buf[i] /= 6;
00108   }
00109   
00110   set_tonebank(0, dsp_buf, samplen);
00111   set_tonebank(1, dsp_buf, samplen);
00112   set_tonebank(2, dsp_buf, samplen);
00113   set_tonebank(3, dsp_buf, samplen);
00114   set_tonebank(4, dsp_buf, samplen);
00115   set_tonebank(5, dsp_buf, samplen);
00116   set_tonebank(6, dsp_buf, samplen);
00117   
00118 }
00119 
00120 void gs::set_tonebank(int tb, signed short *buf, int length)
00121 {
00122     switch(tb){
00123     case 0:
00124         tonebank0 = buf;
00125         tonebank_length0 = length;
00126         break;
00127     case 1:
00128         tonebank1 = buf;
00129         tonebank_length1 = length;
00130         break;
00131     case 2:
00132         tonebank2 = buf;
00133         tonebank_length2 = length;
00134         break;
00135     case 3:
00136         tonebank3 = buf;
00137         tonebank_length3 = length;
00138         break;
00139     case 4:
00140         tonebank4 = buf;
00141         tonebank_length4 = length;
00142         break;
00143     case 5:
00144         tonebank5 = buf;
00145         tonebank_length5 = length;
00146         break;
00147     case 6:
00148         tonebank6 = buf;
00149         tonebank_length6 = length;
00150         break;
00151         
00152     }
00153 }
00154 
00155 
00156 void gs::Finger(int f, int position){
00157   if (f < 0 || f > 5){
00158     fprintf(stderr, "Error - finger2 value was %d\n", f);
00159     return;
00160   }
00161 
00162   finger[f] = position;
00163 }
00164 
00165 void gs::Tuning(int t[6]){
00166   for (int i=0;i<6;i++){
00167     tuning[i] = t[i];
00168   }
00169 }
00170 
00171 // length in ps (seconds x 10^-9) of the period of a note.
00172 // we use these as ratios in a breshenham-like algorithm to
00173 // scale a deep note to a higher pitch
00174 // They are derived from f(A) = 440Hz and multiply each successive
00175 // semitone by the 12th root of 2 (such that after 12 multiplications for
00176 // 12 semitones you have a note exactly 2x the frequency of the initial one,
00177 // - an octave higher in other words.)
00178 
00179 int gs::note_periods[12] = {
00180 90703,
00181 85612,
00182 80802,
00183 76272,
00184 71991,
00185 67950,
00186 64137,
00187 60537,
00188 57139,
00189 53932,
00190 50905,
00191 48048
00192 };
00193 
00194 int gs::octave_step[6] = {
00195   1,
00196   2,
00197   4,
00198   8,
00199   16,
00200   32
00201 };
00202 
00203 int gs::Play(){
00204     int format;
00205     int channels;
00206     int speed;
00207 
00208     frames = 0;
00209 
00210     if (audio_fd == -1){
00211         if ( (audio_fd = open("/dev/dsp", O_WRONLY, 0) ) == -1){    
00212           audio_fd = -1;
00213           return 1;
00214         }
00215         
00216         format = AFMT_S16_NE;
00217         
00218         if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format) == -1){
00219             fprintf(stderr, "Error SNDCTL DSP SETFMT, %s\n", strerror(errno));
00220             exit(0);
00221         }
00222 
00223         channels = 1;
00224 
00225         if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1){
00226             fprintf(stderr, "Error SNDCTL DSP CHANNELS, %s\n", strerror(errno));
00227             exit(0);
00228         }
00229         
00230         speed = 11025;
00231         
00232         if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &speed) == -1){
00233             fprintf(stderr, "Error SNDCTL DSP SPEED, %s\n", strerror(errno));
00234             exit(0);
00235         }
00236 
00237         // buffering q's
00238         //      audio_buf_info info;
00239         //      if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) == -1){
00240         //          fprintf(stderr, "Error SNDCTL DSP GETOSPACE, %s\n", strerror(errno));
00241         //          exit(0);
00242         //      }
00243         //      fprintf(stderr, "fragments %d\nfragstotal %d\nfragsize %d\nbytes %d\n", info.fragments, info.fragstotal, info.fragsize, info.bytes);
00244         
00245         
00246         
00247 // audio math.
00248 // A4 = 440Hz
00249 // +1 octave = 2x freq
00250 // -1 octave = /2 freq.
00251 // +1 semitone = *= 12 root 2;
00252 // ie. * 1.059463094
00253 
00254 // tones, approx Hz, samples at 11025
00255 // A4    440  25
00256 // A#4   466  24
00257 // B4    494  22
00258 // C4    523  21
00259 // C#4   554  20
00260 // D4    587  19
00261 // D#4   622  18
00262 // E4    659  17
00263 // F4    698  16
00264 // F#4   740  15
00265 // G4    784  14
00266 // G#4   831  13
00267 
00268         
00269     }
00270     else{
00271         fprintf(stderr, "Already playing\n");
00272     }
00273     
00274     return 0;
00275 }
00276 
00277 void gs::note_start(int chan, int note, int octave)
00278 {
00279     switch (chan){
00280         
00281     case 0:
00282         pb_rate0 = note_periods[note];
00283         pb_oct0 = octave_step[octave];
00284         pb_ratio0 = 0;
00285         pb_rsc0 = 0;
00286         break;
00287     case 1:
00288         pb_rate1 = note_periods[note];
00289         pb_oct1 = octave_step[octave];
00290         pb_ratio1 = 0;
00291         pb_rsc1 = 0;
00292         break;
00293     case 2:
00294         pb_rate2 = note_periods[note];
00295         pb_oct2 = octave_step[octave];
00296         pb_ratio2 = 0;
00297         pb_rsc2 = 0;
00298         break;
00299     case 3:
00300         pb_rate3 = note_periods[note];
00301         pb_oct3 = octave_step[octave];
00302         pb_ratio3 = 0;
00303         pb_rsc3 = 0;
00304         break;
00305     case 4:
00306         pb_rate4 = note_periods[note];
00307         pb_oct4 = octave_step[octave];
00308         pb_ratio4 = 0;
00309         pb_rsc4 = 0;
00310         break;
00311     case 5:
00312         pb_rate5 = note_periods[note];
00313         pb_oct5 = octave_step[octave];
00314         pb_ratio5 = 0;
00315         pb_rsc5 = 0;
00316         break;
00317     case 6:
00318         pb_rate6 = note_periods[note];
00319         pb_oct6 = octave_step[octave];
00320         pb_ratio6 = 0;
00321         pb_rsc6 = 0;
00322         break;
00323     default:
00324         fprintf(stderr, "Bad channel\n");
00325         exit(-1);
00326     }
00327     
00328   
00329 }
00330 
00331 void gs::write_buffer(){
00332   int num_read;
00333   num_read = write(audio_fd, (void *)audio_buf, BUFSIZE*2);
00334   //  fprintf(stderr, "Wrote %d bytes\n", num_read);
00335 }
00336 
00337 void gs::fill_buffer()
00338 {
00339   frames ++;
00340 
00341   int i;
00342 
00343   for (i=0;i<BUFSIZE;i++){
00344       
00345       audio_buf[i] = 0;
00346       
00347       if (pb_rate0){
00348           audio_buf[i] += tonebank0[pb_rsc0];
00349           pb_rsc0 += pb_oct0;
00350           pb_ratio0 += 90703;
00351           pb_ratio0 -= pb_rate0;
00352           if (pb_ratio0 >= pb_rate0){
00353               pb_rsc0 += pb_oct0;
00354               pb_ratio0 -= pb_rate0;
00355               
00356           }
00357           if (pb_rsc0 >= tonebank_length0) pb_rate0 = 0;
00358       }
00359 
00360       if (pb_rate1){
00361           audio_buf[i] += tonebank1[pb_rsc1];
00362           pb_rsc1 += pb_oct1;
00363           pb_ratio1 += 90703;
00364           pb_ratio1 -= pb_rate1;
00365           if (pb_ratio1 >= pb_rate1){
00366               pb_rsc1 += pb_oct1;
00367               pb_ratio1 -= pb_rate1;
00368               
00369           }
00370           if (pb_rsc1 >= tonebank_length1) pb_rate1 = 0;
00371       }
00372 
00373       if (pb_rate2){
00374           audio_buf[i] += tonebank2[pb_rsc2];
00375           pb_rsc2 += pb_oct2;
00376           pb_ratio2 += 90703;
00377           pb_ratio2 -= pb_rate2;
00378           if (pb_ratio2 >= pb_rate2){
00379               pb_rsc2 += pb_oct2;
00380               pb_ratio2 -= pb_rate2;
00381               
00382           }
00383           if (pb_rsc2 >= tonebank_length2) pb_rate2 = 0;
00384       }
00385 
00386       if (pb_rate3){
00387           audio_buf[i] += tonebank3[pb_rsc3];
00388           pb_rsc3 += pb_oct3;
00389           pb_ratio3 += 90703;
00390           pb_ratio3 -= pb_rate3;
00391           if (pb_ratio3 >= pb_rate3){
00392               pb_rsc3 += pb_oct3;
00393               pb_ratio3 -= pb_rate3;
00394               
00395           }
00396           if (pb_rsc3 >= tonebank_length3) pb_rate3 = 0;
00397       }
00398 
00399       if (pb_rate4){
00400           audio_buf[i] += tonebank4[pb_rsc4];
00401           pb_rsc4 += pb_oct4;
00402           pb_ratio4 += 90703;
00403           pb_ratio4 -= pb_rate4;
00404           if (pb_ratio4 >= pb_rate4){
00405               pb_rsc4 += pb_oct4;
00406               pb_ratio4 -= pb_rate4;
00407               
00408           }
00409           if (pb_rsc4 >= tonebank_length4) pb_rate4 = 0;
00410       }
00411 
00412       if (pb_rate5){
00413           audio_buf[i] += tonebank5[pb_rsc5];
00414           pb_rsc5 += pb_oct5;
00415           pb_ratio5 += 90703;
00416           pb_ratio5 -= pb_rate5;
00417           if (pb_ratio5 >= pb_rate5){
00418               pb_rsc5 += pb_oct5;
00419               pb_ratio5 -= pb_rate5;
00420               
00421           }
00422           if (pb_rsc5 >= tonebank_length5) pb_rate5 = 0;
00423       }
00424 
00425       if (pb_rate6){
00426           audio_buf[i] += tonebank6[pb_rsc6];
00427           pb_rsc6 += pb_oct6;
00428           pb_ratio6 += 90703;
00429           pb_ratio6 -= pb_rate6;
00430           if (pb_ratio6 >= pb_rate6){
00431               pb_rsc6 += pb_oct6;
00432               pb_ratio6 -= pb_rate6;
00433               
00434           }
00435           if (pb_rsc6 >= tonebank_length6) pb_rate6 = 0;
00436       }
00437 
00438       // do reverb
00439       signed short rtmp = reverb[reverb_ptr];
00440       reverb[reverb_ptr] /= 2;
00441       reverb[reverb_ptr] += audio_buf[i]/4;
00442       audio_buf[i] += rtmp;
00443       reverb_ptr++;
00444       if (reverb_ptr >= reverb_max) reverb_ptr = 0;
00445   }
00446 }
00447 
00448 
00449 void gs::Stop(){
00450     if (audio_fd == -1){
00451         fprintf(stderr, "Already stopped\n");
00452     }
00453     else{
00454         //ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
00455         
00456         close(audio_fd);
00457         audio_fd = -1;
00458     }
00459     
00460 }
00461 
00462 gs::~gs()
00463 {}

Generated on Sat Nov 5 16:17:35 2005 for OPIE by  doxygen 1.4.2