- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 C++ (Ubuntu) 解码音频文件(wav、mp3、aiff)并将其存储(PCM/int)在 vector/数组中?
到目前为止我做了什么:我使用 gstreamer(我是一个非常初学者)来解码文件,我可以播放它并使用 pull-buffer 获取数据,但是我没有找到获取整个音频的方法要存储在数组中的数据。
gstreamer中有这样的方法吗?或者是否存在其他 C++ 库来解码音频文件并获取原始 (PCM/int) 数据?
编辑:将频率更改为 PCM
最佳答案
我自己用 gstreamer
解决了这个问题。诀窍是使用 giostreamsink
作为接收器,这会将数据存储到 G_MEMORY_OUTPUT_STREAM
中。
完整代码示例:
#include <string>
#include <stdio.h>
#include <gst/gst.h>
#include <gio/gio.h>
#include <boost/thread.hpp>
static void on_pad_added(GstElement *decodebin,
GstPad *pad,
gpointer data) {
GstElement *convert = (GstElement *) data;
GstCaps *caps;
GstStructure *str;
GstPad *audiopad;
audiopad = gst_element_get_static_pad(convert, "sink");
if (GST_PAD_IS_LINKED(audiopad)) {
g_object_unref(audiopad);
return;
}
caps = gst_pad_get_caps(pad);
str = gst_caps_get_structure(caps, 0);
printf("here %s\n",gst_structure_get_name(str));
if (!g_strrstr(gst_structure_get_name(str), "audio")) {
gst_caps_unref(caps);
gst_object_unref(audiopad);
return;
}
gst_caps_unref(caps);
gst_pad_link(pad, audiopad);
g_object_unref(audiopad);
}
static gboolean bus_call(GstBus *bus,
GstMessage *msg,
gpointer data) {
GMainLoop *loop = (GMainLoop*)data;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit(loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error(msg, &error, &debug);
g_free (debug);
g_printerr("Error: %s\n", error->message);
g_error_free(error);
g_main_loop_quit(loop);
break;
}
default:
break;
}
return true;
}
int main (int argc, char **argv) {
gst_init(&argc, &argv);
GstElement *pipeline, *source, *decode, *sink, *convert;
int rate = 44100;
int channels = 1;
int depth = 16;
bool output_signed = true;
GMainLoop *loop;
GstBus *bus;
guint bus_watch_id;
GMemoryOutputStream *stream;
gpointer out_data;
// loop
loop = g_main_loop_new(NULL, false);
// pipeline
pipeline = gst_pipeline_new("test_pipeline");
// sink
stream = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new(NULL, 0, (GReallocFunc)g_realloc, (GDestroyNotify)g_free));
sink = gst_element_factory_make ("giostreamsink", "sink");
g_object_set(G_OBJECT(sink), "stream", stream, NULL);
// source
source = gst_element_factory_make("filesrc", "source");
g_object_set(G_OBJECT(source), "location", "/home/sam/Desktop/audio/audio.wav", NULL);
// convert
convert = gst_element_factory_make("audioconvert", "convert");
// decode
decode = gst_element_factory_make("decodebin", "decoder");
// link decode to convert
g_signal_connect(decode, "pad-added", G_CALLBACK(on_pad_added), convert);
// bus
bus = gst_pipeline_get_bus(GST_PIPELINE (pipeline));
bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
gst_object_unref(bus);
// add elements into pipeline
gst_bin_add_many(GST_BIN(pipeline), source, decode, convert, sink, NULL);
// link source to decode
gst_element_link(source, decode);
// caps
GstCaps *caps;
caps = gst_caps_new_simple("audio/x-raw-int",
"rate", G_TYPE_INT, rate,
"channels", G_TYPE_INT, channels,
"width", G_TYPE_INT, depth,
"depth", G_TYPE_INT, depth,
"signed", G_TYPE_BOOLEAN, output_signed,
NULL);
// link convert to sink
gst_element_link_filtered(convert, sink, caps);
gst_caps_unref(caps);
// start playing
gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
// iterate
g_print("Running...\n");
g_main_loop_run(loop);
// out of the main loop, clean up nicely
g_print("Returned, stopping playback\n");
gst_element_set_state(pipeline, GST_STATE_NULL);
g_print("Deleting pipeline\n");
gst_object_unref(GST_OBJECT(pipeline));
g_source_remove (bus_watch_id);
g_main_loop_unref(loop);
// get data
g_print("get data\n");
out_data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM(stream));
unsigned long size = g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(stream));
unsigned long sizeData = g_memory_output_stream_get_data_size(G_MEMORY_OUTPUT_STREAM(stream));
std::cout << "stream size: " << size << std::endl;
std::cout << "stream data size: " << sizeData << std::endl;
// access data and store in vector
std::vector<int16_t> data;
for (unsigned long i = 0; i < sizeData/2; ++i) {
data.push_back(((gint16*)out_data)[i]);
}
return 0;
}
关于C++ (Ubuntu) : load audio file (wav, mp3, aiff) 到数组/vector 与 gstreamer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35288919/
我有一个小型WordPress网站。我做了很多音频工作,并且试图在WordPress的博客条目中发布HTML5音频剪辑。由于某种原因,它不起作用。它可能与我在WordPress网站上使用的样式有关,但
我在让 html5 标签与 Web Audio API .createMediaElementSource() 方法配合使用时遇到问题。请参阅下面的 jsFiddle/代码。任何想法这里出了什么问题将
我尝试安装ffmpeg $ brew install ffmpeg 并运行 ffmpeg $ ffmpeg -i audio.m4a -ar 8000 -ab 12.2k audio.amr 我收到以
我已使用Web Audio API中的getByteFrequencyData方法使用了来自Analyzer节点的FFT数据来创建频谱可视化器,如下所示: 在这种情况下,我有256个数据箱。这个数字到
Google VR刚刚为wwise制作了一个VR插件: https://developers.google.com/vr/audio/wwise-getting-started https://git
如何将新记录追加到现有记录中的选定位置或特定位置? 例如,有一个5秒的录制,我想再次录制,但是将此录制追加到先前录制的特定位置,说出来:在3秒钟的录制长度之后追加此录制。 最佳答案 您可以使用getC
我们基于 Raspberry Pi 和 omxplayer 构建简单的网络控制视频播放器。我们遇到的问题是任何使用 ffmpeg 转码的文件都有音频不同步。在 iPad 上制作并直接上传到 Pi 的视
我正在尝试了解Web Audio API的引入对基于Web的游戏的开发意味着什么。 Flash游戏当然可以执行一些相当高级的音频处理,对于简单的游戏,音频元素也许就足够了。但是Web Audio AP
我已经在如何用简单的音频引擎循环播放声音效果方面进行了广泛的搜索,但是在cocos2d论坛上除了hello with looping sfx之外,它并没有取得太大进展,因为它存在多个问题。如何在Sim
我的任务是打开一个扩展名为 mka 的现有音频文件(Matroska 容器)并提取原始音频数据。 This示例仅显示了从 mp2 文件中提取原始数据的示例。我不知道如何使用 mka 容器执行此操作。我
我是Lync 2013 SDK的新手(现在已经使用了几周),并且能够弄清除此以外的大部分东西…… 当我加入 session 时(使用ConversationManager.JoinConference
我好奇。如何实现有史以来最简单的音频引擎?我有一些类似使用默认音频设备的音频数据流的想法。玩了很多 RtAudio,我认为如果可以放弃一些功能,这是可能的。有人知道从哪里开始吗? 最佳答案 我会这样做
我一直在玩网络音频API。 我正在使用getByteFrequencyData来显示频带的分贝数据,但是我想更改显示频带的整个范围,因为现在重要的音频都被压缩为一对频带。 有关如何执行此操作的任何想法
我想在音频 session 以NAudio开始和结束时接收回调。以下代码正在运行: private void SetupMediaSessionCallbacks() {
我可以用trackPosition,offset以某种方式记录并输出到WAV。当在浏览器中播放时,它工作正常,我只想输出到WAV文件。 for (var i = 0; i 0) {
在哪种情况下,我们可以不将Google Resonance Audio SDK与耳机配合使用,而应将其与真实的扬声器配合使用(例如,安装在360°的音圈设置中)? 还是所有算法都不适用于真实的扬声器输
AudioPannerNode是一个处理节点,用于在三维空间中定位/空间化传入的音频流。有没有一种方法可以将其用于常规LR平移,请记住它使用3D笛卡尔坐标系与侦听器结合使用,该侦听器的位置和方向与平移
我有一个带有两个源的音频对象,分别为M4A和OGG格式。 代码如下: 然后,我可以调用document.getElementById('audio1')。play()并开始播放。 它适用于所有
我正在尝试构建一个允许将时间/节奏(可能是音高)输入到 Web 音频振荡器节点的界面。实际上创建了一个“步进音序器”。 为 Web Audio API 振荡器节点触发预定 NoteOn 的最佳方式是什
是否可以使用 Core Audio 以亚毫秒级延迟播放声音? 我尝试过使用具有不同大小和缓冲区数量的 AudioQueues,也尝试过使用 AudioUnits,但我一直无法将延迟降低到 30 毫秒以
我是一名优秀的程序员,十分优秀!