/* 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 filename led_number n number_of_samples print_out eg talk12 3 60000 1 (1 gives printout of sample values in the two arrays, 0 for none) set number_of_samples equal to or a bit less than the number of samples errors do not stop it working - genuine Sunspot quality tested and approved Lesson 1 software ;-) THIS NEEDS A CLEANOUT - just for testing !!! */ #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 gaps; //number of positive pulses PLUS number of baseline periods 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 ongap; int offgap; int print_out; if (argc != 5) { printf("Error. usage: %s lednumber stretcher_count samples print_out\n", argv[0]); f_status = 0; } /* led_nr is the first number after the program name */ led_nr = atoi(argv[1]); /* set the pulse stretcher count*/ n = atoi(argv[2]); /* set the number of samples to read*/ samples = atoi(argv[3]); gaps = samples * 2; /* set 1 for printout, 0 for none*/ print_out = atoi(argv[4]); // hold the sound samples in memory int buffer_lo[samples]; int buffer_hi[samples]; //-------------------------------fill the buffer array from the sound samples file FILE *fp = fopen("test.wav", "rb"); if (fp == NULL) { printf("The file didn't open.\n"); return 0; } for (i=0; i < samples; i = i + 1) { int rc = fgetc(fp); if (rc == EOF) { printf("There was an error reading the file.\n"); break; } buffer_hi[i] = rc; //use for the plus pulse time buffer_lo[i] = 255 - rc; //use for the base line time if (print_out == 1) { printf("%d",buffer_hi[i]); printf("+"); printf("%d",buffer_lo[i]); printf("---"); } } fclose(fp); //------------------------------------------------------------- // fast loop follows - error detection 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 */ 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; }