Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * fileIOTest.c
- *
- * Created on: Mar 10, 2012
- * Author: BLEE
- *
- * For the book "Real Time Digital Signal Processing:
- * Fundamentals, Implementation and Application, 3rd Ed"
- * By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
- * Publisher: John Wiley and Sons, Ltd
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "tistdtypes.h"
- Uint8 waveHeader[44]={ // 44 bytes for WAV file header
- 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00,
- 0x57, 0x41, 0x56, 0x45, 0x66, 0x6D, 0x74, 0x20,
- 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
- 0x40, 0x1F, 0x00, 0x00, 0x80, 0x3E, 0x00, 0x00,
- 0x02, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61,
- 0x00, 0x00, 0x00, 0x00};
- #define SIZE 1024
- Uint8 ch[SIZE]; // Declare a char[1024] array for experiment
- Uint16 number[SIZE/2];
- Uint8 char_array[6];
- void DK_MF_ITOA(Uint16 number, Uint8* pointer)
- {
- pointer[0] = number / 10000 + 0x30;
- pointer[1] = (number / 1000) % 10 + 0x30;
- pointer[2] = (number / 100) % 10 + 0x30;
- pointer[3] = (number / 10) % 10 + 0x30;
- pointer[4] = number % 10 + 0x30;
- pointer[5] = 0;
- return;
- }
- void main()
- {
- FILE *fp1,*fp2,*fp3; // File pointers
- Uint32 i; // Unsigned long integer used as a counter
- Uint32 j;
- printf("Exp. 1.2 --- file IO\n");
- fp1 = fopen("..\\data\\C55DSPUSBStickAudioTest.pcm", "rb"); // Open input file
- fp2 = fopen("..\\data\\C55DSPUSBStickAudioTest.wav", "wb"); // Open output file
- fp3 = fopen("..\\data\\C55DSPUSBStickAudioTest.csv", "wb"); // Open output file
- if (fp1 == NULL) // Check if the input file exists
- {
- printf("Failed to open input file 'C55DSPUSBStickAudioTest.pcm'\n");
- exit(0);
- }
- fseek(fp2, 44, 0); // Advance output file point 44 bytes
- i=0;
- while (fread(ch, sizeof(Uint8), SIZE, fp1) == SIZE) // Read in SIZE of input data bytes
- {
- fwrite(ch, sizeof(Uint8), SIZE, fp2); // Write SIZE of data bytes to output file
- i += SIZE;
- printf("%ld bytes processed\n", i); // Show the number of data is processed
- }
- fclose(fp1); // Close input file
- fp1 = fopen("..\\data\\C55DSPUSBStickAudioTest.pcm", "rb"); // Open input file
- i=0;
- while (fread(ch, sizeof(Uint8), SIZE, fp1) == SIZE) // Read in SIZE of input data bytes
- {
- for(j=0; j<=SIZE; j=j+2)
- {
- number[j/2] = (ch[j]<<8) + ch[j+1];
- }
- for(j=0; j<=SIZE/2; j=j+1)
- {
- DK_MF_ITOA(number[j], char_array);
- fwrite(char_array, 1, 6, fp3); // Write SIZE of data bytes to output file
- fp3 += 6;
- }
- i += SIZE;
- printf("%ld bytes saved\n", i); // Show the number of data is processed
- }
- waveHeader[40] = (Uint8)(i&0xff); // Update the size parameter into WAV header
- waveHeader[41] = (Uint8)(i>>8)&0xff;
- waveHeader[42] = (Uint8)(i>>16)&0xff;
- waveHeader[43] = (Uint8)(i>>24)&0xff;
- waveHeader[4] = waveHeader[40];
- waveHeader[5] = waveHeader[41];
- waveHeader[6] = waveHeader[42];
- waveHeader[7] = waveHeader[43];
- rewind(fp2); // Adjust output file point to beginning
- fwrite(waveHeader, sizeof(Uint8), 44, fp2); // Write 44 bytes of WAV header to output file
- fclose(fp1); // Close input file
- fclose(fp2); // Close output file
- fclose(fp3); // Close output file
- printf("\nExp --- completed\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement