test public

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SoundlessSpeaker.cpp Source File

SoundlessSpeaker.cpp

00001 /* mbed SoundlessSpeaker Library
00002  * Copyright (C) 2016 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 "SoundlessSpeaker.h"
00018 
00019 SoundlessSpeaker::SoundlessSpeaker() {
00020     _length = 16;
00021     _hz = 44100;
00022     _byte_per_sec = (_hz * (_length / 8) * 2);
00023     _t.reset();
00024     _next_time = 0;
00025 }
00026 
00027 bool SoundlessSpeaker::format(char length) {
00028     switch (length) {
00029         case 8:
00030         case 16:
00031             break;
00032         default:
00033             return false;
00034     }
00035     _length = length;
00036     _byte_per_sec = (_hz * (_length / 8) * 2);
00037     return true;
00038 }
00039 
00040 bool SoundlessSpeaker::frequency(int hz) {
00041     switch (hz) {
00042         case 48000:
00043         case 44100:
00044         case 32000:
00045         case 8021:
00046         case 8000:
00047             break;
00048         default:
00049             return false;
00050     }
00051     _hz = hz;
00052     _byte_per_sec = (_hz * (_length / 8) * 2);
00053     return true;
00054 }
00055 
00056 int SoundlessSpeaker::write(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf) {
00057     int wk_next_time = _next_time;
00058     int wk_time = _t.read_ms();
00059     int wk_over_time;
00060 
00061     if (wk_time < wk_next_time) {
00062         ThisThread::sleep_for(wk_next_time - wk_time);
00063     }
00064     _next_time = (data_size * 1000) / _byte_per_sec;
00065     wk_over_time = _t.read_ms() - wk_next_time;
00066     if (wk_over_time > 0) {
00067         if (wk_over_time > _next_time) {
00068             _next_time = 0;
00069         } else {
00070             _next_time -= wk_over_time;
00071         }
00072     }
00073     _t.reset();
00074     _t.start();
00075 
00076     if (p_data_conf != NULL) {
00077         return data_size;
00078     }
00079     if (p_data_conf->p_notify_func != NULL) {
00080         p_data_conf->p_notify_func(p_data, data_size, p_data_conf->p_app_data);
00081     }
00082     return 0;
00083 }
00084