Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Recorder.cpp Source File

Recorder.cpp

00001 #include "Recorder.h"
00002 
00003 #define RAM_TOTAL 2048
00004 
00005 AnalogOut dacout(p18);
00006 AnalogIn adcin(p20);
00007 DigitalOut led_play_ok(LED1);
00008 DigitalOut led_rec_ok(LED2);
00009 
00010 Ticker ticker;
00011 extern float *buffer;
00012 int rp = 0;
00013 int wp = 0;
00014 int dropout = 0;
00015 
00016 #define WAVFILE_ERROR_PRINT(RESULT) \
00017     do { \
00018         WavFileResult R = RESULT; \
00019         if (R != WavFileResultOK) { \
00020             char wavfile_error_print_text[BUFSIZ]; \
00021             wavfile_result_string(R, wavfile_error_print_text, sizeof(wavfile_error_print_text)); \
00022             printf("%s (code=%d)\r\n", wavfile_error_print_text, R); \
00023             return 1; \
00024         } \
00025     } while(0)
00026 
00027 
00028 void tickplay(void)
00029 {
00030     /*
00031      * Check the play underflow
00032      */
00033     if (rp != wp) {
00034         dacout = buffer[rp];
00035         rp = (rp + 1) & (RAM_TOTAL - 1);
00036     } else {
00037         dropout++;
00038     }
00039     led_play_ok = !led_play_ok;
00040 }
00041 
00042 void tickrec(void)
00043 {
00044     int np = (wp + 1) & (RAM_TOTAL - 1);
00045     if (np != rp) {
00046         buffer[wp] = adcin;
00047         wp = np;
00048     } else {
00049         dropout++;
00050     }
00051     led_rec_ok = !led_rec_ok;
00052 }
00053 
00054 int play(const char *filename)
00055 {
00056     WavFileResult result;
00057     wavfile_info_t info;
00058     wavfile_data_t data;
00059 
00060     WAVFILE *wf = wavfile_open(filename, WavFileModeRead, &result);
00061     WAVFILE_ERROR_PRINT(result);
00062     WAVFILE_ERROR_PRINT(wavfile_read_info(wf, &info));
00063 
00064     //printf("[PLAY:%s]\r\n", filename);
00065     //printf("\tWAVFILE_INFO_AUDIO_FORMAT(&info)    = %d\r\n", WAVFILE_INFO_AUDIO_FORMAT(&info));
00066     //printf("\tWAVFILE_INFO_NUM_CHANNELS(&info)    = %d\r\n", WAVFILE_INFO_NUM_CHANNELS(&info));
00067     //printf("\tWAVFILE_INFO_SAMPLE_RATE(&info)     = %d\r\n", WAVFILE_INFO_SAMPLE_RATE(&info));
00068     //printf("\tWAVFILE_INFO_BYTE_RATE(&info)       = %d\r\n", WAVFILE_INFO_BYTE_RATE(&info));
00069     //printf("\tWAVFILE_INFO_BLOCK_ALIGN(&info)     = %d\r\n", WAVFILE_INFO_BLOCK_ALIGN(&info));
00070     //printf("\tWAVFILE_INFO_BITS_PER_SAMPLE(&info) = %d\r\n", WAVFILE_INFO_BITS_PER_SAMPLE(&info));
00071 
00072     const int interval_us =  1000000 / WAVFILE_INFO_SAMPLE_RATE(&info);
00073 
00074     rp = 0;
00075     wp = 0;
00076     dropout = 0;
00077     ticker.attach_us(tickplay, interval_us);
00078     while (1) {
00079         int np = (wp + 1) & (RAM_TOTAL - 1);
00080         while (np == rp) {
00081             wait_us(1);
00082         }
00083         WAVFILE_ERROR_PRINT(wavfile_read_data(wf, &data));
00084         if (WAVFILE_DATA_IS_END_OF_DATA(&data)) {
00085             break;
00086         }
00087         buffer[wp] = WAVFILE_DATA_CHANNEL_DATA(&data, 0);
00088         wp = np;
00089     }
00090     ticker.detach();
00091     dacout = 0.5;
00092     led_play_ok = 0;
00093     //printf("\t-- Play done. (dropout=%d) --\r\n", dropout);
00094 
00095     WAVFILE_ERROR_PRINT(wavfile_close(wf));
00096     return 0;
00097 }
00098 
00099 int rec(const char *filename, const int nsec)
00100 {
00101     WavFileResult result;
00102     wavfile_info_t info;
00103     wavfile_data_t data;
00104 
00105     WAVFILE_INFO_AUDIO_FORMAT(&info)    = 1;
00106     WAVFILE_INFO_NUM_CHANNELS(&info)    = 1;
00107     WAVFILE_INFO_SAMPLE_RATE(&info)     = 11025;
00108     WAVFILE_INFO_BYTE_RATE(&info)       = 22050;
00109     WAVFILE_INFO_BLOCK_ALIGN(&info)     = 2;
00110     WAVFILE_INFO_BITS_PER_SAMPLE(&info) = 16;
00111 
00112     WAVFILE *wf = wavfile_open(filename, WavFileModeWrite, &result);
00113     WAVFILE_ERROR_PRINT(result);
00114     WAVFILE_ERROR_PRINT(wavfile_write_info(wf, &info));
00115 
00116     //printf("[REC:%s]\r\n", filename);
00117     //printf("\tWAVFILE_INFO_AUDIO_FORMAT(&info)    = %d\r\n", WAVFILE_INFO_AUDIO_FORMAT(&info));
00118     //printf("\tWAVFILE_INFO_NUM_CHANNELS(&info)    = %d\r\n", WAVFILE_INFO_NUM_CHANNELS(&info));
00119     //printf("\tWAVFILE_INFO_SAMPLE_RATE(&info)     = %d\r\n", WAVFILE_INFO_SAMPLE_RATE(&info));
00120     //printf("\tWAVFILE_INFO_BYTE_RATE(&info)       = %d\r\n", WAVFILE_INFO_BYTE_RATE(&info));
00121     //printf("\tWAVFILE_INFO_BLOCK_ALIGN(&info)     = %d\r\n", WAVFILE_INFO_BLOCK_ALIGN(&info));
00122     //printf("\tWAVFILE_INFO_BITS_PER_SAMPLE(&info) = %d\r\n", WAVFILE_INFO_BITS_PER_SAMPLE(&info));
00123 
00124     const int interval_us =  1000000 / WAVFILE_INFO_SAMPLE_RATE(&info);
00125     const unsigned int samples_for_nsec = WAVFILE_INFO_SAMPLE_RATE(&info) * nsec;
00126 
00127     rp = 0;
00128     wp = 0;
00129     dropout = 0;
00130     unsigned int count = 0;
00131     ticker.attach_us(tickrec, interval_us);
00132     WAVFILE_DATA_NUM_CHANNELS(&data) = 1;
00133     while (1) {
00134         while (rp == wp) {
00135             wait_us(1);
00136         }
00137 
00138         WAVFILE_DATA_CHANNEL_DATA(&data, 0) = buffer[rp];
00139         rp = (rp + 1) & (RAM_TOTAL - 1);
00140         WAVFILE_ERROR_PRINT(wavfile_write_data(wf, &data));
00141 
00142         count++;
00143         if (count > samples_for_nsec) {
00144             break;
00145         }
00146     }
00147     ticker.detach();
00148     led_rec_ok = 0;
00149     //printf("\t-- Rec done. (dropout=%d) --\r\n", dropout);
00150 
00151     WAVFILE_ERROR_PRINT(wavfile_close(wf));
00152     return 0;
00153 }