![](/media/cache/profiles/98c39996bf1543e974747a2549b3107c.50x50_q85.png)
For BU9480F D-A Conv.
main.cpp@0:805cffac956b, 2010-08-26 (annotated)
- Committer:
- lynxeyed_atsu
- Date:
- Thu Aug 26 12:48:50 2010 +0000
- Revision:
- 0:805cffac956b
- Child:
- 1:02bfb013660c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lynxeyed_atsu | 0:805cffac956b | 1 | /* DPCM & Rice Golomb Lossless Audio Player and Realtime encoder */ |
lynxeyed_atsu | 0:805cffac956b | 2 | #include "mbed.h" |
lynxeyed_atsu | 0:805cffac956b | 3 | #include "string" |
lynxeyed_atsu | 0:805cffac956b | 4 | #include "SDHCFileSystem.h" |
lynxeyed_atsu | 0:805cffac956b | 5 | #include "_bitio.h" |
lynxeyed_atsu | 0:805cffac956b | 6 | //FILE *infp,*outfp; |
lynxeyed_atsu | 0:805cffac956b | 7 | SDFileSystem sd(p11, p12, p13, p14, "sd"); |
lynxeyed_atsu | 0:805cffac956b | 8 | DigitalOut led1(LED1); |
lynxeyed_atsu | 0:805cffac956b | 9 | DigitalOut led2(LED2); |
lynxeyed_atsu | 0:805cffac956b | 10 | AnalogOut DACout(p18); |
lynxeyed_atsu | 0:805cffac956b | 11 | Ticker tick; |
lynxeyed_atsu | 0:805cffac956b | 12 | |
lynxeyed_atsu | 0:805cffac956b | 13 | // a FIFO for the DAC |
lynxeyed_atsu | 0:805cffac956b | 14 | volatile short DAC_fifo[1024]; |
lynxeyed_atsu | 0:805cffac956b | 15 | volatile short DAC_wptr=0; |
lynxeyed_atsu | 0:805cffac956b | 16 | volatile short DAC_rptr=0; |
lynxeyed_atsu | 0:805cffac956b | 17 | volatile short DAC_on; |
lynxeyed_atsu | 0:805cffac956b | 18 | volatile short int DAC_diff=0; |
lynxeyed_atsu | 0:805cffac956b | 19 | |
lynxeyed_atsu | 0:805cffac956b | 20 | |
lynxeyed_atsu | 0:805cffac956b | 21 | void encode(long int n); |
lynxeyed_atsu | 0:805cffac956b | 22 | void decode(void); |
lynxeyed_atsu | 0:805cffac956b | 23 | void dac_out(); |
lynxeyed_atsu | 0:805cffac956b | 24 | |
lynxeyed_atsu | 0:805cffac956b | 25 | |
lynxeyed_atsu | 0:805cffac956b | 26 | |
lynxeyed_atsu | 0:805cffac956b | 27 | |
lynxeyed_atsu | 0:805cffac956b | 28 | int main() { |
lynxeyed_atsu | 0:805cffac956b | 29 | |
lynxeyed_atsu | 0:805cffac956b | 30 | |
lynxeyed_atsu | 0:805cffac956b | 31 | if ( NULL == (infp = fopen( "/sd/test.rgm", "rb" )) ) { |
lynxeyed_atsu | 0:805cffac956b | 32 | printf( "\r\nError: The message file cannot be accessed\r\n" ); |
lynxeyed_atsu | 0:805cffac956b | 33 | return -1; |
lynxeyed_atsu | 0:805cffac956b | 34 | } |
lynxeyed_atsu | 0:805cffac956b | 35 | //tick.detach(); |
lynxeyed_atsu | 0:805cffac956b | 36 | tick.attach_us(&dac_out, 45); //set 22.050kHz sampling data |
lynxeyed_atsu | 0:805cffac956b | 37 | decode(); |
lynxeyed_atsu | 0:805cffac956b | 38 | tick.detach(); |
lynxeyed_atsu | 0:805cffac956b | 39 | |
lynxeyed_atsu | 0:805cffac956b | 40 | fclose( infp ); |
lynxeyed_atsu | 0:805cffac956b | 41 | //fclose( outfp ); |
lynxeyed_atsu | 0:805cffac956b | 42 | |
lynxeyed_atsu | 0:805cffac956b | 43 | |
lynxeyed_atsu | 0:805cffac956b | 44 | |
lynxeyed_atsu | 0:805cffac956b | 45 | while (1); |
lynxeyed_atsu | 0:805cffac956b | 46 | } |
lynxeyed_atsu | 0:805cffac956b | 47 | |
lynxeyed_atsu | 0:805cffac956b | 48 | void dac_out() |
lynxeyed_atsu | 0:805cffac956b | 49 | { |
lynxeyed_atsu | 0:805cffac956b | 50 | // printf("\t%d\r\n",DAC_diff); |
lynxeyed_atsu | 0:805cffac956b | 51 | if (DAC_diff > 0) { |
lynxeyed_atsu | 0:805cffac956b | 52 | led2 = 0; |
lynxeyed_atsu | 0:805cffac956b | 53 | DACout.write_u16(DAC_fifo[DAC_rptr]); |
lynxeyed_atsu | 0:805cffac956b | 54 | DAC_rptr=(DAC_rptr+1) & 0x3ff; |
lynxeyed_atsu | 0:805cffac956b | 55 | DAC_diff--; |
lynxeyed_atsu | 0:805cffac956b | 56 | }else led2 = 1; |
lynxeyed_atsu | 0:805cffac956b | 57 | } |
lynxeyed_atsu | 0:805cffac956b | 58 | void encode(long int n){ |
lynxeyed_atsu | 0:805cffac956b | 59 | int zero_shift = 0; |
lynxeyed_atsu | 0:805cffac956b | 60 | |
lynxeyed_atsu | 0:805cffac956b | 61 | if(n < 0){ |
lynxeyed_atsu | 0:805cffac956b | 62 | putbit(0); // sign (put 0:if n as negative) |
lynxeyed_atsu | 0:805cffac956b | 63 | n = -n; // n = abs(n) |
lynxeyed_atsu | 0:805cffac956b | 64 | //printf("\t 0"); |
lynxeyed_atsu | 0:805cffac956b | 65 | } |
lynxeyed_atsu | 0:805cffac956b | 66 | else{ |
lynxeyed_atsu | 0:805cffac956b | 67 | putbit(1); // sign (put 1:if n as positive) |
lynxeyed_atsu | 0:805cffac956b | 68 | //printf("\t 1"); |
lynxeyed_atsu | 0:805cffac956b | 69 | } |
lynxeyed_atsu | 0:805cffac956b | 70 | zero_shift = (n >> (k)); |
lynxeyed_atsu | 0:805cffac956b | 71 | //printf("\t shift= %d",zero_shift); |
lynxeyed_atsu | 0:805cffac956b | 72 | while(zero_shift > 0){ |
lynxeyed_atsu | 0:805cffac956b | 73 | |
lynxeyed_atsu | 0:805cffac956b | 74 | zero_shift--; |
lynxeyed_atsu | 0:805cffac956b | 75 | putbit(0); |
lynxeyed_atsu | 0:805cffac956b | 76 | } // put n/(2^k) 0's |
lynxeyed_atsu | 0:805cffac956b | 77 | |
lynxeyed_atsu | 0:805cffac956b | 78 | putbit(1); // terminating "1" |
lynxeyed_atsu | 0:805cffac956b | 79 | putbits(k,rightbits(k,n)); |
lynxeyed_atsu | 0:805cffac956b | 80 | //printf("\t finish= %d \r\n",(n & ((1U<<k)-1))); |
lynxeyed_atsu | 0:805cffac956b | 81 | } |
lynxeyed_atsu | 0:805cffac956b | 82 | |
lynxeyed_atsu | 0:805cffac956b | 83 | |
lynxeyed_atsu | 0:805cffac956b | 84 | |
lynxeyed_atsu | 0:805cffac956b | 85 | void decode(void){ |
lynxeyed_atsu | 0:805cffac956b | 86 | short dac_data; |
lynxeyed_atsu | 0:805cffac956b | 87 | long int decode_buff; |
lynxeyed_atsu | 0:805cffac956b | 88 | short diff,diff2; |
lynxeyed_atsu | 0:805cffac956b | 89 | unsigned int buff_sign,zero_shift; |
lynxeyed_atsu | 0:805cffac956b | 90 | //char flag; |
lynxeyed_atsu | 0:805cffac956b | 91 | |
lynxeyed_atsu | 0:805cffac956b | 92 | diff = 0; |
lynxeyed_atsu | 0:805cffac956b | 93 | diff2 = 0; |
lynxeyed_atsu | 0:805cffac956b | 94 | // get sign(1 for positive, 0 for negative) |
lynxeyed_atsu | 0:805cffac956b | 95 | while(1){ |
lynxeyed_atsu | 0:805cffac956b | 96 | |
lynxeyed_atsu | 0:805cffac956b | 97 | if((buff_sign = getbit())==OVERRUN)break; |
lynxeyed_atsu | 0:805cffac956b | 98 | zero_shift = 0; |
lynxeyed_atsu | 0:805cffac956b | 99 | while(getbit()==0)zero_shift++; |
lynxeyed_atsu | 0:805cffac956b | 100 | |
lynxeyed_atsu | 0:805cffac956b | 101 | decode_buff = (signed int)((1U << k)*zero_shift); |
lynxeyed_atsu | 0:805cffac956b | 102 | decode_buff += (getbits(k)); |
lynxeyed_atsu | 0:805cffac956b | 103 | |
lynxeyed_atsu | 0:805cffac956b | 104 | if(!buff_sign)decode_buff =- decode_buff; |
lynxeyed_atsu | 0:805cffac956b | 105 | /* return decode_buff; */ |
lynxeyed_atsu | 0:805cffac956b | 106 | diff =(diff + decode_buff); |
lynxeyed_atsu | 0:805cffac956b | 107 | |
lynxeyed_atsu | 0:805cffac956b | 108 | dac_data = ((int)diff + 32768) >> 1; |
lynxeyed_atsu | 0:805cffac956b | 109 | DAC_fifo[DAC_wptr]=dac_data; |
lynxeyed_atsu | 0:805cffac956b | 110 | DAC_wptr=(DAC_wptr+1) & 0x3ff; |
lynxeyed_atsu | 0:805cffac956b | 111 | DAC_diff=(DAC_diff+1); |
lynxeyed_atsu | 0:805cffac956b | 112 | while (DAC_diff >= 1023){ |
lynxeyed_atsu | 0:805cffac956b | 113 | led1 = 1; |
lynxeyed_atsu | 0:805cffac956b | 114 | } //wait |
lynxeyed_atsu | 0:805cffac956b | 115 | led1=0; |
lynxeyed_atsu | 0:805cffac956b | 116 | if(DAC_diff <= 0) |
lynxeyed_atsu | 0:805cffac956b | 117 | led2=1; |
lynxeyed_atsu | 0:805cffac956b | 118 | else led2=0; |
lynxeyed_atsu | 0:805cffac956b | 119 | |
lynxeyed_atsu | 0:805cffac956b | 120 | |
lynxeyed_atsu | 0:805cffac956b | 121 | if((buff_sign = getbit())==OVERRUN)break; |
lynxeyed_atsu | 0:805cffac956b | 122 | zero_shift = 0; |
lynxeyed_atsu | 0:805cffac956b | 123 | while(getbit()==0)zero_shift++; |
lynxeyed_atsu | 0:805cffac956b | 124 | |
lynxeyed_atsu | 0:805cffac956b | 125 | decode_buff = (signed int)((1U << k)*zero_shift); |
lynxeyed_atsu | 0:805cffac956b | 126 | decode_buff += (getbits(k)); |
lynxeyed_atsu | 0:805cffac956b | 127 | |
lynxeyed_atsu | 0:805cffac956b | 128 | if(!buff_sign)decode_buff =- decode_buff; |
lynxeyed_atsu | 0:805cffac956b | 129 | /* return decode_buff; */ |
lynxeyed_atsu | 0:805cffac956b | 130 | diff2 =(diff2 + decode_buff); |
lynxeyed_atsu | 0:805cffac956b | 131 | |
lynxeyed_atsu | 0:805cffac956b | 132 | /* |
lynxeyed_atsu | 0:805cffac956b | 133 | drawc((unsigned char)diff2 & 0xff); |
lynxeyed_atsu | 0:805cffac956b | 134 | drawc((unsigned char)((diff2>>8) &0xff)); |
lynxeyed_atsu | 0:805cffac956b | 135 | */ |
lynxeyed_atsu | 0:805cffac956b | 136 | //fwrite(&diff2, sizeof(short int), 1, outfp); |
lynxeyed_atsu | 0:805cffac956b | 137 | |
lynxeyed_atsu | 0:805cffac956b | 138 | |
lynxeyed_atsu | 0:805cffac956b | 139 | |
lynxeyed_atsu | 0:805cffac956b | 140 | } |
lynxeyed_atsu | 0:805cffac956b | 141 | } |