Easy playback library for AUDIO_GRBoard.
Dependents: GR-PEACH_Audio_WAV_PwmOut GR-Boards_Audio_WAV
EasyPlayback.cpp@0:9956e2ed09da, 2017-10-30 (annotated)
- Committer:
- dkato
- Date:
- Mon Oct 30 07:04:30 2017 +0000
- Revision:
- 0:9956e2ed09da
- Child:
- 1:fdd79b99ba73
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:9956e2ed09da | 1 | /* mbed EasyPlayback Library |
dkato | 0:9956e2ed09da | 2 | * Copyright (C) 2017 dkato |
dkato | 0:9956e2ed09da | 3 | * |
dkato | 0:9956e2ed09da | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:9956e2ed09da | 5 | * you may not use this file except in compliance with the License. |
dkato | 0:9956e2ed09da | 6 | * You may obtain a copy of the License at |
dkato | 0:9956e2ed09da | 7 | * |
dkato | 0:9956e2ed09da | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:9956e2ed09da | 9 | * |
dkato | 0:9956e2ed09da | 10 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:9956e2ed09da | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:9956e2ed09da | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:9956e2ed09da | 13 | * See the License for the specific language governing permissions and |
dkato | 0:9956e2ed09da | 14 | * limitations under the License. |
dkato | 0:9956e2ed09da | 15 | */ |
dkato | 0:9956e2ed09da | 16 | |
dkato | 0:9956e2ed09da | 17 | #include "mbed.h" |
dkato | 0:9956e2ed09da | 18 | #include "dcache-control.h" |
dkato | 0:9956e2ed09da | 19 | #include "EasyPlayback.h" |
dkato | 0:9956e2ed09da | 20 | |
dkato | 0:9956e2ed09da | 21 | EasyPlayback::EasyPlayback() : audio(0x80, (AUDIO_WRITE_BUFF_NUM - 1), 0), _buff_index(0), |
dkato | 0:9956e2ed09da | 22 | _skip(false), _pause(false), _init_end(false) |
dkato | 0:9956e2ed09da | 23 | { |
dkato | 0:9956e2ed09da | 24 | _heap_buf = new uint8_t[AUDIO_WRITE_BUFF_SIZE * AUDIO_WRITE_BUFF_NUM + 31]; |
dkato | 0:9956e2ed09da | 25 | _audio_buf = (uint8_t (*)[AUDIO_WRITE_BUFF_SIZE])(((uint32_t)_heap_buf + 31ul) & ~31ul); |
dkato | 0:9956e2ed09da | 26 | } |
dkato | 0:9956e2ed09da | 27 | |
dkato | 0:9956e2ed09da | 28 | EasyPlayback::~EasyPlayback() |
dkato | 0:9956e2ed09da | 29 | { |
dkato | 0:9956e2ed09da | 30 | delete [] _heap_buf; |
dkato | 0:9956e2ed09da | 31 | } |
dkato | 0:9956e2ed09da | 32 | |
dkato | 0:9956e2ed09da | 33 | bool EasyPlayback::get_tag(const char* filename, char* p_title, char* p_artist, char* p_album, uint16_t tag_size) |
dkato | 0:9956e2ed09da | 34 | { |
dkato | 0:9956e2ed09da | 35 | FILE * fp; |
dkato | 0:9956e2ed09da | 36 | EasyDecoder * decoder; |
dkato | 0:9956e2ed09da | 37 | bool ret = false; |
dkato | 0:9956e2ed09da | 38 | |
dkato | 0:9956e2ed09da | 39 | decoder = create_decoer_class(filename); |
dkato | 0:9956e2ed09da | 40 | if (decoder == NULL) { |
dkato | 0:9956e2ed09da | 41 | return false; |
dkato | 0:9956e2ed09da | 42 | } |
dkato | 0:9956e2ed09da | 43 | |
dkato | 0:9956e2ed09da | 44 | fp = fopen(filename, "r"); |
dkato | 0:9956e2ed09da | 45 | if (decoder->AnalyzeHeder(p_title, p_artist, p_album, tag_size, fp) != false) { |
dkato | 0:9956e2ed09da | 46 | ret = true; |
dkato | 0:9956e2ed09da | 47 | } |
dkato | 0:9956e2ed09da | 48 | delete decoder; |
dkato | 0:9956e2ed09da | 49 | fclose(fp); |
dkato | 0:9956e2ed09da | 50 | |
dkato | 0:9956e2ed09da | 51 | return ret; |
dkato | 0:9956e2ed09da | 52 | } |
dkato | 0:9956e2ed09da | 53 | |
dkato | 0:9956e2ed09da | 54 | bool EasyPlayback::play(const char* filename) |
dkato | 0:9956e2ed09da | 55 | { |
dkato | 0:9956e2ed09da | 56 | const rbsp_data_conf_t audio_write_async_ctl = {NULL, NULL}; |
dkato | 0:9956e2ed09da | 57 | size_t audio_data_size = AUDIO_WRITE_BUFF_SIZE; |
dkato | 0:9956e2ed09da | 58 | FILE * fp; |
dkato | 0:9956e2ed09da | 59 | uint8_t * p_buf; |
dkato | 0:9956e2ed09da | 60 | EasyDecoder * decoder; |
dkato | 0:9956e2ed09da | 61 | bool ret = false; |
dkato | 0:9956e2ed09da | 62 | |
dkato | 0:9956e2ed09da | 63 | decoder = create_decoer_class(filename); |
dkato | 0:9956e2ed09da | 64 | if (decoder == NULL) { |
dkato | 0:9956e2ed09da | 65 | return false; |
dkato | 0:9956e2ed09da | 66 | } |
dkato | 0:9956e2ed09da | 67 | |
dkato | 0:9956e2ed09da | 68 | if (!_init_end) { |
dkato | 0:9956e2ed09da | 69 | audio.power(); |
dkato | 0:9956e2ed09da | 70 | audio.outputVolume(1.0, 1.0); |
dkato | 0:9956e2ed09da | 71 | _init_end = true; |
dkato | 0:9956e2ed09da | 72 | } |
dkato | 0:9956e2ed09da | 73 | |
dkato | 0:9956e2ed09da | 74 | _skip = false; |
dkato | 0:9956e2ed09da | 75 | fp = fopen(filename, "r"); |
dkato | 0:9956e2ed09da | 76 | if (decoder->AnalyzeHeder(NULL, NULL, NULL, 0, fp) == false) { |
dkato | 0:9956e2ed09da | 77 | // do nothing |
dkato | 0:9956e2ed09da | 78 | } else if ((decoder->GetChannel() != 2) |
dkato | 0:9956e2ed09da | 79 | || (audio.format(decoder->GetBlockSize()) == false) |
dkato | 0:9956e2ed09da | 80 | || (audio.frequency(decoder->GetSamplingRate()) == false)) { |
dkato | 0:9956e2ed09da | 81 | // do nothing |
dkato | 0:9956e2ed09da | 82 | } else { |
dkato | 0:9956e2ed09da | 83 | while (audio_data_size == AUDIO_WRITE_BUFF_SIZE) { |
dkato | 0:9956e2ed09da | 84 | while ((_pause) && (!_skip)) { |
dkato | 0:9956e2ed09da | 85 | Thread::wait(100); |
dkato | 0:9956e2ed09da | 86 | } |
dkato | 0:9956e2ed09da | 87 | if (_skip) { |
dkato | 0:9956e2ed09da | 88 | break; |
dkato | 0:9956e2ed09da | 89 | } |
dkato | 0:9956e2ed09da | 90 | p_buf = _audio_buf[_buff_index]; |
dkato | 0:9956e2ed09da | 91 | audio_data_size = decoder->GetNextData(p_buf, AUDIO_WRITE_BUFF_SIZE); |
dkato | 0:9956e2ed09da | 92 | if (audio_data_size > 0) { |
dkato | 0:9956e2ed09da | 93 | dcache_clean(p_buf, audio_data_size); |
dkato | 0:9956e2ed09da | 94 | audio.write(p_buf, audio_data_size, &audio_write_async_ctl); |
dkato | 0:9956e2ed09da | 95 | _buff_index = (_buff_index + 1) & AUDIO_MSK_RING_BUFF; |
dkato | 0:9956e2ed09da | 96 | } |
dkato | 0:9956e2ed09da | 97 | } |
dkato | 0:9956e2ed09da | 98 | Thread::wait(500); |
dkato | 0:9956e2ed09da | 99 | ret = true; |
dkato | 0:9956e2ed09da | 100 | } |
dkato | 0:9956e2ed09da | 101 | delete decoder; |
dkato | 0:9956e2ed09da | 102 | fclose(fp); |
dkato | 0:9956e2ed09da | 103 | |
dkato | 0:9956e2ed09da | 104 | return ret; |
dkato | 0:9956e2ed09da | 105 | } |
dkato | 0:9956e2ed09da | 106 | |
dkato | 0:9956e2ed09da | 107 | bool EasyPlayback::is_paused(void) |
dkato | 0:9956e2ed09da | 108 | { |
dkato | 0:9956e2ed09da | 109 | return _pause; |
dkato | 0:9956e2ed09da | 110 | } |
dkato | 0:9956e2ed09da | 111 | |
dkato | 0:9956e2ed09da | 112 | void EasyPlayback::pause() |
dkato | 0:9956e2ed09da | 113 | { |
dkato | 0:9956e2ed09da | 114 | _pause = !_pause; |
dkato | 0:9956e2ed09da | 115 | } |
dkato | 0:9956e2ed09da | 116 | |
dkato | 0:9956e2ed09da | 117 | void EasyPlayback::pause(bool type) |
dkato | 0:9956e2ed09da | 118 | { |
dkato | 0:9956e2ed09da | 119 | _pause = type; |
dkato | 0:9956e2ed09da | 120 | } |
dkato | 0:9956e2ed09da | 121 | |
dkato | 0:9956e2ed09da | 122 | void EasyPlayback::skip(void) |
dkato | 0:9956e2ed09da | 123 | { |
dkato | 0:9956e2ed09da | 124 | _skip = true; |
dkato | 0:9956e2ed09da | 125 | } |
dkato | 0:9956e2ed09da | 126 | |
dkato | 0:9956e2ed09da | 127 | bool EasyPlayback::outputVolume(float VolumeOut) |
dkato | 0:9956e2ed09da | 128 | { |
dkato | 0:9956e2ed09da | 129 | if (!_init_end) { |
dkato | 0:9956e2ed09da | 130 | audio.power(); |
dkato | 0:9956e2ed09da | 131 | _init_end = true; |
dkato | 0:9956e2ed09da | 132 | } |
dkato | 0:9956e2ed09da | 133 | return audio.outputVolume(VolumeOut, VolumeOut); |
dkato | 0:9956e2ed09da | 134 | } |
dkato | 0:9956e2ed09da | 135 | |
dkato | 0:9956e2ed09da | 136 | EasyDecoder * EasyPlayback::create_decoer_class(const char* filename) |
dkato | 0:9956e2ed09da | 137 | { |
dkato | 0:9956e2ed09da | 138 | std::map<std::string, EasyDecoder*(*)()>::iterator itr; |
dkato | 0:9956e2ed09da | 139 | char *extension = strstr((char *)filename, "."); |
dkato | 0:9956e2ed09da | 140 | |
dkato | 0:9956e2ed09da | 141 | if (extension == NULL) { |
dkato | 0:9956e2ed09da | 142 | return NULL; |
dkato | 0:9956e2ed09da | 143 | } |
dkato | 0:9956e2ed09da | 144 | |
dkato | 0:9956e2ed09da | 145 | itr = m_lpDecoders.find(extension); |
dkato | 0:9956e2ed09da | 146 | if (itr == m_lpDecoders.end()) { |
dkato | 0:9956e2ed09da | 147 | return NULL; |
dkato | 0:9956e2ed09da | 148 | } |
dkato | 0:9956e2ed09da | 149 | |
dkato | 0:9956e2ed09da | 150 | return (*itr).second(); |
dkato | 0:9956e2ed09da | 151 | } |
dkato | 0:9956e2ed09da | 152 |