雖然沒有官方的方式直接從SoundCloud API請求獲取原始波形數據,但是有一種方法可以在PHP中使用此方法來導出SoundCloud在非官方端點(類似於https://wis.sndcdn.com/XwA2iPEIVF8z_m.json
之類)中顯示的完全相同的數據這樣的代碼。簡單地改變$image_file
值以符合您有任何的SoundCloud 1800 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/
網址是有用的,是沒有說服力如果/當SoundCloud會改變來自它的數據結構。從官方波形中獲取數據PNG提供了一些長期穩定性,因爲它們不會在沒有對SoundCloud API最終用戶進行公正警告的情況下更改該PNG圖像。
此外,請注意,雖然$source_width
是1800,但$source_height
是140,因爲雖然SoundCloud PNG文件是280像素高,但下半部分基本上只是上半部分的翻轉/鏡像副本。所以只需測量0到150的值就可以得到所需的波形數據值。