Search code examples
iosswiftiphoneavaudioplayer

How to draw waveform from waveformdata object in iOS Swift?


[
  {
    "id": "48250",
    "created_at": "2014-07-06 13:05:10",
    "user_id": "7",
    "duration": "7376",
    "permalink": "shawne-back-to-the-roots-2-05072014",
    "description": "Years: 2000 - 2005\r\nSet Time: Warm Up (11 pm - 01 am)\r\n",
    "downloadable": "1",
    "genre": "Drum & Bass",
    "genre_slush": "drumandbass",
    "title": "Shawne @ Back To The Roots 2 (05.07.2014)",
    "uri": "https:\/\/api-v2.hearthis.at\/\/shawne-back-to-the-roots-2-05072014\/",
    "permalink_url": "http:\/\/hearthis.at\/\/shawne-back-to-the-roots-2-05072014\/",
    "artwork_url": "http:\/\/hearthis.at\/_\/cache\/images\/track\/500\/801982cafc20a06ccf6203f21f10c08d_w500.png",
    "background_url": "",
    "waveform_data": "http:\/\/hearthis.at\/_\/wave_data\/7\/3000_4382f398c454c47cf171aab674cf00f0.mp3.js",
    "waveform_url": "http:\/\/hearthis.at\/_\/wave_image\/7\/4382f398c454c47cf171aab674cf00f0.mp3.png",
    "user": {
      "id": "7",
      "permalink": "shawne",
      "username": "Shawne (hearthis.at)",
      "uri": "https:\/\/api-v2.hearthis.at\/shawne\/",
      "permalink_url": "http:\/\/hearthis.at\/shawne\/",
      "avatar_url": "http:\/\/hearthis.at\/_\/cache\/images\/user\/512\/06a8299b0e7d8f2909a22697badd7c09_w512.jpg"
    },
    "stream_url": "http:\/\/hearthis.at\/shawne\/shawne-back-to-the-roots-2-05072014\/listen\/",
    "download_url": "http:\/\/hearthis.at\/shawne\/shawne-back-to-the-roots-2-05072014\/download\/",
    "playback_count": "75",
    "download_count": "9",
    "favoritings_count": "7",
    "favorited": false,
    "comment_count": "0"
  }
]

This Api returns waveform url and waveformdata. How do I convert waveform data to draw waveform similar to image in waveformurl

Api - https://hearthis.at/api-v2/


Solution

  • It looks like the "data" is merely a succession of bar heights:

    [136,132,133,133,138,...]
    

    So just draw a succession of bars at those heights (or heights proportional to them). You might need to draw just every nth bar, or maybe average each clump of n bars together, in order to get a neater representation (that is what they do at the site you pointed to).