FaceAccess/Record/Player.cpp
2024-09-04 17:57:23 +08:00

56 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "IoContext.h"
#include "main.h"
#include <boost/asio/post.hpp>
#include <filesystem>
void PlayerTask::setPath(const std::string &path) {
if (m_path != path) {
m_path = path;
}
if (std::filesystem::exists(path)) {
m_ifs = std::make_shared<std::ifstream>(path, std::ifstream::binary);
}
}
/*
ffmpeg将一个1通道mp3文件转换为 2通道,16bit,16000采样率的pcm文件,但是左通道数据为静音
ffmpeg -i 20200316_1900.mp3 -ac 2 -ar 16000 -filter_complex "[0:a]pan=stereo|c0=0*c0|c1=1*c0[a]" -map "[a]" -f s16le -acodec pcm_s16le 20200316_1900.pcm
ffmpeg将一个1通道mp3文件转换为 2通道,16bit,16000采样率的pcm文件,但是右通道数据为静音
ffmpeg -i 20200316_1900.mp3 -ac 2 -ar 16000 -filter_complex "[0:a]pan=stereo|c0=1*c0|c1=0*c0[a]" -map "[a]" -f s16le -acodec pcm_s16le 20200316_1900.pcm
*/
// ./Record --play --path=/sdcard/data/20240904160913.pcm
void PlayerTask::run() {
using namespace Amass;
RkAudio::Format format;
format.channels = 2;
format.period = 64;
m_output = std::make_shared<RkAudio::Output>();
if (!m_output->open(sizeof(uint16_t), format.sampleRate, format.channels, format.period, false)) {
LOG(error) << "audio output open failed.";
return;
}
play();
}
void PlayerTask::play() {
using namespace Amass;
auto ioConext = Singleton<IoContext>::instance();
if (!m_ifs || !(*m_ifs)) {
LOG(info) << "play finished";
return;
}
char buffer[2 * sizeof(int16_t) * 16 * 64];
m_ifs->read(buffer, sizeof(buffer));
auto readedSize = m_ifs->gcount();
m_output->write(reinterpret_cast<const uint8_t *>(buffer), readedSize);
boost::asio::post(*ioConext->ioContext(), [this]() { play(); });
}