-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstreamfile-encode
executable file
·47 lines (36 loc) · 1.38 KB
/
streamfile-encode
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
#!/usr/bin/env raku
use v6;
use Audio::Encode::LameMP3;
use Audio::Libshout;
use Audio::Sndfile;
multi sub MAIN(Str :$host = 'localhost', Int :$port = 8000, Str :$user = 'source', Str :$password!, Int :$bitrate = 128, Int :$quality = 5, Int :$bufsize = 512, Str :$mount = '/stream', Str :$file!, *%extra) {
my $format = Audio::Libshout::Format::MP3;
my $sndfile = Audio::Sndfile.new(filename => $file, :r);
my $mode = do given $sndfile.channels {
when 2 {
Audio::Encode::LameMP3::JointStereo;
}
when 1 {
Audio::Encode::LameMP3::Mono;
}
default {
die "$file has $_ channels - I only support 2 at most";
}
}
my $in-samplerate = $sndfile.samplerate;
my $shout = Audio::Libshout.new(:$host, :$port, :$user, :$password, :$mount, :$format, |%extra);
my $encoder = Audio::Encode::LameMP3.new(:$bitrate, :$mode, :$quality);
my $send-channel = $shout.send-channel;
loop {
my $in-pcm = $sndfile.read-short($bufsize, :raw);
my $encoded = $encoder.encode-short($in-pcm[0], $in-pcm[1], :raw);
$send-channel.send($encoded);
last if $in-pcm[1] != $bufsize;
}
$sndfile.close;
my $encoded = $encoder.encode-flush(:raw);
$send-channel.send($encoded);
$send-channel.close;
$shout.close;
}
# vim: expandtab shiftwidth=4 ft=raku