C++ Streaming audio from microphone [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have got a classic client-server environment in c++.
Now i want to implement a voice chat between specific clients.
My problem is: How to stream without "pauses" the audio. I'm interestend only to the theory. My first idea was capture some bytes every 0,5 seconds to a buffer and then send it to the server, which re-send it to the interested clients. But I don't know how to do this as in-real-time as possible!
Edit Client is Windows and Server is Linux

You would pick a codec and stream based on the clock rate specified by that codec. For example, the G.711 codec specifies a clock rate of 8000 Hz (meaning the microphone input source will be sampled 8000 times per second). It also specifies that (by default) each packet should contain 20 milliseconds of audio, so over one second you would send 50 audio packets (1 second = 1000 millseconds / 20 millseconds per packet = 50 packets per second).
Implementation-wise (for real-time) you would have a separate thread that has "realtime" priority that would be responsible for sampling the audio from the microphone, wrapping it in a RTP packet, and shipping that packet off to your server. Your server would likewise have a separate "realtime" priority thread that would receive each RTP packet and forward it to each client that is subscribed.

Related

How to build a robust network architecture using sockets [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have to make connections form one server to many PCs ( ~1000 PC). These PC are connected by a Wifi Network in the same Building.
Each PC have a dedicated connection with the server. Form its IP address, the server knows the specific data to generate to him.
I have to send a dedicated short strings over network to each PC. (~30 characters by a string)
The dedicated string is sent by a frequency of 30 strings by a second to each PC.
The problem is that these sent data are critical and should be sent in real time.
Which solution is the faster and the most robust in my case?
I assume you have two PC connected by some Ethernet or wifi, or good enough modern Internet connection (both on Earth; no interplanetary ...; no pigeon IP RFC1149 or 1200 baud analog modem from the 1970s).
Then 30 strings of about 30 chars per second is about a kilobyte per second, not a big deal, and certainly not high frequency as you claim.
My current fiber internet connection at home (near Paris, France) is able of a dozen of megabytes per second of download, and at least a megabyte per second of upload. A few years ago it was ADSL with about one megabyte per second download. I never had at home an Internet connection for which a kilobyte each second was a high load. (If you are in interplanetary space, or in the most remote and desolate places of Africa or Antarctica, then 1Kbyte/sec might be an issue in 2016, but then you are very unlucky regarding Internet connection).
Your HTTP setup might use websockets (so a bit like your second solution). You could use libonion (an HTTP server library) on the server side, and libcurl (an HTTP client library) on the client side. Periodically polling (e.g. issuing an HTTP request twenty times per second) would require more resources (but that is still manageable). An HTTP connection would be slower, because HTTP adds some overhead (the headers in HTTP requests & responses).
Notice that HTTP protocol is above TCP/IP, so will definitely use BSD sockets on operating systems providing them (Linux, Windows, MacOSX, ...). So a "web solution" is using sockets already.
If you use sockets, you'll need to define a protocol on them (or using some existing one, like HTTP or JSONRPC).
I'll go for a socket approach. Probably some JSON related thing like JSONRPC. Be aware, if you code on the socket API, that TCP/IP is a stream protocol without message boundaries. You'll need to buffer on both sides, and define some message boundary conventions. You might send JSON, terminated by a newline (the ending newline is JSON compatible, and facilitate delimiting messages).
You might be interested by messaging libraries such as 0mq.
addenda (after question edition)
Your new question is widely different (thousands of PCs, not only two of them; I guess they are in the same building, or at least the same continent.). You need about 1000 * 30 * 30 i.e. less than a megabyte per second of bandwidth.
I would still suggest using some sockets. Probably 0mq is even more relevant. You might make each message some JSON. You need to document very well the protocol you are using. Probably, you want the server to have several threads (e.g. a dozen, not many thousands of threads) to loop on emitting messages (on TCP). Perhaps you might want to have a server with several Ethernet connections (but a megabyte per second can go on one single Ethernet, even a slow 100Mbits/sec one).
30 bytes, 30 times per second, is 900 bytes per second. That's not fast at all; either method will work fine. And note that an HTTP connection uses a socket anyway.
It sounds like your "socket" option implies keeping a socket connection open all the time, as opposed to HTTP, where (typically) a separate connection is opened for each request. I think what you're really asking is:
Make the client periodically ask the server if there's new data, or
Have the server immediately send the new data as soon as it's available.
That depends entirely on what your program's requirements are, which we don't know.
A thousand biderectional TCP communications require 1000 sockets (unless you want to open and close connection for every string sent, but that would be a major performance drain).
That is dangerously close to the customary soft limit of maximum open file descriptors (which is 1024). And it is 25% of customary hard limit of 4096. Given that, I find that TCP is not well suited here.
Instead, I suggest going with UDP. With UDP, you'd need only handful of sockets (even one would do, but with multiple you could scale better). It would have a problem of reliability, but you can implement some sort of it on top of UDP.
Please make yourself familiar with the OSI model.
Sockets (UDP, TCP) are on layer 4, HTTP is on layer 5, thus using a layer 4 protocol already.

MFC application that uses Serial Ports [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For my thesis I'm given an application written in visual c++ and using MFC that receives gps data (NMEA 183 (RMC)), the data is received from UDP and/or a Serial port (COM port) (they have to work separatly or together). The UDP part of the application is done and its working (written by another student before me), so I have to finish the Serial port part, but here comes my problem, it turns out that MFC does not support COM ports or at least doesn't have a class for serial ports, I found some serial port implementation on the internet: LINK, but I dont know how to integrate it and make it work, it turns out that the UDP part is working like an event triggering mechanism using virtual function CAsyncSocket::OnReceive. I was wondering if its posible to make anything similar to this working with Serial Ports?
Thank you for your help.
Best Regards.
If your program is a GUI then it is a good idea to run the serial port code in a separate worker thread. ReadFile can take a long time to get serial data and this would block the GUI message processing if it was done in the main thread. To provide notification events from the serial thread to the main (GUI) thread you can use PostMessage with a user-defined message. An example of doing this is at
http://vcfaq.mvps.org/mfc/12.htm

Programming a motor connected to computer through CAN communication? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a motor connected to my computer, which is connected through CAN to the motor. Basically, I have a USB-to-CAN adapter, to which I connect a USB cable from my computer. Then, from the adapter, the motor is connected through CAN.
Now, I wish to send data to the motor- I already know what sequence of bytes I need to send, but I'm not sure what commands to use to "talk" to my motor that is connected through CAN. I have been able to send data by direct USB connection from my computer to motor (using the WriteFile command in C++), but this method does not work for CAN.
Are there any libraries/functions that I can use to talk to my motor via CAN in C++?
Since you cannot connect a motor directly to a CAN bus, there must be some sort of motor controller, drive or I/O controller between the bus. Details of this device are required in order to advise on a more than fundamental level.
CAN itself does not define an application layer protocol, and several application protocols exist for CAN, such as CANopen, DeviceNet and SDS. Your device may use such a protocol or possibly something entirely proprietary. Your starting point should be the documentation for your I/O device.
For testing, most PC CAN adapter manufacturers will provide some sort of debug or development tool that allows you to construct and send individual messages and message sequences at a low-level; such a tool will allow you to verify the operation of the bus and I/O device.

Decrease latency for TCP stack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I have a PC(A) that receive data via TCP and need forward them to other PC(B)server.
The problem is PC(B) server can open only few sessions about 5 and we need to connect it to about 100 clients and we want to do it via PC(A) that read data and add id to data and forward it to server.
for do that I write a simple tcp server(run on PC A), that read data from TCP stack and forward it to PC(B)
I accept the connection on an open socket and forward it to PC(B).
In this PC(A) take about 200 μs to do this data forwarding. I have 3 GHZ * 10 and I use fedora OS, my data is so small.
How I can decrease this latency?
Do I need to change my OS or make any optimization that decreases this latency?

Microphone Specific Signal to activate interupt [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
A heads up, i am very new to audio so please bear with me =)
I am trying to interpret audio signals into an AVR (its a classical myAVR MK2 board). Now normally, the interupt signal is always some kind of switch. So if i press this swich, go into that interupt.
My goal is to interpret audio signals via microphone into the board, and have the board react to it. My first question is, when sending the microphone signal, do i have to put it through the A/D Converter, since technically it is an anolag signal ??
My second and more complicated question is, how would i actually interpret the audio signal coming in?
For example, if i scream "GREEN" then what ever the programm was doing should be stopped, the interupt should be called and the green LED should come on. Now the mic is preatty much always on ... how do i control so that only if GREEN is said, the interupt signal is sent. I dont want him constantly going in and out of the interupts just because someone made some noise ...
Would i have to save "GREEN" as a bit-combination somewhere and compare the incoming signal with the saved bits ... or ??
Some answers:
...do i have to put it through the A/D Converter, since technically it is an anolag signal ?
Yes, digital chips may fry when exposed to analog signals.
Be aware that you may have delay some time after starting the ADC before the signals are accurate.
how would i actually interpret the audio signal coming in?
Basically you have digital values coming in at a frequency. You will need to store those values and then analyze them. You must trade memory capacity/usage for accuracy. The more samples you take, the better your data and results; but this occupies more memory.
You will also need to filter out noise from the signal and from layered sounds.
You may get some benefits from researching on FFTs.
You should compare using "fuzzy logic" because in the real world, nothing is exact; for example your voice signal could be +/- 30 counts and still be "correct".