- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试将 Opus 文件解码回原始 48 kHz。但是我找不到任何示例代码来做到这一点。
我当前的代码是这样的:
void COpusCodec::Decode(unsigned char* encoded, short* decoded, unsigned int len)
{
int max_size=960*6;//not sure about this one
int error;
dec = opus_decoder_create(48000, 1, &error);//decode to 48kHz mono
int frame_size=opus_decode(dec, encoded, len, decoded, max_size, 0);
}
“编码”参数可能是大量数据,所以我认为我必须将其拆分为帧。我不确定我该怎么做。
作为 Opus 的初学者,我真的很害怕把事情搞砸。
有人可以帮忙吗?
最佳答案
我认为 opus_demo.c
来自 source tarball 的程序有你想要的。
这很复杂,因为所有不相关的代码都与
事实证明,删除所有这些位是一项非常乏味的工作。但是一旦你这样做了,你就会得到非常干净、易于理解的代码,见下文。
注意我
主要是因为它似乎不会使代码复杂化,而且您可能会对它感兴趣。
我用两种方式测试了这个程序:
opus_demo
编码的单声道 wav 是否已使用此剥离解码器正确解码)。测试 wav 约为 23Mb,压缩后为 2.9Mb。./opus_demo -d 48000 1 <opus-file> <pcm-file>
调用时,回归测试与原版 opus_demo 一起进行.结果文件具有相同的 md5sum
校验和作为此处使用剥离解码器解码的校验和。重大更新我对代码进行了 C++ 化。这应该可以让您使用 iostreams。
fin.readsome
上的循环现在;此循环可设为“异步”(即,可使其返回并在新数据到达时继续读取(在您的 Decode
函数的下一次调用时?)[1] vector
、unique_ptr
)。OpusErrorException
来自 std::exception
的类用于从 libopus
传播错误在此处查看所有代码 + Makefile:https://github.com/sehe/opus/tree/master/contrib
[1] for true async IO (e.g. network or serial communinication) consider using Boost Asio, see e.g. http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/overview/networking/iostreams.html
// (c) Seth Heeren 2013
//
// Based on src/opus_demo.c in opus-1.0.2
// License see http://www.opus-codec.org/license/
#include <stdexcept>
#include <memory>
#include <iosfwd>
struct OpusErrorException : public virtual std::exception
{
OpusErrorException(int code) : code(code) {}
const char* what() const noexcept;
private:
const int code;
};
struct COpusCodec
{
COpusCodec(int32_t sampling_rate, int channels);
~COpusCodec();
bool decode_frame(std::istream& fin, std::ostream& fout);
private:
struct Impl;
std::unique_ptr<Impl> _pimpl;
};
// (c) Seth Heeren 2013
//
// Based on src/opus_demo.c in opus-1.0.2
// License see http://www.opus-codec.org/license/
#include "COpusCodec.hpp"
#include <vector>
#include <iomanip>
#include <memory>
#include <sstream>
#include "opus.h"
#define MAX_PACKET 1500
const char* OpusErrorException::what() const noexcept
{
return opus_strerror(code);
}
// I'd suggest reading with boost::spirit::big_dword or similar
static uint32_t char_to_int(char ch[4])
{
return static_cast<uint32_t>(static_cast<unsigned char>(ch[0])<<24) |
static_cast<uint32_t>(static_cast<unsigned char>(ch[1])<<16) |
static_cast<uint32_t>(static_cast<unsigned char>(ch[2])<< 8) |
static_cast<uint32_t>(static_cast<unsigned char>(ch[3])<< 0);
}
struct COpusCodec::Impl
{
Impl(int32_t sampling_rate = 48000, int channels = 1)
:
_channels(channels),
_decoder(nullptr, &opus_decoder_destroy),
_state(_max_frame_size, MAX_PACKET, channels)
{
int err = OPUS_OK;
auto raw = opus_decoder_create(sampling_rate, _channels, &err);
_decoder.reset(err == OPUS_OK? raw : throw OpusErrorException(err) );
}
bool decode_frame(std::istream& fin, std::ostream& fout)
{
char ch[4] = {0};
if (!fin.read(ch, 4) && fin.eof())
return false;
uint32_t len = char_to_int(ch);
if(len>_state.data.size())
throw std::runtime_error("Invalid payload length");
fin.read(ch, 4);
const uint32_t enc_final_range = char_to_int(ch);
const auto data = reinterpret_cast<char*>(&_state.data.front());
size_t read = 0ul;
for (auto append_position = data; fin && read<len; append_position += read)
{
read += fin.readsome(append_position, len-read);
}
if(read<len)
{
std::ostringstream oss;
oss << "Ran out of input, expecting " << len << " bytes got " << read << " at " << fin.tellg();
throw std::runtime_error(oss.str());
}
int output_samples;
const bool lost = (len==0);
if(lost)
{
opus_decoder_ctl(_decoder.get(), OPUS_GET_LAST_PACKET_DURATION(&output_samples));
}
else
{
output_samples = _max_frame_size;
}
output_samples = opus_decode(
_decoder.get(),
lost ? NULL : _state.data.data(),
len,
_state.out.data(),
output_samples,
0);
if(output_samples>0)
{
for(int i=0; i<(output_samples)*_channels; i++)
{
short s;
s=_state.out[i];
_state.fbytes[2*i] = s&0xFF;
_state.fbytes[2*i+1] = (s>>8)&0xFF;
}
if(!fout.write(reinterpret_cast<char*>(_state.fbytes.data()), sizeof(short)* _channels * output_samples))
throw std::runtime_error("Error writing");
}
else
{
throw OpusErrorException(output_samples); // negative return is error code
}
uint32_t dec_final_range;
opus_decoder_ctl(_decoder.get(), OPUS_GET_FINAL_RANGE(&dec_final_range));
/* compare final range encoder rng values of encoder and decoder */
if(enc_final_range!=0
&& !lost && !_state.lost_prev
&& dec_final_range != enc_final_range)
{
std::ostringstream oss;
oss << "Error: Range coder state mismatch between encoder and decoder in frame " << _state.frameno << ": " <<
"0x" << std::setw(8) << std::setfill('0') << std::hex << (unsigned long)enc_final_range <<
"0x" << std::setw(8) << std::setfill('0') << std::hex << (unsigned long)dec_final_range;
throw std::runtime_error(oss.str());
}
_state.lost_prev = lost;
_state.frameno++;
return true;
}
private:
const int _channels;
const int _max_frame_size = 960*6;
std::unique_ptr<OpusDecoder, void(*)(OpusDecoder*)> _decoder;
struct State
{
State(int max_frame_size, int max_payload_bytes, int channels) :
out (max_frame_size*channels),
fbytes(max_frame_size*channels*sizeof(decltype(out)::value_type)),
data (max_payload_bytes)
{ }
std::vector<short> out;
std::vector<unsigned char> fbytes, data;
int32_t frameno = 0;
bool lost_prev = true;
};
State _state;
};
COpusCodec::COpusCodec(int32_t sampling_rate, int channels)
: _pimpl(std::unique_ptr<Impl>(new Impl(sampling_rate, channels)))
{
//
}
COpusCodec::~COpusCodec()
{
// this instantiates the pimpl deletor code on the, now-complete, pimpl class
}
bool COpusCodec::decode_frame(
std::istream& fin,
std::ostream& fout)
{
return _pimpl->decode_frame(fin, fout);
}
// (c) Seth Heeren 2013
//
// Based on src/opus_demo.c in opus-1.0.2
// License see http://www.opus-codec.org/license/
#include <fstream>
#include <iostream>
#include "COpusCodec.hpp"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cerr << "Usage: " << argv[0] << " <input> <output>\n";
return 255;
}
std::basic_ifstream<char> fin (argv[1], std::ios::binary);
std::basic_ofstream<char> fout(argv[2], std::ios::binary);
if(!fin) throw std::runtime_error("Could not open input file");
if(!fout) throw std::runtime_error("Could not open output file");
try
{
COpusCodec codec(48000, 1);
size_t frames = 0;
while(codec.decode_frame(fin, fout))
{
frames++;
}
std::cout << "Successfully decoded " << frames << " frames\n";
}
catch(OpusErrorException const& e)
{
std::cerr << "OpusErrorException: " << e.what() << "\n";
return 255;
}
}
关于c++ - 解码 Opus 音频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16496288/
我一直在为实时流和静态文件(HTTP 上的 MP3)构建网络广播播放器。我选了Howler.js作为规范化 quirks 的后端的 HTML5 Audio (思考:自动播放、淡入/淡出、进度事件)。
vue移动端input上传视频、音频,供大家参考,具体内容如下 html部分 ?
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我想在我的程序中访问音频和视频。 MAC里面可以吗? 我们的程序在 Windows 上运行,我使用 directshow 进行音频/视频编程。但我想在 MAC 中开发相同的东西。 有没有像direct
我的应用程序(使用 Flutter 制作,但这应该无关紧要)具有类似于计时器的功能,可以定期(10 秒到 3 分钟)发出滴答声。我在我的 Info.plist 中激活了背景模式 Audio、AirPl
我是 ionic 2 的初学者 我使用了音频文件。 import { Component } from '@angular/core'; import {NavController, Alert
我有一个包含ListView和图片的数据库,我想在每个语音数据中包含它们。我已经尝试过,但是有很多错误。以下是我的java和xml。 数据库.java package com.example.data
我在zend framework 2上建立了一个音乐社交网络。您可以想象它与SoundCloud相同,用户上传歌曲,其他用户播放它们,这些是网站上的基本操作。 我知道将要托管该页面的服务器将需要大量带
我正在尝试在android应用中播放音频,但是在代码中AssetFileDescriptor asset1及其下一行存在错误。这是代码: MediaPlayer mp; @Override prote
我对 WordPress Audio Shortcode有问题。我这样使用它: 但是在前面,在HTML代码中我得到了: document.createElement('audio');
我正在做一项关于降低噪音的滤波技术的实验。我在数据集中的样本是音频文件(.wav),因此,我有:原始录制的音频文件,我将它们与噪声混合,因此变得混合(噪声信号),我将这些噪声信号通过滤波算法传递,输出
一个人会使用哪种类型的神经网络架构将声音映射到其他声音?神经网络擅长学习从序列到其他序列,因此声音增强/生成似乎是它们的一种非常流行的应用(但不幸的是,事实并非如此-我只能找到一个(相当古老的)洋红色
这个让我抓狂: 在专用于此声音播放/录制应用程序的 Vista+ 计算机上,我需要我的应用程序确保(默认)麦克风电平被推到最大。我该怎么做? 我找到了 Core Audio lib ,找到了如何将 I
{ "manifest_version": 2, "name": "Kitten Radio Extension", "description": "Listen while browsi
class Main { WaveFileReader reader; short[] sample; Complex[] tmpComplexArray; publi
我正在使用电话录音软件(android),该软件可以记录2个人在电话中的通话。每个电话的输出是一个音频文件,其中包含来自 call 者和被 call 者的声音。 但是,大多数情况下,运行此软件的电话发
我正在构建一个需要语音激活命令的Web应用程序。我正在使用getUserMedia作为音频输入。 对于语音激活命令,该过程是用户将需要通过记录其语音来“校准”命令。例如,对于“停止”命令,用户将说出“
我正在开发一个Cordova应用程序,并将PouchDB用作数据库,当连接可用时,它将所有信息复制到CouchDB。 我成功存储了简单的文本和图像。 我一直在尝试存储视频和音频,但是没有运气。 我存储
我正在开发web application,我必须在其中使用.MP3的地方使用播放声音,但是会发生问题。 声音为play good in chrome, Firefox,但为safari its not
如何减少音频文件的位深?是否忽略了MSB或LSB?两者混合吗? (旁问:这叫什么?) 最佳答案 TL / DR:将音频曲线高度变量右移至较低位深度 可以将音频视为幅度(Y轴)随时间(X轴)的模拟曲线。
我是一名优秀的程序员,十分优秀!