生の波形データをSoundCloud APIリクエストから直接取得する公式の方法はありませんが、SoundCloudが非公式のエンドポイント(別名: のようなものhttps://wis.sndcdn.com/XwA2iPEIVF8z_m.json
)で公開するのとまったく同じデータを、このようなコードを使用して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 がその URL からのデータの構造を変更するかどうか、いつ変更するかがわからないことです。公式の波形 PNG からデータを取得すると、SoundCloud API エンド ユーザーへの公正な警告なしにその PNG 画像を変更するだけではないため、長期的な安定性が得られます。
また、 SoundCloud PNG ファイルの高さは 280 ピクセルですが、下半分は基本的に上半分の反転/ミラーコピーであるため、$source_width
1800は 140であることに注意してください。$source_height
したがって、0 から 150 までの値を測定するだけで、必要な波形データの値が得られます。