Daiki Kato / EasyPlaybackPWM
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EasyPlaybackPWM.cpp Source File

EasyPlaybackPWM.cpp

00001 /* mbed EasyPlaybackPWM Library
00002  * Copyright (C) 2017 dkato
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "EasyPlaybackPWM.h"
00019 
00020 EasyPlaybackPWM::EasyPlaybackPWM(PinName pwm_l, PinName pwm_r) : audio(pwm_l, pwm_r), _skip(false), _pause(false)
00021 {
00022     _audio_buf = new uint8_t[AUDIO_WRITE_BUFF_SIZE];
00023 }
00024 
00025 EasyPlaybackPWM::~EasyPlaybackPWM()
00026 {
00027     delete [] _audio_buf;
00028 }
00029 
00030 bool EasyPlaybackPWM::get_tag(const char* filename, char* p_title, char* p_artist, char* p_album, uint16_t tag_size)
00031 {
00032     FILE * fp;
00033     EasyDecoder * decoder;
00034     bool ret = false;
00035 
00036     decoder = create_decoer_class(filename);
00037     if (decoder == NULL) {
00038         return false;
00039     }
00040 
00041     fp = fopen(filename, "r");
00042     if (decoder->AnalyzeHeder(p_title, p_artist, p_album, tag_size, fp) != false) {
00043         ret = true;
00044     }
00045     delete decoder;
00046     fclose(fp);
00047 
00048     return ret;
00049 }
00050 
00051 bool EasyPlaybackPWM::play(const char* filename)
00052 {
00053     size_t audio_data_size = AUDIO_WRITE_BUFF_SIZE;
00054     FILE * fp;
00055     EasyDecoder * decoder;
00056     bool ret = false;
00057 
00058     decoder = create_decoer_class(filename);
00059     if (decoder == NULL) {
00060         return false;
00061     }
00062 
00063      _skip = false;
00064     fp = fopen(filename, "r");
00065     if (decoder->AnalyzeHeder(NULL, NULL, NULL, 0, fp) == false) {
00066         // do nothing
00067     } else if  ((decoder->GetChannel() != 2)
00068             || (audio.format(decoder->GetBlockSize()) == false)
00069             || (audio.frequency(decoder->GetSamplingRate()) == false)) {
00070         // do nothing
00071     } else {
00072         while (audio_data_size == AUDIO_WRITE_BUFF_SIZE) {
00073             while ((_pause) && (!_skip)) {
00074                 Thread::wait(100);
00075             }
00076             if (_skip) {
00077                 break;
00078             }
00079             audio_data_size = decoder->GetNextData(_audio_buf, AUDIO_WRITE_BUFF_SIZE);
00080             if (audio_data_size > 0) {
00081                 audio.write(_audio_buf, audio_data_size);
00082             }
00083         }
00084         Thread::wait(500);
00085         ret = true;
00086     }
00087     delete decoder;
00088     fclose(fp);
00089 
00090     return ret;
00091 }
00092 
00093 bool EasyPlaybackPWM::is_paused(void)
00094 {
00095     return _pause;
00096 }
00097 
00098 void EasyPlaybackPWM::pause()
00099 {
00100     _pause = !_pause;
00101 }
00102 
00103 void EasyPlaybackPWM::pause(bool type)
00104 {
00105     _pause = type;
00106 }
00107 
00108 void EasyPlaybackPWM::skip(void)
00109 {
00110     _skip = true;
00111 }
00112 
00113 bool EasyPlaybackPWM::outputVolume(float VolumeOut)
00114 {
00115     return audio.outputVolume(VolumeOut);
00116 }
00117 
00118 EasyDecoder * EasyPlaybackPWM::create_decoer_class(const char* filename)
00119 {
00120     std::map<std::string, EasyDecoder*(*)()>::iterator itr;
00121     char *extension = strstr((char*)filename, ".");
00122 
00123     if (extension == NULL) {
00124         return NULL;
00125     }
00126 
00127     itr = m_lpDecoders.find(extension);
00128     if (itr == m_lpDecoders.end()) {
00129         return NULL;
00130     }
00131 
00132     return (*itr).second();
00133 }
00134