Reproductor de archivos wav

Dependencies:   SDFileSystem mbed-src mbed

Fork of SDFileSystem_HelloWorld by mbed official

Files at this revision

API Documentation at this revision

Comitter:
EduardoVitela
Date:
Fri Jun 12 04:49:18 2015 +0000
Parent:
0:bdbd3d6fc5d5
Commit message:
publicando

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
wavplayer.cpp Show annotated file Show diff for this revision Revisions of this file
wavplayer.h Show annotated file Show diff for this revision Revisions of this file
--- a/SDFileSystem.lib	Fri Dec 07 11:25:01 2012 +0000
+++ b/SDFileSystem.lib	Fri Jun 12 04:49:18 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
+http://developer.mbed.org/users/EduardoVitela/code/SDFileSystem/#c8f66dc765d4
--- a/main.cpp	Fri Dec 07 11:25:01 2012 +0000
+++ b/main.cpp	Fri Jun 12 04:49:18 2015 +0000
@@ -1,19 +1,16 @@
 #include "mbed.h"
+#include "wavplayer.h"
 #include "SDFileSystem.h"
- 
-SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
- 
-int main() {
-    printf("Hello World!\n");   
- 
-    mkdir("/sd/mydir", 0777);
-    
-    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
-    if(fp == NULL) {
-        error("Could not open file for write\n");
-    }
-    fprintf(fp, "Hello fun SD Card World!");
-    fclose(fp); 
- 
-    printf("Goodbye World!\n");
+
+SDFileSystem sd(PTD6, PTD7, PTD5, PTC8, "sd"); // MOSI, MISO, SCK, CS
+
+int main()
+{
+    WavPlayer myWavPlayer;
+    myWavPlayer.play_wave("/sd/micro/guns.wav"); //  8 bit sample size
+    myWavPlayer.play_wave("/sd/micro/cain.wav");
+    myWavPlayer.play_wave("/sd/micro/link.wav");
+    myWavPlayer.play_wave("/sd/micro/rock.wav");
+    myWavPlayer.play_wave("/sd/micro/dune.wav");
 }
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Fri Jun 12 04:49:18 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#a5bce224b5d8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wavplayer.cpp	Fri Jun 12 04:49:18 2015 +0000
@@ -0,0 +1,156 @@
+/*
+ Library wave file player by Tom Coxon
+
+ Based on WAVEplayer by Vlad Cazan/Stephan Rochon modified by Tom Coxon to:
+
+ 1. Run correctly on the Embedded Artists LPCXpresso baseboard.
+ 2. To play 8 bit sample size in addition to original 16 bit
+ 3. To be more fault tolerant when playing wav files.
+*/
+
+#include "wavplayer.h"
+
+Ticker tick;
+Serial pc(USBTX,USBRX);
+DigitalIn p1(SW1);
+AnalogOut DACout(PTE30);
+
+void WavPlayer::dac_out() {
+    if (DAC_on) {
+        DACout.write_u16(DAC_fifo[DAC_rptr]);
+        DAC_rptr=(DAC_rptr+1) & 0xff;
+    }
+}
+
+void WavPlayer::play_wave(char *wavname) {
+    unsigned chunk_id,chunk_size,channel;
+    unsigned data,samp_int,i;
+    short dac_data;
+    char *slice_buf;
+    short *data_sptr;
+    FMT_STRUCT wav_format;
+    FILE *wavfile;
+    long slice,num_slices;
+    DAC_wptr=0;
+    DAC_rptr=0;
+
+    size_t result;
+
+    for (i=0;i<256;i+=2) {
+        DAC_fifo[i]=0;
+        DAC_fifo[i+1]=3000;
+    }
+    DAC_wptr=4;
+    DAC_on=0;
+
+    pc.printf("Reproduciendo arcivo '%s'\r\n",wavname);
+
+    wavfile=fopen(wavname,"rb");
+    if (!wavfile) {
+        pc.printf("Incapas de abrir archivo '%s'\r\n",wavname);
+        exit(1);
+    }
+
+    fread(&chunk_id,4,1,wavfile);
+    fread(&chunk_size,4,1,wavfile);
+    while (!feof(wavfile)) {
+        pc.printf("Read chunk ID 0x%x, size 0x%x\r\n",chunk_id,chunk_size);
+        switch (chunk_id) {
+            case 0x46464952:
+                fread(&data,4,1,wavfile);
+                pc.printf("RIFF chunk\r\n");
+                pc.printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+                pc.printf("  RIFF type 0x%x\r\n",data);
+                break;
+            case 0x20746d66:
+                fread(&wav_format,sizeof(wav_format),1,wavfile);
+                pc.printf("FORMAT chunk\r\n");
+                pc.printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+                pc.printf("  compression code %d\r\n",wav_format.comp_code);
+                pc.printf("  %d channels\r\n",wav_format.num_channels);
+                pc.printf("  %d samples/sec\r\n",wav_format.sample_rate);
+                pc.printf("  %d bytes/sec\r\n",wav_format.avg_Bps);
+                pc.printf("  block align %d\r\n",wav_format.block_align);
+                pc.printf("  %d bits per sample\r\n",wav_format.sig_bps);
+                if (chunk_size > sizeof(wav_format))
+                    fseek(wavfile,chunk_size-sizeof(wav_format),SEEK_CUR);
+// create a slice buffer large enough to hold multiple slices
+                slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
+                if (!slice_buf) {
+                    pc.printf("Unable to malloc slice buffer");
+                    exit(1);
+                }
+                break;
+            case 0x61746164:
+                slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
+                if (!slice_buf) {
+                    pc.printf("Unable to malloc slice buffer");
+                    exit(1);
+                }
+                num_slices=chunk_size/wav_format.block_align;
+                pc.printf("DATA chunk\r\n");
+                pc.printf("  chunk size %d (0x%x)\r\n",chunk_size,chunk_size);
+                pc.printf("  %d slices\r\n",num_slices);
+                pc.printf("  Ideal sample interval=%d\r\n",(unsigned)(1000000.0/wav_format.sample_rate));
+                samp_int=1000000/(wav_format.sample_rate);
+                pc.printf("  programmed interrupt tick interval=%d\r\n",samp_int);
+
+// starting up ticker to write samples out -- no printfs until tick.detach is called
+                tick.attach_us(this,&WavPlayer::dac_out, samp_int);
+                DAC_on=1;
+                for (slice=0;slice<num_slices;slice+=SLICE_BUF_SIZE) {
+
+                    result = fread(slice_buf,wav_format.block_align*SLICE_BUF_SIZE,1,wavfile);
+                    if (feof(wavfile)) {
+                        pc.printf("Oops -- not enough slices in the wave file\r\n");
+
+                        break;
+                    }
+
+                    data_sptr=(short *)slice_buf;
+                    for (i=0;i<SLICE_BUF_SIZE;i++) {
+                        dac_data=0;
+
+// for a stereo wave file average the two channels.
+                        for (channel=0;channel<wav_format.num_channels;channel++) {
+                            switch (wav_format.sig_bps) {
+                                case 16:
+                                    dac_data+=(  ((int)(*data_sptr++)) +32768 );
+                                    break;
+                                case 8:
+                                    dac_data+=(  ((int)(*data_sptr++)) +32768 <<8);
+                                    break;
+                            }
+                        }
+                        DAC_fifo[DAC_wptr]=dac_data;
+                        DAC_wptr=(DAC_wptr+1) & 0xff;
+                        while (DAC_wptr==DAC_rptr) {
+                            wait_us(10);
+                        }
+                    }
+                    if(p1==0){
+                        break;    
+                    }
+                }
+                DAC_on=0;
+                tick.detach();
+                pc.printf("Ticker detached\r\n");
+                free(slice_buf);
+                break;
+            case 0x5453494c:
+                pc.printf("INFO chunk, size %d\r\n",chunk_size);
+                fseek(wavfile,chunk_size,SEEK_CUR);
+                break;
+            default:
+                pc.printf("unknown chunk type 0x%x, size %d\r\n",chunk_id,chunk_size);
+                data=fseek(wavfile,chunk_size,SEEK_CUR);
+                break;
+        }
+        fread(&chunk_id,4,1,wavfile);
+        fread(&chunk_size,4,1,wavfile);
+    }
+    pc.printf("++++++++++++ Fin de reproducion del archivo ++++++++++\r\n");
+    fclose(wavfile);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wavplayer.h	Fri Jun 12 04:49:18 2015 +0000
@@ -0,0 +1,51 @@
+/*
+ Library wave file player by Tom Coxon
+
+ Based on WAVEplayer by Vlad Cazan/Stephan Rochon modified by Tom Coxon to:
+
+ 1. Run correctly on the Embedded Artists LPCXpresso baseboard.
+ 2. To play 8 bit sample size in addition to original 16 bit
+ 3. To be more fault tolerant when playing wav files.
+*/
+
+#ifndef WAVPLAYER_H
+#define WAVPLAYER_H
+
+#include "mbed.h"
+
+#define SAMPLE_FREQ 40000
+#define BUF_SIZE (SAMPLE_FREQ/10)
+#define SLICE_BUF_SIZE 1
+
+typedef struct uFMT_STRUCT {
+    short comp_code;
+    short num_channels;
+    unsigned sample_rate;
+    unsigned avg_Bps;
+    short block_align;
+    short sig_bps;
+} FMT_STRUCT;
+
+class WavPlayer {
+
+public:
+
+    void play_wave(char *wavname);
+
+private:
+
+    void cleanup(char *);
+    void fill_adc_buf(short *, unsigned);
+    void swapword(unsigned *);
+
+// a FIFO for the DAC
+    short DAC_fifo[256];
+    short DAC_wptr;
+    short DAC_rptr;
+    short DAC_on;
+
+    void dac_out();
+
+};
+
+#endif