【问题标题】:Is there a way to retrieve or derive raw SoundCloud API waveform data?有没有办法检索或导出原始 SoundCloud API 波形数据?
【发布时间】:2012-05-18 12:35:37
【问题描述】:
我正在创建一个使用 SoundCloud API 流式传输艺术家曲目的 Web 应用程序。我知道如何获得波形 PNG 图像(例如http://w1.sndcdn.com/fxguEjG4ax6B_m.png),但我实际上需要某种波形数据(歌曲中什么时候高,什么时候低?)。
我无法访问诸如 LAME 之类的音频库或类似的东西,因为我的虚拟主机不允许这样做。有没有可能
- 以某种方式直接从 SoundCloud API 获取这些数据。
- 在 PHP 或 JavaScript 中处理波形 PNG 图像以检索所需数据? (是否有某种库可用于这种处理?)
【问题讨论】:
标签:
php
javascript
soundcloud
waveform
【解决方案1】:
Soundcloud 开始提供浮点数,但尚未正式发布。只是一个小技巧,当你有你的 PNG 时:
https://w1.sndcdn.com/XwA2iPEIVF8z_m.png
将“w1”更改为“wis”,将“png”更改为“json”:
https://wis.sndcdn.com/XwA2iPEIVF8z_m.json
你明白了!
【解决方案3】:
虽然没有官方方法可以直接从 SoundCloud API 请求获取原始波形数据,但有一种方法可以在 PHP 中使用这段代码是这样的。只需更改 $image_file 的值以匹配您拥有的任何 SoundCloud 1800 宽 x 280 高 PNG 图像,您就可以开始了:
$source_width = 1800;
$source_height = 140;
$image_file = 'https://w1.sndcdn.com/XwA2iPEIVF8z_m.png';
$image_processed = imagecreatefrompng($image_file);
imagealphablending($image_processed, true);
imagesavealpha($image_processed, true);
$waveform_data = array();
for ($width = 0; $width < $source_width; $width++) {
for ($height = 0; $height < $source_height; $height++) {
$color_index = @imagecolorat($image_processed, $width, $height);
// Determine the colors—and alpha—of the pixels like this.
$rgb_array = imagecolorsforindex($image_processed, $color_index);
// Peak detection is based on matching a transparent PNG value.
$match_color_index = array(0, 0, 0, 127);
$diff_value = array_diff($match_color_index, array_values($rgb_array));
if (empty($diff_value)) {
break;
}
} // $height loop.
// Value is based on the delta between the actual height versus detected height.
$waveform_data[] = $source_height - $height;
} // $width loop.
// Dump the waveform data array to check the values.
echo '<pre>';
print_r($waveform_data);
echo '</pre>';
这种方法的好处是虽然https://wis.sndcdn.com/ URL 很有用,但不知道 SoundCloud 是否/何时会改变来自它的数据结构。从官方波形 PNG 派生数据提供了一些长期稳定性,因为它们不仅会在没有向 SoundCloud API 最终用户发出公平警告的情况下更改该 PNG 图像。
另外,请注意$source_width 是 1800,$source_height 是 140,因为虽然 SoundCloud PNG 文件的高度为 280 像素,但下半部分基本上只是上半部分的翻转/镜像副本。因此,只需测量 0 到 150 之间的值,即可获得必要的波形数据值。