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