18.11 Exercise: Transmission of AudioThis section extends the UICI server and client of Program 18.1 and Program 18.3 to send audio information from the client to the server. These programs can be used to implement a network intercom, network telephone service, or network radio broadcasts, as described in Chapter 21. Start by incorporating audio into the UICI server and client as follows.
The program sends even if no one is talking because once the program opens the audio device, the underlying device driver and interface card sample the audio input at a fixed rate until the program closes the file. The continuous sampling produces a prohibitive amount of data for transmission across the network. Use a filter to detect whether a packet contains voice, and throw away audio packets that contain no voice. A simple method of filtering is to convert the u-law (m-law) data to a linear scale and reject packets that fall below a threshold. Program 18.15 shows an implementation of this filter for Solaris. The hasvoice function returns 1 if the packet contains voice and 0 if it should be thrown away. Incorporate hasvoice or another filter so that the client does not transmit silence. Program 18.15 hasvoice.cA simple threshold function for filtering data with no voice. #include <stdio.h> #include <stdlib.h> #include "/usr/demo/SOUND/include/multimedia/audio_encode.h" #define THRESHOLD 20 /* amplitude of ambient room noise, linear PCM */ /* return 1 if anything in audiobuf is above THRESHOLD */ int hasvoice(char *audiobuf, int length) { int i; for (i = 0; i < length; i++) if (abs(audio_u2c(audiobuf[i])) > THRESHOLD) return 1; return 0; } Write the following enhancements to the basic audio transmission service. Develop a calibration function that allows the threshold for voice detection to be adjusted according to the current value of the ambient room noise. Use more sophisticated filtering algorithms in place of simple thresholds. Keep track of the total number of packets and the actual number of those that contain voice data. Display the information on standard error when the client receives a SIGUSR1 signal. Add volume control options on both client and server sides. Design an interface for accepting or rejecting connections in accordance with sender information. Devise protocols analogous to caller ID and call-waiting. Add an option on the server side to record the incoming audio to a file for later playback. Recording is easy if the client is sending all the packets. However, since the client is sending only packets with voice, straight recording does not sound right on playback because all silences are compressed. Keep timing information as well as the audio information in the recorded data. |