FaceAccess/Record/Player.cpp
2024-09-04 20:17:15 +08:00

71 lines
2.4 KiB
C++
Raw Permalink 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);
}
}
void PlayerTask::setChannels(int channels) {
if (m_channels != channels) {
m_channels = channels;
}
}
/*
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
ffmpeg -i 20200316_1900.mp3 -ac 2 -ar 16000 -f s16le -acodec pcm_s16le 20200316_1900_2ch.pcm
ffmpeg -i 20200316_1900.mp3 -ac 1 -ar 16000 -f s16le -acodec pcm_s16le 20200316_1900_1ch.pcm
20200316_1900.mp3
./Record --play --channels=1 --path=/sdcard/data/20200316_1900_1ch.pcm
./Record --play --path=/sdcard/data/20200316_1900_2ch.pcm
./Record --play --path=/sdcard/data/20200316_1900_left_silence.pcm
./Record --play --path=/sdcard/data/20200316_1900_right_silense.pcm
20240904160913.pcm
*/
// ./Record --play --path=/sdcard/data/20240904160913.pcm
void PlayerTask::run() {
using namespace Amass;
RkAudio::Format format;
format.channels = m_channels;
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;
}
m_buffer.resize(m_channels*sizeof(int16_t)*16* format.period);
play();
}
void PlayerTask::play() {
using namespace Amass;
auto ioConext = Singleton<IoContext>::instance();
if (!m_ifs || !(*m_ifs)) {
LOG(info) << "play finished";
return;
}
m_ifs->read(m_buffer.data(), m_buffer.size());
auto readedSize = m_ifs->gcount();
m_output->write(reinterpret_cast<const uint8_t *>(m_buffer.data()), readedSize);
boost::asio::post(*ioConext->ioContext(), [this]() { play(); });
}