Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /*
  2. * fileIOTest.c
  3. *
  4. * Created on: Mar 10, 2012
  5. * Author: BLEE
  6. *
  7. * For the book "Real Time Digital Signal Processing:
  8. * Fundamentals, Implementation and Application, 3rd Ed"
  9. * By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
  10. * Publisher: John Wiley and Sons, Ltd
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "tistdtypes.h"
  16.  
  17. Uint8 waveHeader[44]={ // 44 bytes for WAV file header
  18. 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00,
  19. 0x57, 0x41, 0x56, 0x45, 0x66, 0x6D, 0x74, 0x20,
  20. 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
  21. 0x40, 0x1F, 0x00, 0x00, 0x80, 0x3E, 0x00, 0x00,
  22. 0x02, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61,
  23. 0x00, 0x00, 0x00, 0x00};
  24.  
  25. #define SIZE 1024
  26. Uint8 ch[SIZE]; // Declare a char[1024] array for experiment
  27. Uint16 number[SIZE/2];
  28. Uint8 char_array[6];
  29.  
  30. void DK_MF_ITOA(Uint16 number, Uint8* pointer)
  31. {
  32. pointer[0] = number / 10000 + 0x30;
  33. pointer[1] = (number / 1000) % 10 + 0x30;
  34. pointer[2] = (number / 100) % 10 + 0x30;
  35. pointer[3] = (number / 10) % 10 + 0x30;
  36. pointer[4] = number % 10 + 0x30;
  37. pointer[5] = 0;
  38.  
  39. return;
  40. }
  41.  
  42. void main()
  43. {
  44. FILE *fp1,*fp2,*fp3; // File pointers
  45. Uint32 i; // Unsigned long integer used as a counter
  46. Uint32 j;
  47.  
  48. printf("Exp. 1.2 --- file IO\n");
  49.  
  50. fp1 = fopen("..\\data\\C55DSPUSBStickAudioTest.pcm", "rb"); // Open input file
  51. fp2 = fopen("..\\data\\C55DSPUSBStickAudioTest.wav", "wb"); // Open output file
  52. fp3 = fopen("..\\data\\C55DSPUSBStickAudioTest.csv", "wb"); // Open output file
  53.  
  54. if (fp1 == NULL) // Check if the input file exists
  55. {
  56. printf("Failed to open input file 'C55DSPUSBStickAudioTest.pcm'\n");
  57. exit(0);
  58. }
  59.  
  60. fseek(fp2, 44, 0); // Advance output file point 44 bytes
  61. i=0;
  62. while (fread(ch, sizeof(Uint8), SIZE, fp1) == SIZE) // Read in SIZE of input data bytes
  63. {
  64. fwrite(ch, sizeof(Uint8), SIZE, fp2); // Write SIZE of data bytes to output file
  65. i += SIZE;
  66. printf("%ld bytes processed\n", i); // Show the number of data is processed
  67.  
  68. }
  69.  
  70. fclose(fp1); // Close input file
  71. fp1 = fopen("..\\data\\C55DSPUSBStickAudioTest.pcm", "rb"); // Open input file
  72. i=0;
  73. while (fread(ch, sizeof(Uint8), SIZE, fp1) == SIZE) // Read in SIZE of input data bytes
  74. {
  75. for(j=0; j<=SIZE; j=j+2)
  76. {
  77. number[j/2] = (ch[j]<<8) + ch[j+1];
  78. }
  79.  
  80. for(j=0; j<=SIZE/2; j=j+1)
  81. {
  82. DK_MF_ITOA(number[j], char_array);
  83. fwrite(char_array, 1, 6, fp3); // Write SIZE of data bytes to output file
  84. fp3 += 6;
  85. }
  86.  
  87. i += SIZE;
  88. printf("%ld bytes saved\n", i); // Show the number of data is processed
  89.  
  90. }
  91.  
  92. waveHeader[40] = (Uint8)(i&0xff); // Update the size parameter into WAV header
  93. waveHeader[41] = (Uint8)(i>>8)&0xff;
  94. waveHeader[42] = (Uint8)(i>>16)&0xff;
  95. waveHeader[43] = (Uint8)(i>>24)&0xff;
  96. waveHeader[4] = waveHeader[40];
  97. waveHeader[5] = waveHeader[41];
  98. waveHeader[6] = waveHeader[42];
  99. waveHeader[7] = waveHeader[43];
  100.  
  101. rewind(fp2); // Adjust output file point to beginning
  102. fwrite(waveHeader, sizeof(Uint8), 44, fp2); // Write 44 bytes of WAV header to output file
  103.  
  104. fclose(fp1); // Close input file
  105. fclose(fp2); // Close output file
  106. fclose(fp3); // Close output file
  107.  
  108. printf("\nExp --- completed\n");
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement