Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automatic Gain Control in Python?

571 views
Skip to first unread message

Steve GS

unread,
May 28, 2022, 6:16:09 PM5/28/22
to
I have an extensive Excel/VBA program that hourly calls and plays podcasts
through a "smart" speaker. The output of the speaker feeds into another
computer that records the m\audio using Audacity.

It has become obvious that NPR does not regulate volumes for podcasts and
broadcasts nor are programs in themselves regulated by volume. Audacity
does not have an AGC.

It has also been noted that Excel/VBA code cannot see the audio being played
on the same computer.

I would like to find code that will regulate the volume and give some
semblance of control/moderation. Also, sometimes the Smart Speaker fails to
play the program and I get an hour of silence before the next command to
play happens. The code should detect that nothing is playing and send the
command to the smart speaker again.

Is there any Python code that I might be able to call from VBA that will
monitor and regulate the volume of the audio? A few samples of code that can
read/modify the audio will help me develop the final product.

Suggestions appreciated.




Richard Damon

unread,
May 28, 2022, 6:53:36 PM5/28/22
to
My first thought is you are solving the wrong problem. What seems a better option would be to get your code to actually connect up to the podcast and just download the audio directly, rather than trying to get the smart speaker to play the audio and record it with a microphone. That might require finding the API for the site that hosts the podcasts, to get it to send the files to you to "play".

Once you have the files, it becomes simple signal processing to go over the files and AGCing them as needed.

On a side note, make sure you are within your rights within Copyright law for what you are doing. Recording for PERSONAL use is probably within the bounds of "Fair Use", but the material is surely under Copyright, so be careful what you do with it.

--
Richard Damon

Steve GS

unread,
May 28, 2022, 8:17:26 PM5/28/22
to
"My first thought is you are solving the wrong problem. What seems a better
option would be to get your code to actually connect up to the podcast and
just download the audio directly, rather than trying to get the smart
speaker to play the audio and record it with a microphone."

The smart-speaker is bringing in the podcast by hourly automated commands
and sending by audio cable to a computer which is recording it with
Audacity. This is an automated system that runs for 48 hours every weekend.
Its output is played live throughout the facility and is also recorded for
replay through the week.

No download to use.

AGC is to happen when the Smart Speaker is playing it, real time.
Any post-record editing would be a horrendous task to say the least.

=============================

Genie: You have three wishes.
Me: I wish I had more wishes.
Genie: You cannot wish for more wishes.
Me: I wish I could.
--
https://mail.python.org/mailman/listinfo/python-list

MRAB

unread,
May 28, 2022, 8:57:57 PM5/28/22
to
On 2022-05-29 01:17, Steve GS wrote:
> "My first thought is you are solving the wrong problem. What seems a better
> option would be to get your code to actually connect up to the podcast and
> just download the audio directly, rather than trying to get the smart
> speaker to play the audio and record it with a microphone."
>
> The smart-speaker is bringing in the podcast by hourly automated commands
> and sending by audio cable to a computer which is recording it with
> Audacity. This is an automated system that runs for 48 hours every weekend.
> Its output is played live throughout the facility and is also recorded for
> replay through the week.
>
> No download to use.
>
> AGC is to happen when the Smart Speaker is playing it, real time.
> Any post-record editing would be a horrendous task to say the least.
>
[snip]
Why would post-record editing be "horrendous"?

Does it record the whole 48 hours into 1 file?

If it's recording each podcast separately, it could process each one
after recording it, even while the next one is being recorded, and I
really doubt that processing each one while take long.

Steve GS

unread,
May 28, 2022, 9:23:23 PM5/28/22
to
>> Why would post-record editing be "horrendous"?

This has to be done on-the-fly before it is recorded.
After the AGC is applied, it will be played, live, to the community.
It is played during the week to a much smaller audience, almost as
background noise.
Post recording editing would be a waste of time and worthless.

>> Does it record the whole 48 hours into 1 file?

Two files, 24 hours each, one for Saturday, the other Sunday


-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga....@python.org> On
Behalf Of MRAB
Sent: Saturday, May 28, 2022 8:57 PM
To: pytho...@python.org
Subject: Re: Automatic Gain Control in Python?

--
https://mail.python.org/mailman/listinfo/python-list

Steve GS

unread,
May 29, 2022, 1:09:51 AM5/29/22
to
You really need to understand what I am trying to do.
It is not a simple lesson in use of podcasts.
This is an automated system. I call it my NPR Jukebox.

15 years ago, I started with hourly URL calls to a browser to record specific NPR programs. It took a lot of coordination. I had to use IE because I needed to start and stop browsers on the hour and IE was the only one that could do that. Then web sites started "upgrading" to Edge. Through some trickery I was able to get Edge to do what IE did but it was unstable.

I then discovered the Echo Smart Speaker. I set my program to announce the broadcast station or podcast by speaker to the smart speaker and it cured a lot of headaches. I then was able to call podcasts because the Echo handles them through TuneIn. Unfortunately, not all broadcasts ae available as podcasts.

I am not here diddling around just playing podcasts. Let me repeat what I have already said. It is an automated system. Every hour for 48 hours on every weekend, my system using a well-developed Excel/VBA program that vocally talks to the SS hourly. The SS finds the audio and sends it to my Audacity recorder on another computer through aux-out to mic-in cable. The selections of audio are also transmitted to the community at the time of recording

That part of the system is almost flawless, well compared to that I had before. Although the quality, tone, and timing of the announcement, the SS still gets confused once in a while and goes silent for the hour. I need to detect this too.

Ok, now back to the original question.

Podcasts and broadcasts apparently do not use the Dolby tone to balance the audio levels. And I receive highly fluctuating levels of audio. Sometimes it is between sides of a conversation, sometimes it is the podcast vs station identifications, then it is great differences between one web site and another. Then there's the differences with male and female voices. Imagine that you are watching TV late night then the commercials COME IN AT FULL BLAST.

The technology of the industry grew up with male voices and apparently sees no reason to adjust for female. I have worked with audio systems and making recordings for more years that I want to admit.

All I want is software to detect level changes over time and attempt to equalize them.
It has to be work between the SS and the recorder and is to be checking all the time.

The code is to: Listen to the audio level for about 10 seconds or so and raise or lower the level in small increments.
It has nothing to do with understanding how to grab podcasts. The system is working very well for that.


Footnote:
“What rhymes with orange?”
“No, it doesn’t..”



-----Original Message-----
From: Richard Damon <richar...@gmail.com> On Behalf Of Richard Damon
Sent: Saturday, May 28, 2022 11:37 PM
To: Steve GS <Gron...@SGA.Ninja>
Subject: Re: Automatic Gain Control in Python?

On 5/28/22 8:17 PM, Steve GS wrote:
> "My first thought is you are solving the wrong problem. What seems a
> better option would be to get your code to actually connect up to the
> podcast and just download the audio directly, rather than trying to
> get the smart speaker to play the audio and record it with a microphone."
>
> The smart-speaker is bringing in the podcast by hourly automated
> commands and sending by audio cable to a computer which is recording
> it with Audacity. This is an automated system that runs for 48 hours every weekend.
> Its output is played live throughout the facility and is also recorded
> for replay through the week.
>
> No download to use.
>
> AGC is to happen when the Smart Speaker is playing it, real time.
> Any post-record editing would be a horrendous task to say the least.
>
My guess is you don't understand how "Podcasts" work. All they are is a web resource that your Browser/Smart Device makes a request off, and the contents are streamed over the internet to that device, which then plays it. Smart Speakers just have a program that knows how to access these.

Since they can be listened to on a web browser, a program can download the data. They might be doing things to make this harder, but that is a sign that you shouldn't be doing this in the first place.

Often, the data format that is streamed is exactly like the file format for storing an audio file (since that is what the code in the browser is built to handle).

It may be a bit of work to figure out the access methods to get the data, but this is the sort of job that computers were designed to do.
Trying to make things that weren't designed to be remote controlled to be remote controlled may well be a lot more work.

--
Richard Damon

Christian Gollwitzer

unread,
May 29, 2022, 3:23:05 AM5/29/22
to
Am 29.05.22 um 00:45 schrieb Stefan Ram:
> "Steve GS" <Gron...@SGA.Ninja> writes:
>> Subject: Automatic Gain Control in Python?
>
> Automatic Gain Control in Python is trivial. You have a list
> of samples and normalize them, i.e., divide by max. Slightly
> simplified
>
> [ s/max( samples )for s in samples ]
>
> (where sample values are normalized to the range -1,+1.)

No, that's not it. Loudness is perceived in a different way, the crudest
approximation is the standard deviation averaged over small frames,
better measures take into account that the ear has a strongly frequency
dependent response. For similar sound files, like podcasts which are
voice-heavy, the stddev works reasoably well.

If the normalization only reduces the volume, then dividing by the max
is sufficient. However if you also want to raise the volume, then you
need dynamic range compression. If you have ever tried to record some
music with a simple microphone and a computer, you would have noticed
that the recording is very soft, when normalized to the max. Commercial
music is incredibly loud, and you might have wondered, how they do that.

Google for "Loudness war" and "dynamic range compression" if you want to
understand it in detail.

Christian


Martin Schöön

unread,
May 29, 2022, 5:20:02 AM5/29/22
to
Den 2022-05-29 skrev Christian Gollwitzer <auri...@gmx.de>:
> Am 29.05.22 um 00:45 schrieb Stefan Ram:
>> "Steve GS" <Gron...@SGA.Ninja> writes:
>>> Subject: Automatic Gain Control in Python?
>>
>> Automatic Gain Control in Python is trivial. You have a list
>> of samples and normalize them, i.e., divide by max. Slightly
>> simplified
>>
>> [ s/max( samples )for s in samples ]
>>
>> (where sample values are normalized to the range -1,+1.)
>
> No, that's not it. Loudness is perceived in a different way, the crudest

<snip>

> music is incredibly loud, and you might have wondered, how they do that.
>
> Google for "Loudness war" and "dynamic range compression" if you want to
> understand it in detail.
>
I have no suggestions for solving the problem but it struck me that
you might be interested in looking up a standard called EBU R128.
Start with youtube and you find lectures/demos.

Python connection; There is a Python package called ffmpeg-normalize
which contains an implementation of EBU R128. AFAIK it works on files,
not streaming audio.

/Martin

Richard Damon

unread,
May 29, 2022, 8:03:00 AM5/29/22
to
From your description, your fundamental problem is you are trying to
automatically "control" things that weren't designed to be automatically
controlled in the way you are attempting.

The smart speaker assumes the "user" will adjust the volume either with
the controls or with verbal commands, So things will be a bit "clunky"
in your results as you command the smart speaker volume level.

Yes, you have an automated system that does most of what you want, but
it is based on pieces not designed to be automated in this way, and you
are running into the limitations caused by that.

Yes, you could split the aux-out to bring it into another computer to
listen to the sound level, and then using a sound input package get
samples of what is playing, and analyze that data to get an average
volume, and then issues the command to adjust the volume level.

What you seem to be missing is that you could get the podcasts from a
browser, and all a browser is is a program. It isn't that much work to
write a rudimentary browser in python, especially if you don't actually
need to display the results to a user, but are only trying to automate a
particular task.

You seem to feel strongly invested in your current code base, which is
understandable, but it seems you have reached a point where you don't
want to live with the limitations CAUSED by that system. Yes, there is
likely a way to tack on another layer of "stuff" to adjust for this
issue, but it likely is going to require some real programming.

It may well be the design I am suggesting, of writing a program to fetch
the podcast and save it requires a bit more work to get to the level you
currently are at, but the results are a system that is actually designed
to be controlled by automation. Maybe it is beyond you ability, but then
so might the programming to get the volume.

I will also add, that the way your describe going to your "community"
gives me questions if this is a violation of copyright. Maybe it is
something you can "Get away with", but I am not sure what you are doing
is actually legitimate.
--
Richard Damon

Steve GS

unread,
May 29, 2022, 11:04:42 AM5/29/22
to
>> From your description, your fundamental problem is you are trying to automatically "control" things that weren't designed to be automatically controlled in the way you are attempting.

How so? I am sending commands to a smart speaker and it plays podcasts and broadcasts.
How is this a violation of SS design?

===================
>> The smart speaker assumes the "user" will adjust the volume either with the controls or with verbal commands, So things will be a bit "clunky" in your results as you command the smart speaker volume level.

So, you want me to sit here for every hour of the weekend and monitor the audio levels for a result that will get, at best, one replay when I believe it can be automated.

===================
>> Yes, you have an automated system that does most of what you want, but it is based on pieces not designed to be automated in this way, and you are running into the limitations caused by that.

Again, what limitations of the SS am I violating? It is designed to receive commands and play the audio.
Also, what makes you think that you know how my program is based?

===================
>> Yes, you could split the aux-out to bring it into another computer to listen to the sound level, and then using a sound input package get samples of what is playing, and analyze that data to get an average volume, and then issues the command to adjust the volume level.

Hmmm, is that not my original question? Are you suggesting to monitor the audio, sense it for volume changes and apply those changes to the original audio? One thing that may have to happen is a timed-delay to all for the AGC to work. This may require a correlation circuit.

==================
>> What you seem to be missing is that you could get the podcasts from a browser, and all a browser is is a program. It isn't that much work to write a rudimentary browser in python, especially if you don't actually need to display the results to a user, but are only trying to automate a particular task.

Writing my own browser in Python might work. Do you have a sample one that I could twerk to fit my needs?
I would have to be able to invoke it and an hour later devoke it least I end up with multiple audio channels playing.

Either way, I would still need an AGC program which was my original question.

===================
>> You seem to feel strongly invested in your current code base, which is understandable, but it seems you have reached a point where you don't want to live with the limitations CAUSED by that system.

The changes in volume are not CAUSED by my program. The want to fix them is a new development to improve the product. The volume fluctuations are causes, or neglections, by the engineers at the sources of podcasts and broadcasts.

>> Yes, there is likely a way to tack on another layer of "stuff" to adjust for this issue, but it likely is going to require some real programming.

>> It may well be the design I am suggesting, of writing a program to fetch the podcast and save it requires a bit more work to get to the level you currently are at, but the results are a system that is actually designed to be controlled by automation. Maybe it is beyond you ability, but then so might the programming to get the volume.

"Real programming"?, REAL PROGRAMMING? Well at this I have to say "Well duh". I have more than 40 years of programming in languages including assembly, Pascal, C, C++, BASIC, Visual BASIC, COBOL, VBA and Python for starters. I even dipped into AT&T's SNOBAL. Starting in the mid-80s, I taught Visual BASIC and VBA for 15 years in high school and at the University level.

I have 2000 lines of code of Python that helped me to bring my A1c reading from 9.0 to 6.1 thank you. I have a program of similar size that tracks the 450 specific plants in my garden. It even monitors degree-days to help me know when certain insects attack and when to tend to individual needs. This Excel/Jukebox program is also about 2000 lines of code. (Seems to be a pattern here). All of these programs are decorated with numerous pops and whistles that make programming and use quite pleasurable,

So, do you I think I am now ready for some "real programming".

===================
>> I will also add, that the way your describe going to your "community"
gives me questions if this is a violation of copyright. Maybe it is something you can "Get away with", but I am not sure what you are doing is actually legitimate.

Yes, I have been through that. It is totally legal to record NPR broadcasts for replay as long as they are not retained for than a month or for multiple replays. Your suggestion to download and play a podcast or broadcast is legal only for live replay. My want to record them for one replay for my own use. Personal play is a different story.

Benjamin Schollnick

unread,
May 29, 2022, 11:18:12 AM5/29/22
to
Okay, you are capturing the audio stream as a digital file somewhere, correct?

Why not just right a 3rd party package to normalize the audio levels in the digital file? It’ll be faster, and probably easier than trying to do it in real time…

eg. https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8 <https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8>

Normalizing an audio file with PyDub

Sometimes you'll have audio files where the speech is loud in some portions and quiet in others. Having this variance in volume can hinder transcription.

Luckily, PyDub's effects module has a function called normalize() which finds the maximum volume of an AudioSegment, then adjusts the rest of the AudioSegment to be in proportion. This means the quiet parts will get a volume boost.

You can listen to an example of an audio file which starts as loud then goes quiet, loud_then_quiet.wav, here <https://assets.datacamp.com/production/repositories/4637/datasets/9251c751d3efccf781f3e189d68b37c8d22be9ca/ex3_datacamp_loud_then_quiet.wav>.

In this exercise, you'll use normalize() to normalize the volume of our file, making it sound more like this <https://assets.datacamp.com/production/repositories/4637/datasets/f0c1ba35ff99f07df8cfeee810c7b12118d9cd0f/ex3_datamcamp_normalized_loud_quiet.wav>.

or

https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python <https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python>


- Benjamin
> --
> https://mail.python.org/mailman/listinfo/python-list

Michael F. Stemper

unread,
May 29, 2022, 11:44:32 AM5/29/22
to
On 29/05/2022 10.04, Steve GS wrote:

>>> What you seem to be missing is that you could get the podcasts from a browser, and all a browser is is a program. It isn't that much work to write a rudimentary browser in python, especially if you don't actually need to display the results to a user, but are only trying to automate a particular task.
>
> Writing my own browser in Python might work. Do you have a sample one that I could twerk to fit my needs?
> I would have to be able to invoke it and an hour later devoke it least I end up with multiple audio channels playing.

Somebody has already shown the rudiments of urllib. Another option to consider
is the use of something like curl or wget to download the podcasts, which can
be automated separately from your replay program.

--
Michael F. Stemper
This email is to be read by its intended recipient only. Any other party
reading is required by the EULA to send me $500.00.

MRAB

unread,
May 29, 2022, 1:27:51 PM5/29/22
to
On 2022-05-29 16:17, Benjamin Schollnick wrote:
> Okay, you are capturing the audio stream as a digital file somewhere, correct?
>
> Why not just right a 3rd party package to normalize the audio levels in the digital file? It’ll be faster, and probably easier than trying to do it in real time…
>
> eg. https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8 <https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8>
>
> Normalizing an audio file with PyDub
>
> Sometimes you'll have audio files where the speech is loud in some portions and quiet in others. Having this variance in volume can hinder transcription.
>
> Luckily, PyDub's effects module has a function called normalize() which finds the maximum volume of an AudioSegment, then adjusts the rest of the AudioSegment to be in proportion. This means the quiet parts will get a volume boost.
>
> You can listen to an example of an audio file which starts as loud then goes quiet, loud_then_quiet.wav, here <https://assets.datacamp.com/production/repositories/4637/datasets/9251c751d3efccf781f3e189d68b37c8d22be9ca/ex3_datacamp_loud_then_quiet.wav>.
>
> In this exercise, you'll use normalize() to normalize the volume of our file, making it sound more like this <https://assets.datacamp.com/production/repositories/4637/datasets/f0c1ba35ff99f07df8cfeee810c7b12118d9cd0f/ex3_datamcamp_normalized_loud_quiet.wav>.
>
> or
>
> https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python <https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python>
>
>
[snip]

Here's a sample script that uses pyaudio instead of Audacity.

You can check whether the podcast is playing by checking the volume soon
after it should've started.

Pyaudio can also read and write files.


import pyaudio
import time
import numpy as np

WIDTH = 2
CHANNELS = 2
RATE = 44100
MAX_VOL = 1024

GAIN_STEP = 0.2
LOUDER = 1 + GAIN_STEP
QUIETER = 1 - GAIN_STEP

gain = 1

p = pyaudio.PyAudio()

def callback(data, frame_count, time_info, status):
global gain

# Decode the bytestream
chunk = np.frombuffer(data, dtype=np.int16)

# Adjust the volume.
chunk = (chunk.astype(np.double) * gain).astype(np.int16)

# Adjust the gain according to the current maximum volume.
max_vol = max(chunk)

if max_vol < MAX_VOL:
gain *= LOUDER
elif max_vol > MAX_VOL:
gain *= QUIETER

return (chunk.tobytes(), pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS,
rate=RATE, input=True, output=True, stream_callback=callback)

stream.start_stream()

while stream.is_active():
time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()

Benjamin Schollnick

unread,
May 29, 2022, 2:33:11 PM5/29/22
to
Well, maybe a different approach might be better?

https://www.amazon.com/LEMONKTV-Control-Device-Karaoke-Machine/dp/B07H49JB8S <https://www.amazon.com/LEMONKTV-Control-Device-Karaoke-Machine/dp/B07H49JB8S>

I’m unclear on how bringing the audio track into a computer to examine detect the audio level, and then adjusting the volume out of the Smart Speaker is really doing more than adding complexity.

An all analog solution might be the better route, although finding something that is inexpensive might be an issue as well.

- Benjamin



> On May 29, 2022, at 11:32 AM, Steve GS <Gron...@SGA.Ninja> wrote:
>
> No, not a digital file. I am playing the podcast/broadcast live to the community using a separate network of smart speakers (Amazon Echo). Commands are being sent hourly through a speaker to the SS from an excel program. I want to monitor the audio level between the line-out of the SS and the input to another computer which then records the audio using Audacity for a single replay during the week.
>
> I think my first post should have started “Fasten your seat belts, it is going to be a bumpy night…)
>
>
> Genie: You have three wishes.
> Me: I wish I had more wishes.
> Genie: You cannot wish for more wishes.
> Me: I wish I could.
>
> From: Benjamin Schollnick <bscho...@schollnick.net <mailto:bscho...@schollnick.net>>
> Sent: Sunday, May 29, 2022 11:18 AM
> To: Steve GS <Gron...@SGA.Ninja <mailto:Gron...@SGA.Ninja>>
> Cc: Richard Damon <Ric...@Damon-Family.org <mailto:Ric...@Damon-Family.org>>; Python <pytho...@python.org <mailto:pytho...@python.org>>
> Subject: Re: Automatic Gain Control in Python?
>
> Okay, you are capturing the audio stream as a digital file somewhere, correct?
>
> Why not just right a 3rd party package to normalize the audio levels in the digital file? It’ll be faster, and probably easier than trying to do it in real time…
>
> eg. https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8 <https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8>
>
>> Normalizing an audio file with PyDub
>>
>> Sometimes you'll have audio files where the speech is loud in some portions and quiet in others. Having this variance in volume can hinder transcription.
>> Luckily, PyDub's effects module has a function called normalize() which finds the maximum volume of an AudioSegment, then adjusts the rest of the AudioSegment to be in proportion. This means the quiet parts will get a volume boost.
>> You can listen to an example of an audio file which starts as loud then goes quiet, loud_then_quiet.wav, here <https://assets.datacamp.com/production/repositories/4637/datasets/9251c751d3efccf781f3e189d68b37c8d22be9ca/ex3_datacamp_loud_then_quiet.wav>.
>> In this exercise, you'll use normalize() to normalize the volume of our file, making it sound more like this <https://assets.datacamp.com/production/repositories/4637/datasets/f0c1ba35ff99f07df8cfeee810c7b12118d9cd0f/ex3_datamcamp_normalized_loud_quiet.wav>.
> or
>
> https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python <https://stackoverflow.com/questions/57925304/how-to-normalize-a-raw-audio-file-with-python>
>
>
>
> - Benjamin
>
>
>> On May 29, 2022, at 11:04 AM, Steve GS <Gron...@SGA.Ninja <mailto:Gron...@SGA.Ninja>> wrote:
>>
>>>> From your description, your fundamental problem is you are trying to automatically "control" things that weren't designed to be automatically controlled in the way you are attempting.
>>
>> How so? I am sending commands to a smart speaker and it plays podcasts and broadcasts.
>> How is this a violation of SS design?
>>
>> ===================
>>
>>>> The smart speaker assumes the "user" will adjust the volume either with the controls or with verbal commands, So things will be a bit "clunky" in your results as you command the smart speaker volume level.
>>
>> So, you want me to sit here for every hour of the weekend and monitor the audio levels for a result that will get, at best, one replay when I believe it can be automated.
>>
>> ===================
>>
>>>> Yes, you have an automated system that does most of what you want, but it is based on pieces not designed to be automated in this way, and you are running into the limitations caused by that.
>>
>> Again, what limitations of the SS am I violating? It is designed to receive commands and play the audio.
>> Also, what makes you think that you know how my program is based?
>>
>> ===================
>>
>>>> Yes, you could split the aux-out to bring it into another computer to listen to the sound level, and then using a sound input package get samples of what is playing, and analyze that data to get an average volume, and then issues the command to adjust the volume level.
>>
>> Hmmm, is that not my original question? Are you suggesting to monitor the audio, sense it for volume changes and apply those changes to the original audio? One thing that may have to happen is a timed-delay to all for the AGC to work. This may require a correlation circuit.
>>
>> ==================
>>
>>>> What you seem to be missing is that you could get the podcasts from a browser, and all a browser is is a program. It isn't that much work to write a rudimentary browser in python, especially if you don't actually need to display the results to a user, but are only trying to automate a particular task.
>>
>> Writing my own browser in Python might work. Do you have a sample one that I could twerk to fit my needs?
>> I would have to be able to invoke it and an hour later devoke it least I end up with multiple audio channels playing.
>>
>> Either way, I would still need an AGC program which was my original question.
>>
>> ===================
>>
>>>> You seem to feel strongly invested in your current code base, which is understandable, but it seems you have reached a point where you don't want to live with the limitations CAUSED by that system.
>>
>> The changes in volume are not CAUSED by my program. The want to fix them is a new development to improve the product. The volume fluctuations are causes, or neglections, by the engineers at the sources of podcasts and broadcasts.
>>
>>
>>>> Yes, there is likely a way to tack on another layer of "stuff" to adjust for this issue, but it likely is going to require some real programming.
>>
>>
>>>> It may well be the design I am suggesting, of writing a program to fetch the podcast and save it requires a bit more work to get to the level you currently are at, but the results are a system that is actually designed to be controlled by automation. Maybe it is beyond you ability, but then so might the programming to get the volume.
>>
>> "Real programming"?, REAL PROGRAMMING? Well at this I have to say "Well duh". I have more than 40 years of programming in languages including assembly, Pascal, C, C++, BASIC, Visual BASIC, COBOL, VBA and Python for starters. I even dipped into AT&T's SNOBAL. Starting in the mid-80s, I taught Visual BASIC and VBA for 15 years in high school and at the University level.
>>
>> I have 2000 lines of code of Python that helped me to bring my A1c reading from 9.0 to 6.1 thank you. I have a program of similar size that tracks the 450 specific plants in my garden. It even monitors degree-days to help me know when certain insects attack and when to tend to individual needs. This Excel/Jukebox program is also about 2000 lines of code. (Seems to be a pattern here). All of these programs are decorated with numerous pops and whistles that make programming and use quite pleasurable,
>>
>> So, do you I think I am now ready for some "real programming".
>>
>> ===================
>>
>>>> I will also add, that the way your describe going to your "community"
>> gives me questions if this is a violation of copyright. Maybe it is something you can "Get away with", but I am not sure what you are doing is actually legitimate.
>>
>> Yes, I have been through that. It is totally legal to record NPR broadcasts for replay as long as they are not retained for than a month or for multiple replays. Your suggestion to download and play a podcast or broadcast is legal only for live replay. My want to record them for one replay for my own use. Personal play is a different story.
>>
>>
>>
>>
>> -----Original Message-----
>> From: Richard Damon <richar...@gmail.com <mailto:richar...@gmail.com>> On Behalf Of Richard Damon
>> Sent: Sunday, May 29, 2022 8:03 AM
>> To: Steve GS <Gron...@SGA.Ninja <mailto:Gron...@SGA.Ninja>>; Python <pytho...@python.org <mailto:pytho...@python.org>>
>> Subject: Re: Automatic Gain Control in Python?
>>
>> From your description, your fundamental problem is you are trying to automatically "control" things that weren't designed to be automatically controlled in the way you are attempting.
>>
>> The smart speaker assumes the "user" will adjust the volume either with the controls or with verbal commands, So things will be a bit "clunky"
>> in your results as you command the smart speaker volume level.
>>
>> Yes, you have an automated system that does most of what you want, but it is based on pieces not designed to be automated in this way, and you are running into the limitations caused by that.
>>
>> Yes, you could split the aux-out to bring it into another computer to listen to the sound level, and then using a sound input package get samples of what is playing, and analyze that data to get an average volume, and then issues the command to adjust the volume level.
>>
>> What you seem to be missing is that you could get the podcasts from a browser, and all a browser is is a program. It isn't that much work to write a rudimentary browser in python, especially if you don't actually need to display the results to a user, but are only trying to automate a particular task.
>>
>> https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>

Steve GS

unread,
May 29, 2022, 2:33:12 PM5/29/22
to
No, not a digital file. I am playing the podcast/broadcast live to the community using a separate network of smart speakers (Amazon Echo). Commands are being sent hourly through a speaker to the SS from an excel program. I want to monitor the audio level between the line-out of the SS and the input to another computer which then records the audio using Audacity for a single replay during the week.

I think my first post should have started “Fasten your seat belts, it is going to be a bumpy night…)





Genie: You have three wishes.

Me: I wish I had more wishes.
Genie: You cannot wish for more wishes.
Me: I wish I could.



From: Benjamin Schollnick <bscho...@schollnick.net>
Sent: Sunday, May 29, 2022 11:18 AM
To: Steve GS <Gron...@SGA.Ninja>
Cc: Richard Damon <Ric...@Damon-Family.org>; Python <pytho...@python.org>
Subject: Re: Automatic Gain Control in Python?



Okay, you are capturing the audio stream as a digital file somewhere, correct?



Why not just right a 3rd party package to normalize the audio levels in the digital file? It’ll be faster, and probably easier than trying to do it in real time…



eg. https://campus.datacamp.com/courses/spoken-language-processing-in-python/manipulating-audio-files-with-pydub?ex=8




Normalizing an audio file with PyDub


Sometimes you'll have audio files where the speech is loud in some portions and quiet in others. Having this variance in volume can hinder transcription.

Luckily, PyDub's effects module has a function called normalize() which finds the maximum volume of an AudioSegment, then adjusts the rest of the AudioSegment to be in proportion. This means the quiet parts will get a volume boost.

You can listen to an example of an audio file which starts as loud then goes quiet, loud_then_quiet.wav, <https://assets.datacamp.com/production/repositories/4637/datasets/9251c751d3efccf781f3e189d68b37c8d22be9ca/ex3_datacamp_loud_then_quiet.wav> here.

In this exercise, you'll use normalize() to normalize the volume of our file, making it sound <https://assets.datacamp.com/production/repositories/4637/datasets/f0c1ba35ff99f07df8cfeee810c7b12118d9cd0f/ex3_datamcamp_normalized_loud_quiet.wav> more like this.

or

Grant Edwards

unread,
May 29, 2022, 3:19:02 PM5/29/22
to
On 2022-05-29, Benjamin Schollnick <bscho...@schollnick.net> wrote:

> Why not just right a 3rd party package to normalize the audio levels
> in the digital file? It’ll be faster, and probably easier than
> trying to do it in real time…

I doubt you even need to write any code to do that. Sox can normalize
audio levels in files with a single command.

http://sox.sourceforge.net/

--
Grant

Benjamin Schollnick

unread,
May 29, 2022, 3:24:28 PM5/29/22
to
> I doubt you even need to write any code to do that. Sox can normalize
> audio levels in files with a single command.

Correct.

My phrasing was slightly misleading.

There are plenty of applications that should do that. I was thinking of one that Marco Arment makes, but I can’t locate it for reference.

- Benjamin
> --
> https://mail.python.org/mailman/listinfo/python-list

Phil Boutros

unread,
May 31, 2022, 2:52:08 PM5/31/22
to
Richard Damon <Ric...@Damon-Family.org> wrote:
> From your description, your fundamental problem is you are trying to
> automatically "control" things that weren't designed to be automatically
> controlled in the way you are attempting.
<snip>
> What you seem to be missing is that you could get the podcasts from a
> browser, and all a browser is is a program. It isn't that much work to
> write a rudimentary browser in python, especially if you don't actually
> need to display the results to a user, but are only trying to automate a
> particular task.

Even easier, the few NPR podcasts I just checked now have RSS
feeds of their episodes (as expected). It seems it would be much
easier to just download the latest episode based on the XML file,
normalize, send it to play, done.

Why is it so crucial that it be done "in real time", which it
really isn't, unless you are listening to the live NPR feed. In the
case of podcasts, *why* would you possible play them via streaming
instead of downloading the publicly accessible file to manipulate
however you chose?

> SteveGS wrote:
>> The code is to: Listen to the audio level for about 10 seconds or
>> so and raise or lower the level in small increments.

Based on what? Do you want a 10 second buffer to continually be
analyzed in real time? From the output of a speaker, no less?

And which level? Peak? Average over...10 seconds? Are you
keeping track of the previous values to make it match? Where is that
stored?

>> It has nothing to do with understanding how to grab podcasts. The
>> system is working very well for that.

OK...then perhaps I/we am/are confused on the limitations of the
program. What does it "grab", *exactly*?


Phil
--
AH#61 Wolf#14 BS#89 bus#1 CCB#1 SENS KOTC#4
ph...@philb.ca http://philb.ca

Steve GS

unread,
May 31, 2022, 9:04:11 PM5/31/22
to

> Even easier, the few NPR podcasts I just checked now have RSS feeds of
their episodes (as expected). It seems it would be much easier to just
download the latest episode based on the XML file, normalize, send it to
play, done.

How can that possibly be easier? I am playing the podcast and recording it
for a one-time replay.
Now you want me to write a program that automatically downloads 48 files
then manipulate them for equalization then replay it. It certainly doesn't
sound easier to me. I already have that working using simple
computer-generated vocal commands.

> Why is it so crucial that it be done "in real time", which it really
isn't, unless you are listening to the live NPR feed. In the case of
podcasts, *why* would you possible play them via streaming instead of
downloading the publicly accessible file to manipulate however you chose?

It is played "real time" and yes, for the listeners in the community, it is
real time no matter when it was recorded.

>> The code is to: Listen to the audio level for about 10 seconds or so
>> and raise or lower the level in small increments.

> Based on what?
A simple sample rate.

> Do you want a 10 second buffer to continually be analyzed in real time?
>From the output of a speaker, no less?

Why should the source be of any concern? It is much more than a speaker. It
is pulling in the podcasts/broadcasts and playing them. The audio is also
being sent to a computer through an aux-out cable. Why is that confusing?

> And which level? Peak? Average over...10 seconds? Are you keeping
track of the previous values to make it match?

That is what correlation is all about.

>Where is that stored?

In the computer that is running the AGC.

>> It has nothing to do with understanding how to grab podcasts. The
>> system is working very well for that.

OK...then perhaps I/we am/are confused on the limitations of the
program. What does it "grab", *exactly*?

Maybe you do not understand smart speakers. That is exactly what they do.
You tell them what podcast/broadcast to play, they get it and play it for
you. It is that simple.

All I want to do is change the audio levels automatically to make it easier
on the ear.



--
AH#61 Wolf#14 BS#89 bus#1 CCB#1 SENS KOTC#4 ph...@philb.ca
http://philb.ca
--
https://mail.python.org/mailman/listinfo/python-list

Chris Angelico

unread,
May 31, 2022, 9:16:10 PM5/31/22
to
On Wed, 1 Jun 2022 at 11:05, Steve GS <Gron...@sga.ninja> wrote:
>
>
> > Even easier, the few NPR podcasts I just checked now have RSS feeds of
> their episodes (as expected). It seems it would be much easier to just
> download the latest episode based on the XML file, normalize, send it to
> play, done.
>
> How can that possibly be easier? I am playing the podcast and recording it
> for a one-time replay.
> Now you want me to write a program that automatically downloads 48 files
> then manipulate them for equalization then replay it. It certainly doesn't
> sound easier to me. I already have that working using simple
> computer-generated vocal commands.
>

General principle: If you're asking someone else for help, don't tell
them that your way is easier, because the obvious response is "go
ahead then, do it your own way".

You're technically right in a sense: something that you already have
is, indeed, easier than something else. But downloading files is
*easy* in Python, and audio analysis on files is FAR easier than
real-time audio analysis with hysteresis avoidance.

What you're doing actually reminds me of the old acoustic couplers
[1], which were a messy hack brought about by monopolies that refused
to allow other devices onto the network. Unless you have a really good
reason for sticking to the black-box system, I would strongly
recommend going for the much much easier method of simply downloading
the files as they are.

ChrisA

[1] https://en.wikipedia.org/wiki/Acoustic_coupler

MRAB

unread,
May 31, 2022, 9:47:19 PM5/31/22
to
On 2022-06-01 02:03, Steve GS wrote:
[snip]

> Maybe you do not understand smart speakers. That is exactly what they do.
> You tell them what podcast/broadcast to play, they get it and play it for
> you. It is that simple.
>
> All I want to do is change the audio levels automatically to make it easier
> on the ear.
>
Did you have a look at the code I posted?

I've found that pyaudio can listen to the Line In input at the same time
as Audacity is recording from it, so you could might be able to use it
to monitor the level and then tell the smart speaker to turn the volume
up or down.

Steve GS

unread,
May 31, 2022, 10:02:19 PM5/31/22
to
I started but major issues prevented me this weekend (New Roof, Major limb
broke for pine tree...
I still have to install pyAudio.



Genie: You have three wishes.
Me: I wish I had more wishes.
Genie: You cannot wish for more wishes.
Me: I wish I could.

-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga....@python.org> On
Behalf Of MRAB
Sent: Tuesday, May 31, 2022 9:47 PM
To: pytho...@python.org
Subject: Re: Automatic Gain Control in Python?

--
https://mail.python.org/mailman/listinfo/python-list

MRAB

unread,
May 31, 2022, 10:38:15 PM5/31/22
to
On 2022-06-01 03:01, Steve GS wrote:
> I started but major issues prevented me this weekend (New Roof, Major
limb
> broke for pine tree...
> I still have to install pyAudio.

PyPI has wheels only up to Python 3.6, but Christoph Gohlke's site has
ones for more recent versions:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

> -----Original Message-----
> From: Python-list <python-list-bounces+gronicus=sga....@python.org> On
> Behalf Of MRAB
> Sent: Tuesday, May 31, 2022 9:47 PM
> To: pytho...@python.org
> Subject: Re: Automatic Gain Control in Python?
>

Phil Boutros

unread,
Jun 6, 2022, 12:39:06 PM6/6/22
to
Chris Angelico <ros...@gmail.com> wrote:
>
> General principle: If you're asking someone else for help, don't tell
> them that your way is easier, because the obvious response is "go
> ahead then, do it your own way".

*Ding Ding Ding*...We have a winner! At least, that's where I
dropped off. My experienced advice is all wrong for you? Sorry I
couldn't help more. Good luck to you.

> You're technically right in a sense: something that you already have
> is, indeed, easier than something else. But downloading files is
> *easy* in Python, and audio analysis on files is FAR easier than
> real-time audio analysis with hysteresis avoidance.

Also this.

<snip>
> Unless you have a really good
> reason for sticking to the black-box system, I would strongly
> recommend going for the much much easier method of simply downloading
> the files as they are.

As pretty much everyone else has said. Insisting on real-time
processing of something that is itself pre-recorded is non-sensical.

Phil Boutros

unread,
Jun 6, 2022, 12:46:33 PM6/6/22
to
Steve GS <Gron...@SGA.Ninja> wrote:
>
> Maybe you do not understand smart speakers. That is exactly what they do.
> You tell them what podcast/broadcast to play, they get it and play it for
> you. It is that simple.
>
> All I want to do is change the audio levels automatically to make it easier
> on the ear.

Once again, what you are telling the speaker to play is a
pre-recorded file. Grab it. Normalize it however you want (compared
to others, compared to set specs, compared to whatever tickles your
fancy). Tell the speaker to play *that* file instead. Done.


Phil

Grant Edwards

unread,
Jun 6, 2022, 1:11:01 PM6/6/22
to
On 2022-06-06, Phil Boutros <ph...@philb.ca> wrote:

> As pretty much everyone else has said. Insisting on real-time
> processing of something that is itself pre-recorded is non-sensical.

Downnloading a file, normalizing/compressing the volume, and then
streaming the result is three lines in a bash script (assuming you've
got wget, sox, and vlc installed). I assume that something similar
could be done in PowerShell on Windows.

With the right libraries, it's probably about the same in Python.

--
Grant






Steve GS

unread,
Jun 6, 2022, 2:09:12 PM6/6/22
to
Yes, it is real-time play back of a pre-recorded presentation.
A juke box does the same thing. It plays records.
You didn't put your quarter in to expect the band to play your piece live,
did you?

Same here, I am pulling in the programs and playing them for an audience.
All I want to do is have some semblance of AGC audio since the sources don't
seem to be able to do it.

" I would strongly
> recommend going for the much much easier method of simply downloading
> the files as they are."

As Bill Gates would say "You have not understood it from the start."
No, downloading the files, all 48 of them every week, is not easier if I
have to turn around and replay them through the system.
What could be easier than to have the smart speaker do all that work and
play the podcasts "live". All I want to do is jump in there between the
Smart Speaker and the input to the intercom and twerk the audio a bit.


Footnote:
If you double major in psychology and reverse psychology, to they cancel
each other out?

-----Original Message-----
From: Python-list <python-list-bounces+gronicus=sga....@python.org> On
Behalf Of Phil Boutros
Sent: Monday, June 6, 2022 12:39 PM
To: pytho...@python.org
Subject: Re: Automatic Gain Control in Python?

--
https://mail.python.org/mailman/listinfo/python-list

Phil Boutros

unread,
Jun 6, 2022, 2:17:37 PM6/6/22
to
Steve GS <Gron...@SGA.Ninja> wrote:
> Yes, it is real-time play back of a pre-recorded presentation.
> A juke box does the same thing. It plays records.
> You didn't put your quarter in to expect the band to play your piece live,
> did you?
>
> Same here, I am pulling in the programs and playing them for an audience.
> All I want to do is have some semblance of AGC audio since the sources don't
> seem to be able to do it.

And if your metaphorical jukebox plays digital files, I would
expect it to do any modifications to the *file* before playing, not try
to modify the output as it analyzed what it was playing via its own
output (for some reason), which is vastly more difficult.

You're saying it here yourself: "I am pulling in the programs".
Good, then modify them *before* playing them? Nope, you insist on
getting them playing, then attempting to monitor them and modify them
on the fly based on...some parameters that are unclear. But, once
again, your way is clearly better, then go ahead and fill your boots
on doing it your way. What do we know?

Good luck to you. It once again appears that we are at an
impasse. Let us know how it works out.

Phil Boutros

unread,
Jun 6, 2022, 2:19:44 PM6/6/22
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>
> When a mike is used anyway: Some mobile devices have a mike
> with AGC in the driver, so they usually record with AGC.

In the name of all that is Holy, please don't suggest another
intermediary step that may actually be taken seriously here! Do you
really want a phone listening to this speaker while recording, and
then...er...what? Feed that to the speaker again?

This is a level of masochism that the Marquis de Sade would blush
at.

Karsten Hilbert

unread,
Jun 6, 2022, 2:57:25 PM6/6/22
to
Am Mon, Jun 06, 2022 at 02:08:41PM -0400 schrieb Steve GS:

> Yes, it is real-time play back of a pre-recorded presentation.
...

What all of us around here don't understand is why you insist
on not being able to modify the data to your heart's content
inbetween this ...

> [...] pulling in the programs

... and that ...

> and playing them for an audience.

??

IOW, during the "and" phase.

Best,
Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B

Dennis Lee Bieber

unread,
Jun 6, 2022, 3:05:24 PM6/6/22
to
On Mon, 06 Jun 2022 10:10:37 -0700 (PDT), Grant Edwards
<grant.b...@gmail.com> declaimed the following:
I would like to point out that "normalization" is not the same as
"AGC", which is not the same as "compression"... So what really is desired
here?

Normalization MUST have the entire file available, as it needs to know
the peak signal and adjusts the entire file to maximize the peak.

AGC in contrast is a sliding window operation which attempts to level
out the overall signal based upon previous "average". It is not "read a
chunk, normalize the chunk, repeat". Aggressive AGC will wipe out any
dynamics in an audio signal (I once had a cheap RatShack stereo cassette
deck which used a fixed AGC on record -- try to imagine the hash it made
when taping the 45rpm record of "The Night Chicago Died"... Every time the
drum hit in the opening measures the "sirens" were suppressed only to rise
in the interval before the next drum hit).

Compression operates sample-by-sample, so can be applied live -- but is
best used after the entire audio file has been normalized. Compression (at
least as used by me in the past -- Vegas 6) /reduces/ the strong parts of
the signal (anything above the specified threshold) by some defined ratio
-- then adjusts the entire signal back up to maximize the "reduced"
portion, thereby raising everything that fell below the threshold value.
There may or may not be some hint of AGC in the algorithm used (the
Audacity documents show attack/release settings acting as a limited AGC in
that it responds when transitions above and below threshold occur).


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
0 new messages