gpt4 book ai didi

spotify - 将 libspotify 中的音频数据写入文件时出现奇怪的噪音和异常

转载 作者:行者123 更新时间:2023-12-04 05:13:33 24 4
gpt4 key购买 nike

目前我们正在 win 7 64 位系统中实现 Libspotify。除了播放之外,一切似乎都正常。我们从回调中获取数据,但即使在保存的音频上使用声音,也充满了异常。因此为了进一步研究,我们采用了 win32 示例 (spshell) 并对其进行了修改以将音乐数据保存到文件中。同样的问题,肯定是带有这些刻度的音乐。我确定我在这里缺少一些简单的东西,但我不知道可能是什么问题。任何帮助都会很棒,因为目前我们的项目处于停滞状态,直到我们能够解决这个问题。

保存的音频可以在这里查看
http://uploader.crestron.com/download.php?file=8001d80992480280dba365752aeaca81

以下是我为保存文件所做的代码更改(仅用于测试)

static FILE *pFile;
int numBytesToWrite=0;
CRITICAL_SECTION m_cs;

int SP_CALLCONV music_delivery(sp_session *s, const sp_audioformat *fmt, const void *frames, int num_frames)
{
if ( num_frames == 0 )
return;
EnterCriticalSection(&m_cs);
numBytesToWrite = ( num_frames ) * fmt->channels * sizeof(short);
if (numBytesToWrite > 0 )
fwrite(frames, sizeof(short), numBytesToWrite, pFile);
LeaveCriticalSection(&m_cs);
return num_frames;
}
static void playtrack_test(void)
{


sp_error err;
InitializeCriticalSection(&m_cs);
pFile = fopen ("C:\\zzzspotify.pcm","wb");
test_start(&playtrack);
if((err = sp_session_player_load(g_session, stream_track)) != SP_ERROR_OK) {
test_report(&playtrack, "Unable to load track: %s", sp_error_message(err));
return;
}

info_report("Streaming '%s' by '%s' this will take a while", sp_track_name(stream_track),
sp_artist_name(sp_track_artist(stream_track, 0)));
sp_session_player_play(g_session, 1);
}

void SP_CALLCONV play_token_lost(sp_session *s)
{
fclose(pFile);
DeleteCriticalSection(&m_cs);
stream_track_end = 2;
notify_main_thread(g_session);
info_report("Playtoken lost");
}
static int check_streaming_done(void)
{
if(stream_track_end == 2)
test_report(&playtrack, "Playtoken lost");
else if(stream_track_end == 1)
test_ok(&playtrack);
else
return 0;
fclose(pFile);
stream_track_end = 0;
return 1;
}

最佳答案

看起来这是问题所在:
fwrite(frames, sizeof(short), numBytesToWrite, pFile);fwrite文档指出,第二个参数是“要写入的每个元素的大小(以字节为单位)”,第三个参数是“元素的数量,每个元素的大小都是 size 个字节”。

您打电话的方式 frwrite会告诉它写 numBytesToWrite * sizeof(short)字节,它将在给定缓冲区的末尾运行。我真的很惊讶它不会崩溃!

我建议改变你的 fwrite调用类似的东西:
fwrite(frames, sizeof(char), numBytesToWrite, pFile);
或:

int numSamplesToWrite = num_frames * fmt->channels;
fwrite(frames, sizeof(short), numSamplesToWrite, pFile);

编辑:

在详细查看您的音频后,我更加确信情况确实如此。这首歌似乎以半速播放(即写入的数据量是原来的 2 倍),并且伪像看起来像是缓冲区溢出到随机内存中。

关于spotify - 将 libspotify 中的音频数据写入文件时出现奇怪的噪音和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14587235/

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