# 2.1 Learning sound recording

Problem

I want to record sound using HARK. This is the first time I am using HARK.

Solution

For first-time HARK users, it is best to start recording first because this is a basic function and also inevitable in running HARK online. It is convenient to use wios  if the aim is to simply record a sound. See HARK document or Recording multichannel sound for details.

Recording device

:
For a start, get a recording device. The microphone input in computers may be used, but a multichannel recording device is needed to localize or separate sounds. (see the chapter on devices in the HARK document in details).

From here, it is assumed that the device used is ALSA. Try to run the following commands: arecord -l A list of connected devices will then be displayed:

card 0: Intel [HDA Intel], device 0: AD198x Analog [AD198x Analog]
Subdevices: 2/2
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
card 1: ** [***], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0


Find the name of the device from the list. In case the device is not on the list, it means that the OS does not recognize the device. If the device is on the list, the device name will be specified by two numbers, the card number and the subdevice number.

For example, if the device used is USB Audio as shown above, the card number is #1 and the subdevice number is #0. In this case the device name will be plughw:1,0. If the device is unknown, try plughw:0,0.

Devices that supports DirectSound and ASIO can be used in Windows. A list of devices that can be used in HARK can be checked through: [Start]   – [Programs]   – [HARK]   – [Sound Device List]

It can also be checked by executing the following in the command prompt:

SoundDeviList

For devices that support DirectSound, the parameters of AudioStreamFromMic DEVICETYPE should be set to “DS“ and DEVICE to the device name. Although multibyte string device name is not supported, a part of the device name is searched so it is possible to input only a part of the device name.

For devices that supports ASIO, install the “ASIO Plugin for HARK“ and use the AudioStreamFromASIO node. Similar to the AudioStreamFromMic, input the device name in the parameter.

If the AudioStreamFromASIO is not found in the Node List of HARK Designer, select the libasio-plugin.def from the packages in [Preference] – [Packages].

Building a recording network

:
Next is to build a recording network using HARK-Designer. The topology of the network is the same as shown in Fig.2.2 and 2.3. Although you can leave almost all parameters to their default settings, it is also possible to change the parameters as shown in 2.1.

Check the specifications of the device to determine the number of channels and the sampling frequency. Identifying the device name is explained in the section above. The number of recording frames will depend on the recording time of the sound. This can be calculated using the equation:

 $\displaystyle Recording~ time = (LENGTH + (frames- 1) * ADVANCE)/SAMPLING\_ RATE$ (1)

where the uppercase variables are the parameters of AudioStreamFromMic . For example, if the recording time is 5 seconds at 16kHz sampling frequency with the LENGTH and ADVANCE set to default, the number of frames will be 498 because:

 $\displaystyle 5[sec]$ $\displaystyle =$ $\displaystyle (512+(frames - 1) * 160)/16000$ (2)
Table 2.1: Parameters to configure
 Node name Parameter name Type Meaning Iterate MAX_ITER Number of frames to record(498) CHANNEL_COUNT Number of channels you use(8) SAMPLING_RATE Sampling rate(16000) DEVICETYPE Type of device(ALSA) DEVICE Device name(plughw:0,0)
Running the network

:
Now, Execute the network. Multiple files such as sep_0.wav, sep_1.wav,..., will be generated with the number of files being the same as the number of channels. Play the files using audio players such as aplay

    aplay sep_0.wav

Troubleshooting

:
If the recording is successful, congratulations! But if not, check the problem by following the instructions below, or consult the recipe here: Sound recording fails.

Microphone connection:

Is the microphone connected properly? Try to unplug and plug in again the microphone.

Plug-in power:

Does the microphone need plug-in (external) power? Check if the battery has enough power and if the switch is on.

Use other recording software:

Try to record by using other recording software such as wavesurfer or audacity. If it still fails, check the configuration of the OS, the driver, or the device itself.

Device name:

If the computer is connected to multiple sound devices, confirm the device name of the recording device used. Then check again using arecord -l or try other device names.

Discussion

SaveWavePCM   is the easiest way to record a sound. However, SaveRawPCM can be used to save waveform without headers. SaveRawPCM is saved as a raw file with 16 bit little-endian format and the file extension is “.sw” Fig. 2.4 shows the subnetwork using the node.

Raw file can be read using programming languages such as python modules numpy  and pylab :


import numpy, pylab
waveform = numpy.fromfile("sep_0.sw", numpy.int16)
pylab.plot(waveform)
pylab.show()


Raw file can be played using aplay:

        aplay -c 1  -r 16000 -f S16_LE sep_0.sw


Raw files (.sw) can be converted to a wave file by using sox. For example, to convert sep_0.sw recorded with 16bit, 16kHz sampling, run the following command to get sep_0.wav

    sox -r 16000 -c 1 --bits 16 -s sep_0.sw sep_0.wav