Measuring Sound with Microphones Using Python (PyQt)

Posted by


In this tutorial, we will learn how to measure sound levels using a microphone and Python with PyQt. We will use PyQt to create a simple GUI that allows us to visually display the sound levels in real-time.

To get started, you will need to have Python and PyQt installed on your computer. If you haven’t already installed these, you can do so by following the instructions on their respective websites.

Next, we need to install a library called ‘pyaudio’ which will allow us to access the microphone and capture sound data. You can install this library using pip by running the following command:

pip install pyaudio

Now that we have all the necessary libraries installed, we can start writing our code. We will first create a simple PyQt application that displays a bar graph representing the sound levels. We will then use the pyaudio library to capture sound data from the microphone and update the bar graph accordingly.

Let’s start by importing the necessary libraries and creating a PyQt application:

import sys
import pyaudio
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QVBoxLayout, QHBoxLayout, QLabel, QWidget

class SoundMeasureApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Sound Level Meter')
        self.setGeometry(100, 100, 800, 400)

        self.init_ui()

    def init_ui(self):
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.sound_label = QLabel('Sound Level: 0')
        self.layout.addWidget(self.sound_label)

        self.bars = []
        self.bar_layout = QHBoxLayout()
        for i in range(10):
            bar = QLabel('|')
            self.bars.append(bar)
            self.bar_layout.addWidget(bar)
        self.layout.addLayout(self.bar_layout)

        self.stream = None

    def start_measurement(self):
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024, stream_callback=self.callback)
        self.stream.start_stream()

    def callback(self, in_data, frame_count, time_info, status):
        data = np.frombuffer(in_data, dtype=np.int16)
        rms = np.sqrt(np.mean(data**2))
        sound_level = int(rms / (2 ** 16) * 100)

        self.update_bars(sound_level)

        return (in_data, pyaudio.paContinue)

    def update_bars(self, sound_level):
        for i, bar in enumerate(self.bars):
            if i < sound_level // 10:
                bar.setStyleSheet('background-color: green;')
            else:
                bar.setStyleSheet('background-color: gray;')

        self.sound_label.setText(f'Sound Level: {sound_level}%')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_win = SoundMeasureApp()
    main_win.show()
    main_win.start_measurement()
    sys.exit(app.exec_())

In this code, we first create a PyQt application with a QLabel displaying the current sound level and 10 QLabel objects which will represent the sound level visually through colored bars. We also create a PyAudio stream that will capture sound data from the microphone and call a callback function to update the display.

When you run this code, you should see a GUI window with a bar graph and a label indicating the current sound level. You will need to allow access to your microphone when prompted in order for the application to capture sound data.

This is a basic example of how you can measure sound levels using a microphone and Python with PyQt. You can further customize the GUI or add more features to the application to suit your needs.

I hope this tutorial was helpful in getting you started with measuring sound levels using a microphone in Python with PyQt. Let me know if you have any questions or need further clarification. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@scipiocooke7098
25 days ago

This looks very cool, do you have a link to GitHub repo?

1
0
Would love your thoughts, please comment.x
()
x