//Voice decompressor example #include #include //I like these definitions #define begin { #define end } #define TableSize 6040 //refers to the following incl file //Contains the packed 2-bit codes for syntehsis //Generated by the program Make2code476.m #include "DPCMcode.h" //reconstruction differentials flash char PCMvalue[4] = {-78, -16, 16, 78}; int outI, tableI; //indexes char cycle ; //decode phase counter char out, lastout; //output values char p1, p2, p3, p4 ; //hold 4 differentials char packed ; //byte containing 4 2-bit values //generate waveform at 7812 scamples/sec interrupt [TIM0_OVF] void voicegen(void) begin //compute next sample cycle = outI%4; if (cycle==0) //do we need to unpack more data? begin if (tableI>6; p2 = (packed & 48)>>4; p3 = (packed & 12)>>2; p4 = (packed & 3); tableI++ ; end //end unpack table entry //compute the output and send to PWM out = lastout + PCMvalue[p1] - lastout>>4 ; OCR0 = out; lastout = out; outI++; end else if (cycle==1) //don't need to unpack yet--just ouput begin out = lastout + PCMvalue[p2] - lastout>>4 ; OCR0 = out; lastout = out; outI++; end else if (cycle==2) begin out = lastout + PCMvalue[p3] - lastout>>4 ; OCR0 = out; lastout = out; outI++; end else if (cycle==3) begin out = lastout + PCMvalue[p4] - lastout>>4 ; OCR0 = out; lastout = out; outI++; end //next sample //at end, turn off TCCRO if (tableI==TableSize) TCCR0 = 0; end void main(void) begin DDRB=0xff; #asm sei #endasm while(1) begin //turn on pwm with period= 256*8 cycles = 2048 cycles // or 7812 samples/sec //fast PWM mode, TCNT0 = 0; OCR0 = 128; TCCR0 = 0b01101010 ; TIMSK = 0b00000001 ; //init the output indexes outI = 0; tableI = 0; //init the ouptut value lastout = 128; //wait until the speech is done then //time delay the next utterance. while (TCCR0>0){} delay_ms(1000); end // end while end //end main