4

Clyp is a service for hosting sound files that (as of now) doesn't require registration.

I'm wondering if it's possible to write a function that'll upload a Sound or Audio object to Clyp, just like the function for uploading images to imgur in the SE uploader. They have a nice API, so I thought it'd be a snap to use URLExecute. (But now I bit the bullet and decided to ask you experts.)

I've seen some of the stuff for uploading in here and in Stack Overflow, but I seem to be unable to adapt them. (In particular, I've tried using ExportString[Sound[blah], "MP3"] as an argument for the API, but it doesn't proceed.) If possible, I'd like to avoid JLink.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
teremok
  • 41
  • 1

1 Answers1

8

Important Note: Clyp seems to have changed its API-related policies recently in a way that breaks the routine given below. I do not know how to fix this. I can only guarantee that it was working well at the time it was written.


It took a while, but using ideas from Zach's answer here, I was able to cobble something together:

ClypExport[snd : (_Sound | _Audio), name_String, detailed : (True | False) : False] :=
Module[{raw}, 
       raw = URLExecute["https://upload.clyp.it/upload", {}, "RawJSON", 
                        Method -> "POST", 
                        "MultipartElements" ->
                        {{StringTemplate["file\"; filename=\"`name`.mp3"] @
                          <|"name" -> name|>, "audio/mpeg"} -> ExportString[snd, "MP3"]}];
       If[raw === $Failed || ! Lookup[raw, "Successful", False], Return[$Failed]];
       If[! detailed, raw @ "Url",
          Grid[Transpose[{{"Title:", "URL:", "MP3:", "OGG:"}, 
                          Prepend[Hyperlink /@
                                  Lookup[raw, {"Url", "SecureMp3Url", "SecureOggUrl"}], 
                                  raw @ "Title"]}], Alignment -> Left]]]

which can either return just the plain URL, or a detailed table showing the corresponding *.mp3 and *.ogg file links.

(If you are going to try this in version 11: it still works even though the Method and "MultipartElements" options are being highlighted as unrecognized. One could rewrite this function using URLFetch[] instead if it bothers you, but I'll leave that as an exercise.)

Examples:

zetaSound = Play[RiemannSiegelZ[2000 t], {t, 0, 3}, PlayRange -> {-20, 20}];
ClypExport[zetaSound, "sound of Riemann zeta function"]
   "https://clyp.it/hajdomy5"

(* old documentation example *)
chime = Play[Sin[π t/2] Sum[Piecewise[{{((i - 36)/13)^2 Sin[1000 (i + Sin[i]) t], 
                                       i/18 < t < (36 - i)/18}}, 0], {i, 36}] // N,
             {t, 0, 2}];
ClypExport[chime, "Sine Chime", True]

output


If you need to modify the routine for more detailed input or output, you can consult Clyp's API.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574