FaceAccess/Record/Player.cpp

71 lines
2.4 KiB
C++
Raw Normal View History

2024-09-04 17:57:23 +08:00
#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);
}
}
2024-09-04 20:17:15 +08:00
void PlayerTask::setChannels(int channels) {
if (m_channels != channels) {
m_channels = channels;
}
}
2024-09-04 17:57:23 +08:00
/*
ffmpeg将一个1通道mp3文件 2,16bit,16000pcm文件,
2024-09-04 20:17:15 +08:00
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
2024-09-04 17:57:23 +08:00
ffmpeg将一个1通道mp3文件 2,16bit,16000pcm文件,
2024-09-04 20:17:15 +08:00
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
2024-09-04 17:57:23 +08:00
2024-09-04 20:17:15 +08:00
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
2024-09-04 17:57:23 +08:00
2024-09-04 20:17:15 +08:00
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
2024-09-04 17:57:23 +08:00
*/
// ./Record --play --path=/sdcard/data/20240904160913.pcm
void PlayerTask::run() {
using namespace Amass;
RkAudio::Format format;
2024-09-04 20:17:15 +08:00
format.channels = m_channels;
2024-09-04 17:57:23 +08:00
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;
}
2024-09-04 20:17:15 +08:00
m_buffer.resize(m_channels*sizeof(int16_t)*16* format.period);
2024-09-04 17:57:23 +08:00
play();
}
void PlayerTask::play() {
using namespace Amass;
auto ioConext = Singleton<IoContext>::instance();
if (!m_ifs || !(*m_ifs)) {
LOG(info) << "play finished";
return;
}
2024-09-04 20:17:15 +08:00
m_ifs->read(m_buffer.data(), m_buffer.size());
2024-09-04 17:57:23 +08:00
auto readedSize = m_ifs->gcount();
2024-09-04 20:17:15 +08:00
m_output->write(reinterpret_cast<const uint8_t *>(m_buffer.data()), readedSize);
2024-09-04 17:57:23 +08:00
boost::asio::post(*ioConext->ioContext(), [this]() { play(); });
}