gpt4 book ai didi

c++ - mpg123 在 C++ 中将 mp3 解码为 pcm

转载 作者:行者123 更新时间:2023-11-28 01:55:30 25 4
gpt4 key购买 nike

我想将 mp3 文件解码为 pcm:

#include <iostream>
#include <mpg123.h>
#include <out123.h>
using namespace std;
int main()
{
mpg123_handle *mh;
unsigned char *buffer;
size_t buffer_size;
size_t done;
int err;
int channels, encoding;
long rate;
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));

mpg123_init();
mh = mpg123_new(NULL, &err);

mpg123_open(mh, "/home/abbas/Desktop/nastaran.mp3");
// mpg123_getformat(mh, &rate, &channels, &encoding);



while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
cout << buffer ;

free(buffer);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
return 0;
}

但它给了我这个错误:

The program has unexpectedly finished.

这个错误没有说明原因。问题出在哪里?

与操作系统有关吗?

cmake 文件:

project(echoprint2)
cmake_minimum_required(VERSION 2.8)

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

add_library(mpg123 SHARED IMPORTED )
set_target_properties(mpg123 PROPERTIES IMPORTED_LOCATION /usr/local /lib/libmpg123.so)
TARGET_LINK_LIBRARIES(echoprint2 mpg123)

最佳答案

我修复了一些错误并生成了一些数值。我留给你检查产生的波形是否正确。建议使用 Excel 和 Audacity 对其进行可视化并确认波形看起来没问题。

#include <fstream>
#include <iostream>
#include <mpg123.h>
#include <out123.h>

int main(){
mpg123_init();

int err;
mpg123_handle *mh = mpg123_new(NULL, &err);
unsigned char *buffer;
size_t buffer_size;
size_t done;

int channels, encoding;
long rate;
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*)malloc(buffer_size * sizeof(unsigned char));

mpg123_open(mh, "/home/abbas/Desktop/nastaran.mp3");
mpg123_getformat(mh, &rate, &channels, &encoding);

std::ofstream out("res.txt");
unsigned int counter = 0;

for (int totalBtyes = 0; mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK; ) {
short* tst = reinterpret_cast<short*>(buffer);
for (auto i = 0; i < buffer_size / 2; i++) {
out<< counter + i<<"\t"<< tst[i] << "\n";
}
counter += buffer_size/2;
totalBtyes += done;
}
out.close();
free(buffer);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
return 0;
}

关于c++ - mpg123 在 C++ 中将 mp3 解码为 pcm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com