This software will read a .wav file from an SD card and display the header information contained within it. It is designed to work with the SD card on the mbed carrier which is part of the RS-EDP system.

Dependencies:   mbed SDFileSystem

Files at this revision

API Documentation at this revision

Comitter:
DavidGilesHitex
Date:
Fri Nov 19 09:54:23 2010 +0000
Commit message:

Changed in this revision

FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
HeaderFiles/Wav_Support_File.h Show annotated file Show diff for this revision Revisions of this file
HeaderFiles/defines.h Show annotated file Show diff for this revision Revisions of this file
HeaderFiles/mbed_Port_Structure.h Show annotated file Show diff for this revision Revisions of this file
HeaderFiles/misra_types.h Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
SourceFiles/Wav_Support_File.cpp Show annotated file Show diff for this revision Revisions of this file
SourceFiles/main.cpp Show annotated file Show diff for this revision Revisions of this file
SourceFiles/mbed_Port_Structure.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FATFileSystem.lib	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HeaderFiles/Wav_Support_File.h	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,24 @@
+/* Wave File Support Header File */
+/* ***************************** */
+
+
+
+/* Function Prototypes */
+extern uint8_t wave_file_check(uint8_t *wave_array, FILE *open_file);
+
+extern struct wave_header_construction
+				{	sint8_t  identification_block[5];
+					uint32_t length_of_file;
+					sint8_t  identifier1[5];
+					sint8_t  identifier2[5];
+					uint32_t position_of_data;
+					uint16_t format_tag;
+					uint16_t number_of_channels;
+					uint32_t samples_per_second;					
+					uint32_t bytes_per_second;
+					uint16_t bytes_per_sample;
+					uint16_t bits_per_sample;
+					sint8_t  marker[5];
+					uint32_t number_of_bits_in_sample;
+				} wave_file_info;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HeaderFiles/defines.h	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,11 @@
+/* Defines */
+/* ******* */
+
+
+/* Software Revision Number */
+#define FIRMWARE_VERSION 3
+
+
+/* LED On/OFF Defines */
+#define LED_ON 1
+#define LED_OFF 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HeaderFiles/mbed_Port_Structure.h	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,17 @@
+/* Header Files for Port Structure */
+/* ******************************* */
+
+
+extern void setup_mbed_ports(void);
+
+
+/* Configure the I/O Port Structure */
+extern DigitalOut User_Led1;
+extern DigitalOut User_Led2;
+extern DigitalOut User_Led3;
+extern DigitalOut User_Led4;
+
+
+/* Serial interface via USB  */
+extern Serial pc;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HeaderFiles/misra_types.h	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,32 @@
+/* Hitex Standard Header File */
+/* Types As Recommended By MISRA */
+
+
+
+/* Bytes (8bit length) */
+typedef unsigned char uint8_t;
+typedef char sint8_t;
+
+/* Half Words (16bit lengths) */
+typedef unsigned short uint16_t;
+typedef short sint16_t;
+
+/* Words (32bit lengths) */
+/* Also int */
+typedef unsigned int uint32_t;
+typedef long sint32_t;
+
+/* Double Words */
+typedef unsigned long long uint64_t;
+typedef long long sint64_t;
+
+
+/*
+ Constants should use the following suffixes also 
+
+u - Unsigned
+f - floating
+L - Long double (64bit)
+
+*/
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/SDFileSystem/#b1ddfc9a9b25
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SourceFiles/Wav_Support_File.cpp	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,224 @@
+/* Wav File Support */
+/* **************** */
+
+#include "mbed.h"
+#include "defines.h"
+#include "misra_types.h"
+#include "mbed_Port_Structure.h"
+
+
+
+/* Function Prototypes */
+uint8_t wave_file_check(uint8_t *wave_array, FILE *open_file);
+
+
+/* Global Variables */
+struct wave_header_construction
+				{	sint8_t  identification_block[5];
+					uint32_t length_of_file;
+					sint8_t  identifier1[5];
+					sint8_t  identifier2[5];
+					uint32_t position_of_data;
+					uint16_t format_tag;
+					uint16_t number_of_channels;
+					uint32_t samples_per_second;					
+					uint32_t bytes_per_second;
+					uint16_t bytes_per_sample;
+					uint16_t bits_per_sample;
+					sint8_t  marker[5];
+					uint32_t number_of_bits_in_sample;
+				} wave_file_info;
+
+
+
+
+
+/* Wave File Checker */
+uint8_t wave_file_check(uint8_t *wave_array, FILE *open_file)
+	{
+		uint8_t brk_flag = 0u;
+		uint8_t x = 0u;
+		uint8_t valid_sample = 0u;
+		sint16_t ch = 0;
+		
+		pc.printf("\n\rPrinting out.WAV file header...\n\r");	
+
+		for (x = 0; x < 0x2bu; x++)												/* Read the first 0x2b bytes of the file as this is the haeder informtation relating to sample rate etc. */
+			{
+				ch = fgetc(open_file);											/* Get the first byte from the file */
+				if (ch == EOF)  												/* Check for eof */
+					{
+						pc.printf("   File too short to be a wave file\n\r");	/* If we have less than 0x2b then file is not a wave file. */
+						valid_sample = 0u;
+						brk_flag = 1u;
+					}
+				else{
+						wave_array[x] = ch;	   									/* Load data in to header file array */
+					}
+					
+				if (brk_flag == 1u) 
+					{
+						break;
+					}
+			}
+			
+		if (brk_flag == 0u) 													/* OK we have downloaded the header into an Array. Lets check to see if its a valid file */
+			{
+				pc.printf("   Identification block (4 ASCII bytes)= ");								/* First 4 bytes are header block */
+				for (x = 0u; x < 4; x++) 
+					{
+						pc.putc(wave_array[x]);                   									/* and write them on the screen */
+						wave_file_info.identification_block[x] = wave_array[x];
+					}
+				wave_file_info.identification_block[4] = 0;											/* add the string terminator on the end */
+				pc.printf(" '%s'\n\r", wave_file_info.identification_block);
+	
+				
+				pc.printf("   Length of file (4 bytes) = ");										/* Length of file. */
+				for (x = 4u; x < 8u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.length_of_file =  ( ((uint32_t) wave_array[7] << 24) 
+												 + ((uint32_t) wave_array[6] << 16) 
+												 + ((uint32_t) wave_array[5] << 8) 
+												 + ((uint32_t) wave_array[4]) ); 
+				pc.printf(" = %d bytes\n\r", wave_file_info.length_of_file);
+
+			
+				pc.printf("   Identifier (4 ASCII bytes) = ");										/* indentifier string1 */
+				for (x = 8u; x < 12u; x++) 
+					{
+						pc.putc(wave_array[x]);                   									/* and write them on the screen */
+						wave_file_info.identifier1[x-8] = wave_array[x];
+					}
+				wave_file_info.identification_block[4] = 0;											/* add the string terminator on the end */
+				pc.printf(" '%s'\n\r", wave_file_info.identifier1);
+	
+				
+				pc.printf("   Identifier (4 ASCII bytes) = ");										/* Identifier String 2 */
+				for (x = 12; x < 16u; x++) 
+					{
+						pc.putc(wave_array[x]);                   									/* and write them on the screen */
+						wave_file_info.identifier2[x-12] = wave_array[x];
+					}
+				wave_file_info.identifier2[4] = 0;													/* add the string terminator on the end */
+				pc.printf(" '%s'\n\r", wave_file_info.identifier2);
+						
+
+				pc.printf("   Position of data (4 bytes) = ");										/* Position of data */
+				for (x = 16u; x < 20u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.position_of_data =  ( ((uint32_t) wave_array[19] << 24) 
+												   + ((uint32_t) wave_array[18] << 16) 
+												   + ((uint32_t) wave_array[17] << 8) 
+												   + ((uint32_t) wave_array[16]) ); 
+				pc.printf(" = %d\n\r", wave_file_info.position_of_data);
+
+			
+				pc.printf("   Format Tag - Always 1 for Wave PCM (2 bytes) = ");					/* format tag */
+				for (x = 20u; x < 22u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.format_tag = ( ((uint32_t) wave_array[21] << 8) 
+											+ ((uint32_t) wave_array[20]) ); 
+				pc.printf(" = %d\n\r", wave_file_info.format_tag);
+
+						
+				pc.printf("   Number of channels (2 bytes) = ");									/* Number of channels, mono or steroeo */
+				for (x = 22u; x < 24u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.number_of_channels = ( ((uint32_t) wave_array[23] << 8) 
+													+ ((uint32_t) wave_array[22]) ); 
+				pc.printf(" = %d channel(s)\n\r", wave_file_info.number_of_channels);
+
+					
+				pc.printf("   Samples per second (4 bytes) = ");		 							/* Samples per second */
+				for (x = 24u; x < 28u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.samples_per_second =( ((uint32_t) wave_array[27] << 24) 
+												   + ((uint32_t) wave_array[26] << 16) 
+												   + ((uint32_t) wave_array[25] << 8) 
+												   + ((uint32_t) wave_array[24]) ); 
+				pc.printf(" = %d samples per sec\n\r", wave_file_info.samples_per_second);
+
+								
+				pc.printf("   Bytes Per Second (4 bytes) = ");	 									/* Bytes per second */
+				for (x = 28u; x < 32u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.bytes_per_second =  ( ((uint32_t) wave_array[31] << 24) 
+												   + ((uint32_t) wave_array[30] << 16) 
+												   + ((uint32_t) wave_array[29] << 8) 
+												   + ((uint32_t) wave_array[28]) ); 
+				pc.printf(" = %d bytes per sec\n\r", wave_file_info.bytes_per_second);
+
+			
+				pc.printf("   Bytes Per Sample (2 bytes) = ");   									/* Bytes per sample */
+				for (x = 32u; x < 34u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.bytes_per_sample = 	( ((uint32_t) wave_array[33] << 8) 
+													+ ((uint32_t) wave_array[32]) ); 
+				pc.printf(" = %d bytes\n\r", wave_file_info.bytes_per_sample);
+
+					
+				pc.printf("   Bits Per Sample (2 bytes) = ");										/* Bits per sample */
+				for (x = 34u; x < 36u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.bits_per_sample = ( ((uint32_t) wave_array[35] << 8) 
+													+ ((uint32_t) wave_array[34]) ); 
+				pc.printf(" = %d bits\n\r", wave_file_info.bits_per_sample);
+
+						
+				pc.printf("   Marker (4 ASCII bytes) = ");											/* Marker */
+				for (x = 36u; x < 40u; x++) 
+					{
+						pc.putc(wave_array[x]);
+						wave_file_info.marker[x-36] = wave_array[x];						
+					}
+				wave_file_info.marker[4] = 0;
+				pc.printf(" '%s'\n\r", wave_file_info.marker);
+						
+
+				pc.printf("   The number of bits in the sample (4 bytes) = "); 						/* Number of bits per sample. */
+				for (x = 40u; x < 44u; x++) 
+					{
+						pc.printf("%d ",wave_array[x]);
+					}
+				wave_file_info.number_of_bits_in_sample =  (  ((uint32_t) wave_array[43] << 24) 
+												   			+ ((uint32_t) wave_array[42] << 16) 
+												   			+ ((uint32_t) wave_array[41] << 8) 
+												   			+ ((uint32_t) wave_array[40]) ); 
+				pc.printf(" = %d bits\n\r", wave_file_info.number_of_bits_in_sample);
+					
+			
+				/* Check for valid Wave file */
+				if (((wave_array[0] == 'R') && (wave_array[1] == 'I') && (wave_array[2] == 'F') && (wave_array[3] == 'F'))
+				&&  ((wave_array[8] == 'W') && (wave_array[9] == 'A') && (wave_array[0x0a] == 'V') && (wave_array[0x0b] == 'E'))
+				&& ((wave_array[0x0c] == 'f') && (wave_array[0x0d] == 'm') && (wave_array[0x0e] == 't') && (wave_array[0x0f] == ' '))
+				&& (wave_array[0x14] == 0x01)) valid_sample = 1u;
+				}
+
+		pc.printf("Finished examining the .WAV file header\n\r");
+		if (valid_sample == 1)
+			{
+				pc.printf("Sample is a valid .WAV sample file\n\n\r");
+			}
+		else{
+				pc.printf("Sample is a NOT a valid .WAV sample file\n\n\r");	
+			}	
+
+		return valid_sample;
+	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SourceFiles/main.cpp	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,85 @@
+/* SD-Card Utility Program for RS-EDP and RS-EDP mbed adapter module */
+/* ***************************************************************** */
+
+
+
+#include "mbed.h"
+
+#include "defines.h"
+#include "misra_types.h"
+#include "mbed_Port_Structure.h"
+
+#include "SDFileSystem.h"
+#include "Wav_Support_File.h"
+
+
+
+
+/* Main Loop Here */
+int main(void) 
+	{
+		FILE *wavfile;
+		char file_name_with_root_and_path[30];
+		char input_file_name[30];
+		char *pointer1;
+		char *pointer2;
+		char n = 0;
+		uint8_t wave_array[30];	
+
+
+
+		for (n = 0; n < 30; n++)												/* initialise arrays to zero */
+			{
+				file_name_with_root_and_path[n] = 0;	
+				input_file_name[n] = 0;
+			}
+			
+		setup_mbed_ports();														/* Setup the I/O Structure of the mbed module */
+
+		pc.printf("\n\n\n\n\n\n\n\rWelcome the the mbed SD-CARD Utility\n\r");
+		pc.printf("Software Revision Number: %d\n\r\n", FIRMWARE_VERSION);
+		
+		pc.printf("This application reads a .WAV file from the SC-CARD and\n\r"); 
+		pc.printf("displays the header information contained within it\n\n\r");
+
+        User_Led1 = LED_OFF;
+        User_Led2 = LED_OFF;
+        User_Led3 = LED_OFF;
+        User_Led4 = LED_OFF;    
+        
+		while(1)
+			{            
+        		pointer1 = file_name_with_root_and_path;								/* set pointer1 to point at the complete name and path array */
+				strcpy(pointer1, "/sd/");												/* Start to build the root, directory and file name of the file we want to examine */
+       
+				pointer2 = input_file_name;												/* set pointer2 to point at an empty string array to take the user input file name */
+	        	pc.printf("Enter the file name you wish to check\n\r");
+
+    	    	scanf("%s", pointer2);													/* Get the file name into an array called 'input_file_name', pointed to by a pointer called pointer2 */
+        		strcat(pointer1, pointer2);												/* Add on the entered file name to the root direcotry */
+
+		        pc.printf("Opening the file: %s\n\r", file_name_with_root_and_path);	/* Print the name including the root and path of the file */
+	  			wavfile = fopen(file_name_with_root_and_path,"r");						/* Attempt to open the file for read only*/
+
+				if (wavfile == 0 ) 
+					{
+    					/* Problems - can not open the file whos name we just inputed */
+    					pc.printf("Unable to open wav file '%s'\n\r", file_name_with_root_and_path);
+						pc.printf("Please try again\n\r");    				
+					}
+   				else{
+  						/* ok file is now open - lets print the header file */
+		  				wave_file_check(wave_array, wavfile);
+		  			}
+  
+
+				/* you can now access all of the .WAV header file information in a global structure called 'wave_file_info' */
+				/* the structure provides you with all the necessary information to manage sample rates and timings etc */
+				/* eg */		
+				/* sample_rate    = wave_file_info.samples_per_second; */
+				/* stereo_or_mono = wave_file_info.number_of_channels  */ 
+
+	  			fclose(wavfile);														/* Close the file */
+  			}
+  	}
+  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SourceFiles/mbed_Port_Structure.cpp	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,47 @@
+/* Configure the I/O Port Structure */
+/* ******************************** */
+
+
+/* includes files */
+#include "mbed.h"                                /* Header file for mbed module */
+#include "defines.h"                            /* User defines */
+#include "misra_types.h"                         /* MISRA Types */
+#include "SDFileSystem.h"                        /* File System for SD Card */
+
+
+
+/* Digital I/O */
+DigitalOut User_Led1(LED1);
+DigitalOut User_Led2(LED2);
+DigitalOut User_Led3(LED3);
+DigitalOut User_Led4(LED4);
+
+
+/* SPI Interface to RS-EDP CNTRL_SPI and the SD Card on Adapter board */
+SDFileSystem sd(p11, p12, p13, p14, "sd");
+
+
+/* Configure the USB as a virtual communications port */
+Serial pc(USBTX, USBRX);
+
+
+
+
+/* Function Prototypes */
+void setup_mbed_ports(void);
+
+
+
+/* Configure the I/O Ports */
+void setup_mbed_ports(void)
+    {
+        pc.baud(115000);                        /* Baud rate should be 115k baud */
+          pc.format(8, Serial::None, 1);        /* format is 8 data bits, no stop bit, no parity */
+
+        User_Led1 = LED_ON;
+        User_Led2 = LED_OFF;
+        User_Led3 = LED_ON;
+        User_Led4 = LED_OFF;
+    }
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Nov 19 09:54:23 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/3944f1e2fa4f