I want to live microphone feed on android, at 44kh...
# coroutines
u
I want to live microphone feed on android, at 44khz and 1024 buffer size thats roughly a buffer every 20ms Is this a good fit to be modeled as a ``FlowBuffer`? Would turning it yo
Channel
help if flow was too heavyweight?
g
Channels are heavier than flow, because they are thread safe and support multiple consumer/multiple emitters, Flow depends on implementation, but basic flow is faster, this one of the reason of their implementation Channels were optimized at some point, but they still inherently heavier
In general, I wouldn't recommend to use anything async for audio, at least until you really know what you are doing Of course, I don't know your use case, it may be fine with Flow, but this is one area where just minimal overhead will pay out with performance and reduce future problems. But maybe I just burnt by audio performance too much in my life, so I'm more cautious
u
what im trying to do is have live mic feed, and upload that to backend for voice recognition
g
realtime?
I would just write it to file and upload from it (realtime or not)
Otherwise you easily can get OOM because of buffer
so it's easier and safer to just write to file directly
u
but how, one thread writes to the store, and other thread reads and uploads?
g
yes
u
so raw threads, blocking requests?
g
it of course should be careful how you work with file, but it honestly looks way better
You can use coroutine threads, we use it everywhere, just easier to work and manager
just code itself is blocking
also upload to BE, you usually don't want to send 1k only, it's not really efficient, I would have bigger buffer
u
about the sending do you really think simply just a while (true) loop in a thread?
g
network could be way faster than your audio input, or could be way slower
yes, simple whille(true), with some way to remember what you already uploaded
u
about the file, im not sure how would that work? I was thinking just holding array of buffers in memory and synchronjze access to it with a file, how would I know what I already written?
@gildor
Copy code
thread {
    while (true) {
        val x = random.nextInt().toString()
        queue.offer(x)
        Thread.sleep(100)
    }
}

thread {
    while (true) {
        while (queue.peek() != null) {
            val item = queue.poll()
            Thread.sleep(500)
        }
    }
}
naive implementation of this, obviously works but the my coroutine brain has a issue with
while (true) while (queue.peek() != null)
. That means this condition is evaluated gazzilion times max speed the cpu can do, right when the queue is empty right? or is this fine and I should adjust my brain instead (as its never going to be permanently empty? 😄
g
so you still want to use memory Wav would be like 10 min per minute, so probably will work on practice, but I would still add some protection from too much data I just don't see issue with file, also if you don't want to lose user's recording
> That means this condition is evaluated gazzilion times max speed the cpu can do, right when the queue is empty right You may have some kind of suspend queue if you really want But i would probably just write to bytearray (or even better to okio.Buffer), keep counter if necessary For sending it's also depend on your strategy how you want to send it (wait for some buffer size or not) Using sleep is totally fine for such use cases btw, paying 1 thread for this is not a big deal on practice.