Easy playback library for PwmOutSpeaker.

Committer:
dkato
Date:
Thu Jul 06 04:42:29 2017 +0000
Revision:
0:41ab76b22961
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:41ab76b22961 1 /* mbed EasyPlaybackPWM Library
dkato 0:41ab76b22961 2 * Copyright (C) 2017 dkato
dkato 0:41ab76b22961 3 *
dkato 0:41ab76b22961 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:41ab76b22961 5 * you may not use this file except in compliance with the License.
dkato 0:41ab76b22961 6 * You may obtain a copy of the License at
dkato 0:41ab76b22961 7 *
dkato 0:41ab76b22961 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:41ab76b22961 9 *
dkato 0:41ab76b22961 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:41ab76b22961 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:41ab76b22961 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:41ab76b22961 13 * See the License for the specific language governing permissions and
dkato 0:41ab76b22961 14 * limitations under the License.
dkato 0:41ab76b22961 15 */
dkato 0:41ab76b22961 16
dkato 0:41ab76b22961 17 #include "mbed.h"
dkato 0:41ab76b22961 18 #include "EasyPlaybackPWM.h"
dkato 0:41ab76b22961 19
dkato 0:41ab76b22961 20 EasyPlaybackPWM::EasyPlaybackPWM(PinName pwm_l, PinName pwm_r) : audio(pwm_l, pwm_r), _skip(false), _pause(false)
dkato 0:41ab76b22961 21 {
dkato 0:41ab76b22961 22 _audio_buf = new uint8_t[AUDIO_WRITE_BUFF_SIZE];
dkato 0:41ab76b22961 23 }
dkato 0:41ab76b22961 24
dkato 0:41ab76b22961 25 EasyPlaybackPWM::~EasyPlaybackPWM()
dkato 0:41ab76b22961 26 {
dkato 0:41ab76b22961 27 delete [] _audio_buf;
dkato 0:41ab76b22961 28 }
dkato 0:41ab76b22961 29
dkato 0:41ab76b22961 30 bool EasyPlaybackPWM::get_tag(const char* filename, char* p_title, char* p_artist, char* p_album, uint16_t tag_size)
dkato 0:41ab76b22961 31 {
dkato 0:41ab76b22961 32 FILE * fp;
dkato 0:41ab76b22961 33 EasyDecoder * decoder;
dkato 0:41ab76b22961 34 bool ret = false;
dkato 0:41ab76b22961 35
dkato 0:41ab76b22961 36 decoder = create_decoer_class(filename);
dkato 0:41ab76b22961 37 if (decoder == NULL) {
dkato 0:41ab76b22961 38 return false;
dkato 0:41ab76b22961 39 }
dkato 0:41ab76b22961 40
dkato 0:41ab76b22961 41 fp = fopen(filename, "r");
dkato 0:41ab76b22961 42 if (decoder->AnalyzeHeder(p_title, p_artist, p_album, tag_size, fp) != false) {
dkato 0:41ab76b22961 43 ret = true;
dkato 0:41ab76b22961 44 }
dkato 0:41ab76b22961 45 delete decoder;
dkato 0:41ab76b22961 46 fclose(fp);
dkato 0:41ab76b22961 47
dkato 0:41ab76b22961 48 return ret;
dkato 0:41ab76b22961 49 }
dkato 0:41ab76b22961 50
dkato 0:41ab76b22961 51 bool EasyPlaybackPWM::play(const char* filename)
dkato 0:41ab76b22961 52 {
dkato 0:41ab76b22961 53 size_t audio_data_size = AUDIO_WRITE_BUFF_SIZE;
dkato 0:41ab76b22961 54 FILE * fp;
dkato 0:41ab76b22961 55 EasyDecoder * decoder;
dkato 0:41ab76b22961 56 bool ret = false;
dkato 0:41ab76b22961 57
dkato 0:41ab76b22961 58 decoder = create_decoer_class(filename);
dkato 0:41ab76b22961 59 if (decoder == NULL) {
dkato 0:41ab76b22961 60 return false;
dkato 0:41ab76b22961 61 }
dkato 0:41ab76b22961 62
dkato 0:41ab76b22961 63 _skip = false;
dkato 0:41ab76b22961 64 fp = fopen(filename, "r");
dkato 0:41ab76b22961 65 if (decoder->AnalyzeHeder(NULL, NULL, NULL, 0, fp) == false) {
dkato 0:41ab76b22961 66 // do nothing
dkato 0:41ab76b22961 67 } else if ((decoder->GetChannel() != 2)
dkato 0:41ab76b22961 68 || (audio.format(decoder->GetBlockSize()) == false)
dkato 0:41ab76b22961 69 || (audio.frequency(decoder->GetSamplingRate()) == false)) {
dkato 0:41ab76b22961 70 // do nothing
dkato 0:41ab76b22961 71 } else {
dkato 0:41ab76b22961 72 while (audio_data_size == AUDIO_WRITE_BUFF_SIZE) {
dkato 0:41ab76b22961 73 while ((_pause) && (!_skip)) {
dkato 0:41ab76b22961 74 Thread::wait(100);
dkato 0:41ab76b22961 75 }
dkato 0:41ab76b22961 76 if (_skip) {
dkato 0:41ab76b22961 77 break;
dkato 0:41ab76b22961 78 }
dkato 0:41ab76b22961 79 audio_data_size = decoder->GetNextData(_audio_buf, AUDIO_WRITE_BUFF_SIZE);
dkato 0:41ab76b22961 80 if (audio_data_size > 0) {
dkato 0:41ab76b22961 81 audio.write(_audio_buf, audio_data_size);
dkato 0:41ab76b22961 82 }
dkato 0:41ab76b22961 83 }
dkato 0:41ab76b22961 84 Thread::wait(500);
dkato 0:41ab76b22961 85 ret = true;
dkato 0:41ab76b22961 86 }
dkato 0:41ab76b22961 87 delete decoder;
dkato 0:41ab76b22961 88 fclose(fp);
dkato 0:41ab76b22961 89
dkato 0:41ab76b22961 90 return ret;
dkato 0:41ab76b22961 91 }
dkato 0:41ab76b22961 92
dkato 0:41ab76b22961 93 bool EasyPlaybackPWM::is_paused(void)
dkato 0:41ab76b22961 94 {
dkato 0:41ab76b22961 95 return _pause;
dkato 0:41ab76b22961 96 }
dkato 0:41ab76b22961 97
dkato 0:41ab76b22961 98 void EasyPlaybackPWM::pause()
dkato 0:41ab76b22961 99 {
dkato 0:41ab76b22961 100 _pause = !_pause;
dkato 0:41ab76b22961 101 }
dkato 0:41ab76b22961 102
dkato 0:41ab76b22961 103 void EasyPlaybackPWM::pause(bool type)
dkato 0:41ab76b22961 104 {
dkato 0:41ab76b22961 105 _pause = type;
dkato 0:41ab76b22961 106 }
dkato 0:41ab76b22961 107
dkato 0:41ab76b22961 108 void EasyPlaybackPWM::skip(void)
dkato 0:41ab76b22961 109 {
dkato 0:41ab76b22961 110 _skip = true;
dkato 0:41ab76b22961 111 }
dkato 0:41ab76b22961 112
dkato 0:41ab76b22961 113 bool EasyPlaybackPWM::outputVolume(float VolumeOut)
dkato 0:41ab76b22961 114 {
dkato 0:41ab76b22961 115 return audio.outputVolume(VolumeOut);
dkato 0:41ab76b22961 116 }
dkato 0:41ab76b22961 117
dkato 0:41ab76b22961 118 EasyDecoder * EasyPlaybackPWM::create_decoer_class(const char* filename)
dkato 0:41ab76b22961 119 {
dkato 0:41ab76b22961 120 std::map<std::string, EasyDecoder*(*)()>::iterator itr;
dkato 0:41ab76b22961 121 char *extension = strstr((char*)filename, ".");
dkato 0:41ab76b22961 122
dkato 0:41ab76b22961 123 if (extension == NULL) {
dkato 0:41ab76b22961 124 return NULL;
dkato 0:41ab76b22961 125 }
dkato 0:41ab76b22961 126
dkato 0:41ab76b22961 127 itr = m_lpDecoders.find(extension);
dkato 0:41ab76b22961 128 if (itr == m_lpDecoders.end()) {
dkato 0:41ab76b22961 129 return NULL;
dkato 0:41ab76b22961 130 }
dkato 0:41ab76b22961 131
dkato 0:41ab76b22961 132 return (*itr).second();
dkato 0:41ab76b22961 133 }
dkato 0:41ab76b22961 134