Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mjago committed May 30, 2018
0 parents commit bfcee8e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
** UDP Audio Player

./player localhost 7355
72 changes: 72 additions & 0 deletions player.cr
@@ -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


14 changes: 14 additions & 0 deletions shard.yml
@@ -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

0 comments on commit bfcee8e

Please sign in to comment.