File: protocols.liq

package info (click to toggle)
liquidsoap 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,924 kB
  • sloc: ml: 73,577; javascript: 24,836; sh: 3,440; makefile: 764; xml: 114; ansic: 96; lisp: 62; python: 35; perl: 8; ruby: 8
file content (732 lines) | stat: -rw-r--r-- 20,708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# @flag hidden
def settings.make.protocol(name) =
  settings.make.void(
    "Settings for the #{name} protocol"
  )
end

let settings.protocol = settings.make.void(
  "Settings for registered protocols"
)

let settings.protocol.replaygain = settings.make.protocol("ReplayGain")
let settings.protocol.replaygain.path = settings.make(
  description="ReplayGain path",
  "#{configure.bindir}/extract-replaygain"
)
let settings.protocol.replaygain.tag = settings.make(
  description="Tag used to annotate ReplayGain",
  "replaygain_track_gain"
)

# @flag hidden
def exec_replaygain(~extract_replaygain="#{configure.bindir}/extract-replaygain",
                     ~delay=_,file)
  def log(x) = log.info(label="extract.replaygain",x) end
  p = process.run(process.quote.command(extract_replaygain, args=[file]))
  stdout = r/\n/.replace(fun (_) -> "", p.stdout)
  if p.status == "exit" and p.status.code == 0 then
    stdout
  else
    stderr = r/\n/.replace(fun (_) -> "", p.stderr)
    log("ReplayGain extraction by #{extract_replaygain} failed with: #{stderr}")
    ""
  end
end

# Register the replaygain protocol.
# @flag hidden
def replaygain_protocol(~rlog=_,~maxtime,arg)
  delay = maxtime - time()
  # The extraction program
  extract_replaygain = settings.protocol.replaygain.path()
  ret = exec_replaygain(delay=delay,extract_replaygain=extract_replaygain,arg)
  if ret != "" then
    tag = settings.protocol.replaygain.tag()
    ["annotate:#{tag}=\"#{ret}\":#{arg}"]
  else
    [arg]
  end
end
add_protocol("replaygain", replaygain_protocol,
             syntax="replaygain:uri",
             doc="Compute ReplayGain value using the extract-replaygain script. \
                  Adds returned value as `\"replaygain_track_gain\"` metadata")

let settings.protocol.process = settings.make.protocol("process")

let settings.protocol.process.env = settings.make(
  description="List of environment variables \
               passed down to the executed process.",
  []
)

let settings.protocol.process.inherit_env = settings.make(
  description="Inherit calling process's environment when `env` parameter is empty.",
  true
)

# Register the process protocol. Syntax:
# process:<output ext>,<cmd>:uri where <cmd> is interpolated with:
# [("input",<input file>),("output",<output file>),("colon",":")]
# See say: protocol for an example.
# @flag hidden
def process_protocol(~rlog=_,~maxtime,arg)
  log.info("Processing #{arg}")

  x = r/:/.split(arg)
  uri = string.concat(separator=":",list.tl(x))
  x = r/,/.split(list.hd(default="",x))
  extname = list.hd(default="liq",x)
  cmd = string.concat(separator=",",list.tl(x))

  output = file.temp("liq-process", ".#{extname}")

  def resolve(input) =
    cmd = cmd % [("input",process.quote(input)),
                 ("output",process.quote(output)),
                 ("colon",":")]

    log.info("Executing #{cmd}")

    env_vars = settings.protocol.process.env()
    env = environment()
    def get_env(k) = (k,env[k]) end
    env = list.map(get_env,env_vars)
    inherit_env = settings.protocol.process.inherit_env()

    delay = maxtime - time()
    p = process.run(timeout=delay,env=env,inherit_env=inherit_env,cmd)

    if p.status == "exit" and p.status.code == 0 then
      [output]
    else
      log.important("Failed to execute #{cmd}: #{p.status} (#{p.status.code})")
      log.info("Standard output:\n#{p.stdout}")
      log.info("Error output:\n#{p.stderr}")
      log.info("Removing #{output}.")
      file.remove(output)
      []
    end
  end

  if uri == "" then
    resolve("")
  else
    r = request.create(uri)
    delay = maxtime - time()
    if request.resolve(timeout=delay,r) then
      res = resolve(request.filename(r))
      request.destroy(r)
      res
    else
      log(level=3,"Failed to resolve #{uri}")
      []
    end
  end
end
add_protocol(temporary=true, "process", process_protocol,
             doc="Resolve a request using an arbitrary process. \
                  `<cmd>` is interpolated with: \
                  `[(\"input\",<input>),(\"output\",<output>),\
                  (\"colon\",\":\")]`. `uri` is an optional child request, \
                  `<output>` is the name of a fresh temporary file and has \
                  extension `.<extname>`. `<input>` is an optional input \
                  file name as returned while resolving `uri`.",
             syntax="process:<extname>,<cmd>[:uri]")

# Create a process: uri, replacing `:` with `$(colon)`
# @category Liquidsoap
# @param cmd Command line to execute
# @param ~extname Output file extension (with no leading '.')
# @param ~uri Input uri
def process_uri(~extname,~uri="",cmd) =
  cmd = r/:/.replace(fun (_) -> "$(colon)",cmd)
  uri = if uri != "" then ":#{uri}" else "" end
  "process:#{extname},#{cmd}#{uri}"
end

%ifdef http.head
# Resolve http(s) URLs using curl
# @flag hidden
def http_protocol(proto,~rlog,~maxtime,arg) =
  uri = "#{proto}:#{arg}"

  def log(~level,s) =
    rlog(s)
    log(label="procol.external",level=level,s)
  end

  timeout = maxtime - time()

  ret = http.head(timeout_ms=int_of_float(timeout*1000.), uri)
  code = ret.status_code ?? 999
  headers = ret.headers

  mime =
    if 200 <= code and code < 300 then
      headers["content-type"]
    else
      log(level=3,"Failed to fetch mime-type for #{uri}.")
      log(level=4,"Request response: #{ret}")
      null()
    end

  extname = null.case(mime, (fun () -> "osb"), (fun (mime) ->
    if list.mem(mime, ["audio/mpeg", "audio/mp3"]) then
      "mp3"
    elsif list.mem(mime,["application/ogg", "application/x-ogg",
                         "audio/x-ogg", "audio/ogg", "video/ogg"]) then
      "ogg"
    elsif list.mem(mime, ["audio/flac", "audio/x-flac"]) then
      "flac"
    elsif list.mem(mime,["audio/mp4", "application/mp4", "video/mp4"]) then
      "mp4"
    elsif list.mem(mime,["audio/vnd.wave", "audio/wav",
                         "audio/wave", "audio/x-wav"]) then
      "wav"
    else
      log(level=3,"No known file extension for mime: #{mime}")
      "osb"
    end))

  output = file.temp("liq-process", ".#{extname}")

  file_writer = file.write.stream(output)

  timeout = maxtime - time()

  try
    response = http.get.stream(
      on_body_data=file_writer,
      timeout_ms=int_of_float(timeout * 1000.),
      uri
    )

    if response.status_code < 400 then
      [output]
    else
      log(level=3, "Error while fetching http data: #{response.status_code} - #{response.status_message}")
      []
    end
  catch err do
    log(level=3, "Error while fetching http data: #{err}")
    []
  end
end

# Register download protocol.
# @flag hidden
def add_http_protocol(proto) =
  def http_protocol(~rlog,~maxtime,arg) = http_protocol(proto,rlog=rlog,maxtime=maxtime,arg) end
  add_protocol(temporary=true,syntax="#{proto}://...",doc="Download http URLs using curl",proto,http_protocol)
end
list.iter(add_http_protocol,["http","https"])
%endif

let settings.protocol.youtube_dl = settings.make.protocol("youtube-dl")

let settings.protocol.youtube_dl.path = settings.make(
  description="Path of the youtube-dl binary",
  "youtube-dl"
)

# Register the youtube-dl protocol, using youtube-dl.
# Syntax: youtube-dl:<ID>
# @flag hidden
def youtube_dl_protocol(~rlog,~maxtime,arg)
  binary = settings.protocol.youtube_dl.path()

  def log(~level,s) =
    rlog(s)
    log(label="protocol.youtube-dl",level=level,s)
  end

  delay = maxtime - time()
  cmd = "#{binary} --get-title --get-filename -- #{process.quote(arg)}"
  log(level=4,"Executing #{cmd}")
  x = process.read.lines(timeout=delay,cmd)

  x =
    if list.length(x) >= 2 then
      x
    else
      ["",".osb"]
    end

  title = list.hd(default="",x)
  ext   = file.extension(leading_dot=false,list.nth(default="",x,1))

  cmd = "rm -f $(output) && #{binary} -q -f best --no-playlist -o $(output) -- #{process.quote(arg)}"
  cmd = process_uri(extname=ext,cmd)

  if title != "" then
    ["annotate:title=#{string.quote(title)}:#{cmd}"]
  else
    [cmd]
  end
end
add_protocol("youtube-dl", youtube_dl_protocol,
              doc="Resolve a request using youtube-dl.",
              syntax="youtube-dl:uri")

# Register the youtube-pl protocol.
# Syntax: youtube-pl:<ID>
# @flag hidden
def youtube_pl_protocol(~rlog=_,~maxtime=_,arg)
  tmp = file.temp("youtube-pl","")
  file.write(data="youtube-pl:#{arg}",tmp)
  [tmp]
end
add_protocol("youtube-pl", youtube_pl_protocol,
              doc="Resolve a request as a youtube playlist using youtube-dl.",
              temporary=true,syntax="youtube-pl:uri")

# Register tmp
# @flag hidden
def tmp_protocol(~rlog=_, ~maxtime=_, arg) =
  [arg]
end
add_protocol("tmp",tmp_protocol,
             doc="Mark the given uri as temporary. Useful when chaining protocols",
             temporary=true,syntax="tmp:uri")

let settings.protocol.ffmpeg = settings.make.protocol("FFmpeg")

let settings.protocol.ffmpeg.path = settings.make(
  description="Path to the ffmpeg binary",
  "ffmpeg"
)

let settings.protocol.ffmpeg.metadata = settings.make(
  description="Should the protocol extract metadata",
  true
)

let settings.protocol.ffmpeg.replaygain = settings.make(
  description="Should the protocol adjust ReplayGain",
  false
)

# Register ffmpeg
# @flag hidden
def ffmpeg_protocol(~rlog,~maxtime,arg) =
  ffmpeg   = settings.protocol.ffmpeg.path()
  metadata = settings.protocol.ffmpeg.metadata()
  replaygain = settings.protocol.ffmpeg.replaygain()

  def log(~level,s) =
    rlog(s)
    log(label="protocol.ffmpeg",level=level,s)
  end

  def annotate(m) =
    def f(x) =
      let (key,value) = x
      "#{key}=#{string.quote(value)}"
    end
    m = string.concat(separator=",",list.map(f,m))
    if string.length(m) > 0 then
      "annotate:#{m}:"
    else
      ""
    end
  end

  def parse_metadata(file) =
    cmd = "#{ffmpeg} -i #{process.quote(file)} -f ffmetadata - 2>/dev/null | grep -v '^;'"
    delay = maxtime - time()
    log(level=4,"Executing #{cmd}")
    lines = process.read.lines(timeout=delay,cmd)
    def f(cur,line) =
      m = r/=/.split(line)
      if list.length(m) >= 2 then
        key = list.hd(default="",m)
        value = string.concat(separator="=",list.tl(m))
        (key,value)::cur
      else
        cur
      end
    end
    list.fold(f,[],lines)
  end

  def replaygain_filter(file) =
    if replaygain then
      # The extraction program
      extract_replaygain = settings.protocol.replaygain.path()
      delay = maxtime - time()
      ret = exec_replaygain(delay=delay,extract_replaygain=extract_replaygain,file)
      if ret != "" then
        "-af \"volume=#{ret}\""
      else
        ""
      end
    else
      ""
    end
  end

  def cue_points(m) =
    cue_in = float_of_string(default=0., list.assoc(default="0.","liq_cue_in",m))
    cue_out = float_of_string(default=0., list.assoc(default="","liq_cue_out",m))

    args =
      if cue_in > 0. then
        "-ss #{cue_in}"
      else
        ""
      end

    if cue_out > cue_in then
      "#{args} -t #{cue_out-cue_in}"
    else
      args
    end
  end

  def fades(r) =
    m = request.metadata(r)

    fade_type = list.assoc(default="","liq_fade_type",m)
    fade_in = list.assoc(default="","liq_fade_in",m)
    cue_in = list.assoc(default="","liq_cue_in",m)
    fade_out = list.assoc(default="","liq_fade_out",m)
    cue_out = list.assoc(default="","liq_cue_out",m)

    curve =
      if fade_type == "lin" then
        ":curve=tri"
      elsif fade_type == "sin" then
        ":curve=qsin"
      elsif fade_type == "log" then
        ":curve=log"
      elsif fade_type == "exp" then
        ":curve=exp"
      else
        ""
      end


    args =
      if fade_in != "" then
        fade_in = float_of_string(default=0.,fade_in)
        start_time =
          if cue_in != "" then
            float_of_string(default=0.,cue_in)
          else
            0.
        end
        if fade_in > 0. then
          ["afade=in:st=#{start_time}:d=#{fade_in}#{curve}"]
        else
          []
        end
      else
        []
      end

    args =
      if fade_out != "" then
        fade_out = float_of_string(default=0.,fade_out)
        end_time =
          if cue_out != "" then
            float_of_string(default=0.,cue_out)
          else
            request.duration(request.filename(r))
          end
        if fade_out > 0. then
          list.append(args,["afade=out:st=#{end_time-fade_out}:d=#{fade_out}#{curve}"])
        else
          args
        end
      else
        args
      end

    if list.length(args) > 0 then
      args = string.concat(separator=",",args)
      "-af #{args}"
    else
      ""
    end
  end

  r = request.create(arg)
  delay = maxtime - time()
  if request.resolve(timeout=delay,r) then
    filename = request.filename(r)
    m = request.metadata(r)

    m = if metadata then
      list.append(m,parse_metadata(filename))
    else
      m
    end

    annotate = annotate(m)
    request.destroy(r)

    # Now parse the audio
    wav = file.temp("liq-process", ".wav")

    cue_points = cue_points(request.metadata(r))
    fades = fades(r)
    replaygain_filter = replaygain_filter(filename)

    cmd = "#{ffmpeg} -y -i $(input) #{cue_points} #{fades} #{replaygain_filter} #{process.quote(wav)}"

    uri = process_uri(extname="wav",uri=filename,cmd)

    wav_r = request.create(uri)
    delay = maxtime - time()
    if request.resolve(timeout=delay,wav_r) then
      request.destroy(wav_r)
      ["#{annotate}tmp:#{wav}"]
    else
      log(level=3,"Failed to resolve #{uri}")
      []
    end
  else
    log(level=3,"Failed to resolve #{arg}")
    []
  end
end
add_protocol("ffmpeg",ffmpeg_protocol,
             doc="Decode any file to wave using ffmpeg",
             syntax="ffmpeg:uri")

# Register stereo protocol which converts a file to stereo (currently decodes as
# wav).
# @flag hidden
def stereo_protocol(~rlog=_, ~maxtime=_, arg)
  file = file.temp("liq-stereo", ".wav")
  r = request.create(arg)
  if not request.resolve(r) then
    log.info("Stereo: failed to resolve request #{arg}")
    []
  else
    # TODO: the following sometimes hangs, so we resolve twice...
    # source.dump(%wav, file, audio_to_stereo(once(request.queue(queue=[r]))))
    source.dump(%wav, file, audio_to_stereo(once(single(arg))))
    [file]
  end
end
add_protocol(static=true, temporary=true, "stereo", stereo_protocol, doc="Convert a file to stereo (currently decodes to wav).", syntax="stereo:<uri>")

let settings.protocol.text2wave = settings.make.protocol("text2wave")

let settings.protocol.text2wave.path = settings.make(
  description="Path to the text2wave binary",
  "text2wave"
)

# Register the text2wave: protocol using text2wav
# @flag hidden
def text2wave_protocol(~rlog=_, ~maxtime=_, arg) =
  binary = settings.protocol.text2wave.path()
  [process_uri(extname="wav", "echo #{process.quote(arg)} | #{binary} -scale 1.9 > $(output)")]
end
add_protocol(static=true,"text2wave",text2wave_protocol,
             doc="Generate speech synthesis using text2wave. Result may be mono.",
             syntax="text2wave:Text to read")

let settings.protocol.gtts = settings.make.protocol("gtts")

let settings.protocol.gtts.path = settings.make(
  description="Path to the gtts binary",
  "gtts-cli"
)

# Register the gtts: protocol using gtts
# @flag hidden
def gtts_protocol(~rlog=_, ~maxtime=_, arg) =
  binary = settings.protocol.gtts.path()
  [process_uri(extname="mp3", "#{binary} -o $(output) #{process.quote(arg)}")]
end
add_protocol(static=true,"gtts",gtts_protocol,
             doc="Generate speech synthesis using Google translate's text-to-speech API. This requires the `gtts-cli` binary.  Result may be mono.",
             syntax="gtts:Text to read")

# Register the legacy say: protocol
# @flag hidden
def say_protocol(~rlog=_, ~maxtime=_, arg) =
  ["stereo:gtts:#{arg}", "stereo:text2wave:#{arg}"]
end
add_protocol(static=true,"say",say_protocol,
             doc="Generate speech synthesis using text2wave. Result is always stereo.",
             syntax="say:Text to read")

let settings.protocol.aws = settings.make.protocol("AWS")

let settings.protocol.aws.profile = settings.make(
  description="Use a specific profile from your credential file.",
  null()
)

let settings.protocol.aws.endpoint = settings.make(
  description="Alternative endpoint URL (useful for other S3 implementations).",
  null()
)

let settings.protocol.aws.region = settings.make(
  description="AWS Region",
  null()
)

let settings.protocol.aws.path = settings.make(
  description="Path to aws CLI binary",
  "aws"
)

let settings.protocol.aws.polly = settings.make.protocol("polly")

let settings.protocol.aws.polly.format = settings.make(
  description="Output format",
  "mp3"
)

let settings.protocol.aws.polly.voice = settings.make(
  description="Voice ID",
  "Joanna"
)

# Build a aws base call
# @flag hidden
def aws_base() =
  aws = settings.protocol.aws.path()

  region = settings.protocol.aws.region()

  aws =
    if null.defined(region) then
      "#{aws} --region #{null.get(region)}"
    else
      aws
    end

  endpoint = settings.protocol.aws.endpoint()

  aws =
    if null.defined(endpoint) then
      "#{aws} --endpoint-url #{process.quote(null.get(endpoint))}"
    else
      aws
    end

  profile = settings.protocol.aws.profile()

  if null.defined(profile) then
    "#{aws} --profile #{process.quote(null.get(profile))}"
  else
    aws
  end
end

# Register the s3:// protocol
# @flag hidden
def s3_protocol(~rlog=_, ~maxtime=_, arg) =
  extname = file.extension(leading_dot=false,dir_sep="/",arg)
  arg = process.quote("s3:#{arg}")
  [process_uri(extname=extname,"#{aws_base()} s3 cp #{arg} $(output)")]
end
add_protocol("s3",s3_protocol,doc="Fetch files from s3 using the AWS CLI",
             syntax="s3://uri")

# Register the polly: protocol using AWS Polly
# speech synthesis services. Syntax: polly:<text>
# @flag hidden
def polly_protocol(~rlog=_, ~maxtime=_, text) =
  aws = aws_base()

  format = settings.protocol.aws.polly.format()

  extname =
    if format == "mp3" then
      "mp3"
    elsif format == "ogg_vorbis" then
      "ogg"
    else
      "wav"
    end

  aws = "#{aws} polly synthesize-speech --output-format #{format}"

  voice_id = settings.protocol.aws.polly.voice()

  cmd = "#{aws} --text #{process.quote(text)} --voice-id #{process.quote(voice_id)} $(output)"

  [process_uri(extname=extname,cmd)]
end
add_protocol(static=true,"polly",polly_protocol,
             doc="Generate speech synthesis using AWS polly service. \
                  Result might be mono, needs aws binary in the path.",
             syntax="polly:Text to read")

# Protocol to synthesize audio.
# @flag hidden
def synth_protocol(~rlog=_, ~maxtime=_, text) =
  log.debug(label="synth", "Synthesizing request: #{text}")
  args = r/,/.split(text)
  args = list.map(r/=/.split, args)
  if list.exists(fun(l)-> list.length(l) != 2, args) then
    []
  else
    args = list.map(fun(l) -> (list.hd(default="",l), list.hd(default="",list.tl(l))), args)
    shape = ref("sine")
    duration = ref(10.)
    frequency = ref(440.)
    def set(p)
      let (k,v) = p
      if k == "d" or k == "duration" then duration := float_of_string(v)
      elsif k == "f" or k == "freq" or k == "frequency" then frequency := float_of_string(v)
      elsif k == "s" or k == "shape" then shape := v
      end
    end
    list.iter(set, args)
    def synth(s)
      file = file.temp("liq-synth",".wav")
      log.info(label="synth", "Synthesizing #{!shape} in #{file}.")
      source.dump(%wav, file, once(s))
      [file]
    end
    if !shape == "sine" then
      synth(sine(duration=!duration, !frequency))
    elsif !shape == "saw" then
      synth(saw(duration=!duration, !frequency))
    elsif !shape == "square" then
      synth(square(duration=!duration, !frequency))
    elsif !shape == "blank" then
      synth(blank(duration=!duration))
    else
      []
    end
  end
end

add_protocol(static=true, temporary=true, "synth", synth_protocol,
doc="Syntesize audio. Parameters are optional.",
syntax="synth:shape=sine,frequency=440.,duration=10.")

# File protocol
# @flag hidden
def file_protocol(~rlog=_, ~maxtime=_, arg) =
  path = list.nth(default="", r/:/.split(arg), 1)
  segments = r/\//.split(path)

  segments =
    if list.length(segments) > 3 and
       list.nth(default="foo",segments,0) == "" and
       list.nth(default="foo",segments,1) == ""
    then
      list.tl(list.tl(segments))
    else
      segments
    end

  [url.decode(string.concat(separator="/",segments))]
end

add_protocol(static=true, temporary=false, "file", file_protocol,
             doc="File protocol. Only local files are supported",
             syntax="file:///path/to/file")