This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: TI C/C++ Compiler
I had converted a .pcm file to a .wav file and knows how it sounds. After I converted the .pcm file to a .csv file and read the .csv file's graph as a representation of the sound file's sound magnitudes, I'm now trying to figure out how to input the .csv file, and then output the .wav file and see if it sounds the same or not. However, every time I debug the program with Code Composer Studio v.10, it always skips the while loop. Does anyone see where the error in my code and what I'm missing? Basically I used the same code for my .pcm to .csv AND .pcm to .wav conversions, except I switched the file paths, yet it's not working. Any help would be greatly appreciated....
Here's my code below...
----------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* 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
void main()
{
FILE *fp1,*fp2; // File pointers
Uint32 i;
//unsigned int j; // Unsigned long integer used as a counter
printf("Exp. 1.2 --- file IO\n");
fp1 = fopen("E:\\Code Composer Studio Workspace\\EE489\\Lab1_FileIO_1\\OUT.xlsx", "rb"); // Open input file
//fp1 = fopen("..\\.cproject", "rb"); // Open input file
fp2 = fopen("E:\\Code Composer Studio Workspace\\EE489\\Lab1_FileIO_1\\output\\C55DSPUSBStickAudioTest.wav", "wb"); // Open output file
if (fp1 == NULL) // Check if the input file exists
{
printf("Failed to open input file 'OUT.xlsx'\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
{
// fread(ch, sizeof(Uint8), SIZE, fp1);
// j++;
fwrite(ch, sizeof(Uint8), SIZE, fp2); // Write SIZE of data bytes to output file
//fwrite(ch, sizeof(Uint8), SIZE, fp3); // Write SIZE of data bytes to output file
i += SIZE;
printf("%ld bytes processed\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
printf("\nExp --- completed\n");
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
I suspect some problem with the C I/O functions (printf, fopen, etc.) Please let me know if the article Tips for using printf resolves the problem.
Thanks and regards,
-George
As mentioned previously, it only skips over the while loop, with the condition being if it reads an input of 1024 bytes. So it maybe might have something to do with the while loop
The while loop is controlled by whether a call to fread succeeded. It must fail on the first call. Why did it fail? Did the earlier fseek also fail?
Thanks and regards,
-George
Evidently, it failed to read because the fread() isn't taking in a size of 1024 in unsigned integers in every 8 bit chunks. That leaves me to a dead end though; the while loop succeeded when reading a .pcm file and converting it to an .xlsx and .wav file, yet the other way around, it fails.
I continue to think one of the Tips for using printf is likely to help. Maybe you have overflowed the stack in all of your programs, and it somehow didn't fail until now.
Thanks and regards,
-George
I just now noticed this problem in your code ...
Eric Graham said:fp2 = fopen("E:\\Code Composer Studio Workspace\\EE489\\Lab1_FileIO_1\\output\\C55DSPUSBStickAudioTest.wav", "wb"); // Open output file
if (fp1 == NULL) // Check if the input file exists
The fopen assigns to fp2, but fp1 is checked for NULL. You need to check fp2 for NULL.
I'm sorry I didn't notice this sooner.
Thanks and regards,
-George