/* audio file reader use Audacity to create WAV(Microsoft), Unsigned 8bit PCM files set n (pulse stretcher loop) to match the sample rate - fine tune in Audacity set for LED (0) - gpio(0), the power led - this one is earthed - connect an amplifier to the positive end of the diode. For each 8bit sample (0-255) this creates a plus pulse of length S and an earth period of (255 - S) - stretched by the n value USAGE talk16 sound_filename n print_out eg talk16 soundclip.wav 0 2 print_out 0 - no printout print_out 1 - prints file size and all bytes print_out 2 - prints file size The program opens the named wav file and counts the bytes -it then loads this number of bytes into buffer_hi and the values (255-byte) into buffer_lo - genuine Sunspot quality tested and approved Lesson 1 software ;-) */ #include #include #include #include #include #include int main(int argc, char** argv) { int led_fd = 0; int f_status = 1; int i; int j; int led_nr = 0; int ontime; int offtime; int samples; /*number of bytes to be read from the file*/ int count; /* this counts up to value of samples*/ int k; /* for the delay loops - usleep has a minimum delay! */ int n ;/*use for delay loop to stretch the pulse lengths*/ int print_out; if (argc != 4) { printf("Error. usage: %s file_to_read.wav stretcher_count print_out\n", argv[0]); f_status = 0; } /* name of file to open goes int argv[1]- eg soundclip.wav */ /* set the pulse stretcher count*/ n = atoi(argv[2]); /* set 1 for printout, 0 for none*/ print_out = atoi(argv[3]); // hold the sound samples in memory //-------------------------------fill the buffer array from the sound samples file FILE *fp = fopen( argv[1], "rb");//open to read as binary if (fp == NULL) { printf("The file didn't open.\n"); return 0; } // get input file length fseek(fp,0,SEEK_END); samples = ftell(fp); if (print_out == 2) { printf("total number of samples = "); printf("%d",samples); printf("\n\n"); } // now we know the size of the file so set up the 2 arrays samples = samples - 4;//hey! 4 works, zero does not! //set up the arrays to hold the sound amplitude values for fast output int buffer_lo[samples]; int buffer_hi[samples]; // set for first read from the wav file fseek(fp,0,SEEK_SET); for (i=0; i < samples; i++) { int c = fgetc(fp); if (c == EOF) { printf("There was an error reading the file.\n"); break; } buffer_hi[i] = c; //use for the plus pulse time buffer_lo[i] = 255 - c; //use for the base line time if (print_out == 1) { printf("%d",i); printf("-->"); printf("%d",buffer_hi[i]); printf("+"); printf("%d",buffer_lo[i]); printf("\n"); } } fclose(fp); samples = samples - 4; //------------------------------------------------------------- // fast loop follows - error detection and led number variable removed for speed // two buffer arrays used for symmetry of delays // perhaps remove delay stretcher and increase sample rate? led_fd = open("/dev/gpio0",O_RDWR); for (count = 1;count <= samples;count = count + 1) { /* positive pulse time*/ ioctl(led_fd, 0, 0); for (k = 1;k <= buffer_hi[count];k = k + 1) { for (j = 0;j <= n;j = j + 1) { /*delay stretcher*/ } } /* zero volts time */ ioctl(led_fd, 1, 0); for (k = 1;k <= buffer_lo[count];k = k + 1) { for (j = 0;j <= n;j = j + 1) { /*delay stretcher*/ } } } close (led_fd); //is that close OK?---------------------------------------------------------- return 0; }