Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bfcee8e
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
** UDP Audio Player | ||
|
||
./player localhost 7355 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "socket" | ||
require "libao" | ||
require "uri" | ||
include Libao | ||
|
||
class UdpPlayer | ||
|
||
def initialize | ||
@ao = Ao.new | ||
@done = 0 | ||
@playdata = Channel(Bytes).new | ||
@playsize = Channel(Int32).new | ||
@server = UDPSocket.new | ||
@quit = false | ||
init_audio | ||
bind_to_server | ||
end | ||
|
||
def play | ||
spawn do | ||
while @quit == false | ||
message = Bytes.new(4096) | ||
size, client_addr = @server.receive(message) | ||
@playdata.send(message) | ||
@playsize.send(size) | ||
end | ||
end | ||
|
||
spawn do | ||
while @quit == false | ||
message = @playdata.receive | ||
size = @playsize.receive | ||
@ao.play(message, size) | ||
end | ||
end | ||
end | ||
|
||
def exit | ||
@server.close | ||
@ao.exit | ||
@quit = true | ||
end | ||
|
||
private def bind_to_server | ||
adr = address | ||
@server.bind(adr[:host], adr[:port]) | ||
end | ||
|
||
private def init_audio | ||
bits = 16 | ||
rate = 48000 | ||
channels = 1 | ||
byte_format = LibAO::Byte_Format::AO_FMT_BIG | ||
@ao.set_format(bits, rate, channels, byte_format, matrix = nil) | ||
@ao.open_live | ||
end | ||
|
||
private def address() : { host: String, port: Int32 } | ||
if ARGV.size == 2 | ||
host, port = ARGV[0], ARGV[1].to_i | ||
else | ||
host, port = "localhost", 7355 | ||
end | ||
{host: host, port: port} | ||
end | ||
end | ||
|
||
player = UdpPlayer.new | ||
player.play | ||
player.exit if gets | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: udp_player | ||
version: 0.1.0 | ||
|
||
authors: | ||
- name <email@example.com> | ||
|
||
# description: | | ||
# Short description of | ||
|
||
dependencies: | ||
libao: | ||
github: mjago/libao | ||
|
||
license: MIT |