more experiments; basic one-way voip; pulse instead of alsa for capturing; capture multiple audio streams using pulse
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import "core:strings"
|
||||
import "core:thread"
|
||||
import "core:fmt"
|
||||
import "core:net"
|
||||
import "core:os/os2"
|
||||
|
||||
import "shared:alsa"
|
||||
|
||||
DEVICE :: "default"
|
||||
FRAMES :: 128
|
||||
FORMAT: alsa.pcm_format_t : .S16_LE
|
||||
BITS_PER_SAMPLE :: 16
|
||||
CHANNELS :: 1
|
||||
SAMPLE_RATE :: 22050
|
||||
UDP_DATAGRAM_MAX_LEN :: 65535
|
||||
|
||||
main :: proc() {
|
||||
handle: ^alsa.pcm_t
|
||||
|
||||
alsa_err := alsa.pcm_open(&handle, DEVICE, .CAPTURE, .BLOCK)
|
||||
if alsa_err < 0 {
|
||||
fmt.eprintln("Failed to open default capture device:", alsa_err)
|
||||
os2.exit(1)
|
||||
}
|
||||
defer alsa.pcm_close(handle)
|
||||
|
||||
alsa_err = alsa.pcm_set_params(handle, FORMAT, .RW_INTERLEAVED, CHANNELS, SAMPLE_RATE, 1, 500000)
|
||||
if alsa_err < 0 {
|
||||
fmt.eprintln("Failed to set the HW params:", alsa_err)
|
||||
os2.exit(1)
|
||||
}
|
||||
|
||||
alsa_err = alsa.pcm_prepare(handle)
|
||||
if alsa_err < 0 {
|
||||
fmt.eprintln("Failed to prepare audio device for capture:", alsa_err)
|
||||
os2.exit(1)
|
||||
}
|
||||
|
||||
sock, err := net.make_bound_udp_socket(net.IP4_Address{0, 0, 0, 0}, 50550)
|
||||
//sock, err := net.create_socket(.IP4, .UDP)
|
||||
if err != nil {
|
||||
fmt.eprintln("Failed to create a UDP socket:", err)
|
||||
os2.exit(1)
|
||||
}
|
||||
fmt.println("Listening on 0.0.0.0:50550")
|
||||
|
||||
samples := make([]u8, FRAMES * CHANNELS * alsa.pcm_format_width(FORMAT) / 8)
|
||||
|
||||
thread.run_with_data(&sock, proc(sock: rawptr) {
|
||||
sock := transmute(^net.UDP_Socket)sock
|
||||
recv_buf := make([]u8, UDP_DATAGRAM_MAX_LEN)
|
||||
|
||||
for {
|
||||
read_bytes, recv_endpoint, err := net.recv_udp(sock^, recv_buf)
|
||||
if err != nil {
|
||||
if err == .Excess_Truncated {
|
||||
continue
|
||||
}
|
||||
fmt.eprintln("Failed to receive data from UDP socket:", err)
|
||||
os2.exit(1)
|
||||
}
|
||||
if read_bytes > 0 {
|
||||
fmt.printf("Got this message from \"%v:%v\": ", recv_endpoint.address, recv_endpoint.port)
|
||||
fmt.printfln("\"%v\"", strings.trim_space(string(recv_buf)))
|
||||
//fmt.println(read_bytes)
|
||||
//fmt.println(recv_endpoint)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
for {
|
||||
read := alsa.pcm_readi(handle, raw_data(samples), FRAMES)
|
||||
if read != FRAMES {
|
||||
fmt.eprintfln("Expected to read %v frame, got %v frames instead", FRAMES, read)
|
||||
os2.exit(1)
|
||||
}
|
||||
|
||||
endpoint := net.Endpoint{
|
||||
address = net.IP4_Address{10, 0, 0, 5},
|
||||
port = 1234,
|
||||
}
|
||||
|
||||
_, err := net.send_udp(sock, samples, endpoint)
|
||||
if err != nil {
|
||||
fmt.eprintln("Failed to send data to UDP socket:", err)
|
||||
os2.exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user