Here is my program:
# include <stdio.h>#include <math.h># include <altera_up_sd_card_avalon_interface.h># include "functions.h"
//#include <time.h>
# define Pushbuttons ((volatile long *) 0x1011410)
short int sd_fileh;
int main()
{
// clock_t tic, toc;
// float run_time;
printf("Running...press the button to begin\n");
int N_samples = 256, NumChannels = 0, sampleRate = 0;
int i,j;
while(1)
{
long PBval;
*Pushbuttons = 0; //clear out any changes so far
PBval = *Pushbuttons;
// printf("not be pushed %ld\n",PBval);
while (1)
{
PBval = *Pushbuttons;
if (PBval == 0)
break;
}
// printf("pushed %ld\n",PBval);
// short int temp;
// signed int *temp2;
float data;
// double number;
int count;
// int n;
// These variables are used
// when wanting to display the header of the wav file
long int long_total;
short int byte1, byte2, byte3, byte4;
signed int total;
printf("Accessing SD Card ...\n");
alt_up_sd_card_dev *sd_card_dev = alt_up_sd_card_open_dev(SDCARD_NAME);
if(sd_card_dev != 0)
{
if(alt_up_sd_card_is_Present())
{
if(alt_up_sd_card_is_FAT16())
printf("Card is FAT16\n");
else
printf("Card is not FAT16\n");
// sd_fileh = alt_up_sd_card_fopen("testfile.txt", true);
sd_fileh = alt_up_sd_card_fopen("TESTSIG4.WAV", false);
if (sd_fileh < 0)
printf("Problem reading file. Error %i", sd_fileh);
else // open the file
{
printf("Reading file...\n");
// // Jump over the header of the wav file
// count = 0;
// while (count < 44) // 44-byte header
// {
// alt_up_sd_card_read(sd_fileh);
// count++;
// }
// Get information of number of channels
count = 0;
while (count < 22) // 44-byte header
{
alt_up_sd_card_read(sd_fileh);
count++;
}
// printf("NumChannels ");
byte1 = 0;byte2 = 0;total = 0;
byte1 = alt_up_sd_card_read(sd_fileh);
byte2 = alt_up_sd_card_read(sd_fileh);
total = (byte1 & 0x00ff) | (byte2 << 8);
NumChannels = total;
// printf("%d\n",total);
// printf("SampleRate ");
byte1 = 0;byte2 = 0;byte3 = 0;byte4 = 0;long_total = 0;
byte1 = alt_up_sd_card_read(sd_fileh);
byte2 = alt_up_sd_card_read(sd_fileh);
byte3 = alt_up_sd_card_read(sd_fileh);
byte4 = alt_up_sd_card_read(sd_fileh);
long_total = (byte4 << 24) | (byte3 << 16) | (byte2 << 8) | ( byte1 & 0x000000ff);
sampleRate = long_total;
// printf("%ld\n",long_total);
count = 0;
while (count < 16) // 44-byte header
{
alt_up_sd_card_read(sd_fileh);
count++;
}....
You should know the structure of a wav file. You read the header of wav file first and then you have to continue reading its data two bytes by two bytes. Hope this will be able to help you.