Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Wednesday, June 25, 2025

Radio Silence

There is a technique used to shorten radio commercials that I have a physical reaction to. Well, to be precise, it is the result that causes my reaction not the technique.

Radio commercials typically remove silence and dead space (including breaths between phrases) from an audio track. If you pay close enough attention to most commercials you will notice that the voice actor will talk for an inordinate amount of time without breathing. This gives me the sensation of being underwater and needing to take a breath - I want to hear the person take a breath so much that my mind causes a sensation as if I am also unable to breathe.

Naturally, because of that sensation I want to understand a bit about this process. In particular, is there something in the approach that can be adjusted to dampen the unnatural experience of a human not taking a breath for an extended period.

Experimentation

The approach I settled on was to do two passes over an audio file: the first would identify the portions of the track that had gaps of 'silence'; the second pass would cut those sections from the audio. In this case silence is defined using a combination of amplitude and duration.

I experimented with using a rolling average of the amplitude but that led to very choppy results and ended up being much harder to control than simply examining the instantaneous value at each sample.

After processing, the sections were partitioned similar to the following image where anything under the blue envelop is captured in the final version of the track.

Results

Being too aggressive on what constitutes silence (i.e. setting the amplitude threshold too high) results in an almost distorted track.

Original

Distorted

By adjusting amplitude along with the duration yields a better result but the sound is still a bit choppy. In particular, words that end with an 's' sound tend to get clipped too early. In addition to the amplitude and duration I ended up adding a buffer on either side of the silent section that would still be included in the clipped audio. This struck a balance between the overall amount of silence removed and continuity of the final track.

Balanced

Removal of extended pauses is somewhat easier as there is literally no sound, but addressing breaths is different in that there is sound in the track we just do not want it to register as loud enough to be included in the final track.

This is the opposite of the situation of words ending in an 's' sound. In that case we still wanted the sound included and it was being clipped; in this case we dont want the sound. It turns out - if the breaths are quiet enough - amplitude is sufficient to filter them out. Louder breaths would certainly need an alternate approach.

Breath Example (original)

Breath Removed

I've not yet determined if any of this resolves the situation I described at the beginning of the post. I anticipate I would need much longer samples of audio to test with. In either case, the tooling I built is flexible enough to adjust for that situation and is a working prototype for answering that question.

Note

While this was a fun experiment to better understand the techniques involved, I would suggest that anyone looking to do this on a more professional level use one of the existing tools that already supports silence removal out-of-the-box. Audacity, Audition, and Studio One all have support for silence detection. There are varied features across the tools for automating tasks based on the detected silent sections (ranging from manual to options for fade in and fade out transitions).

Sunday, October 5, 2014

Automating keystrokes via evdev

In a previous post I talked about how to capture keys out from under the X11 windowing system by reading from /dev/input/eventX. These character devices can also be useful to generate input simulating keyboard activity.

I circled back to this topic after having to automate user keyboard activity. I've accomplished similar tasks in the past with a tool named xdotool - unfortunately, in this case I did not have the luxury of being able to install software. The remainder of this post highlights the differences between consuming and producing events. (By the way, If you have the need to automate X actions I highly suggest looking at what xdotool can do for you.)

Consuming events is the easier of the two tasks: you simply read open the device and read events into the following structure:

/* See: /usr/include/linux/input.h */
struct input_event {
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};      

Filter input with type == 1 and read the code to get the key and value to get the event (eg. press, release).

To produce a compliant event the process is a little more complicated since the input needs to be synchronized. For each event there are three distinct sets of data that are required: setup (EV_MSC); the event (EV_KEY); and event synchronize (EV_SYN). In addition to that, certain events are captured over time so this is a stateful process. An example of this is pressing Ctrl-L; the control key is held down while another key is pressed and then released.

The easiest way I found to initially grok the protocol is to capture all events while there is keyboard activity and see what the output looks like. Obviously, to produce fully compliant input you should consult API documentation or source code.

An example of automatically entering a URL in the Chrome browser (Ctrl-L [URL]) would require the following inputs (the type, code, and value members of struct input_event). The input goes to the focused window (the standard behavior for X) so you need to place focus on the Chrome window for the following example.

4,  4, 29  # Setup  
1, 29,  1  # Press Ctrl key
0,  0,  0  # Sync
4,  4, 29  # Setup  
1, 29,  2  # Ctrl (value == 2 -> autorepeat)
0,  0,  0  # Sync
4,  4, 38
1, 38,  1  # Press 'L' key
0,  0,  0
4,  4, 38
1, 38,  0  # Release 'L' key
0,  0,  0
4,  4, 29
1, 29,  0  # Release Ctrl key
0,  0,  0
  
# and so on for the URL string 

4,  4, 28
1, 28,  1  # Press Enter key
0,  0,  0
4,  4, 28
1, 28,  0  # Release Enter key
0,  0,  0