Giter VIP home page Giter VIP logo

audio-to-plot's Introduction



AUDIO TO PLOT💟

🌏 Readme in فارسی



Table of contents✅✔

Installation

Install the Library with pip:

pip install matplotlib
pip install queuelib
pip install os-sys
pip install sounddevice

Update existing installation:pip3 install (YOUR LIBRARY) --upgrade (update as often as possible because this library is under active development)

Important

read completely:)

Analiys Code🎃:

importing♻🔰:

import queue
import sys
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd

ُSet Variable:

  • This part of the code defines a variable named ``device'' whose initial value is equal to 0.
  • Then parameters related to audio visualization such as window', downsample', channels', and interval' are defined.
  • Finally, a queue named `q' is created, which is used to store sound data.
# Set the audio input device (change the device number if needed)
device = 0
# Parameters for audio visualization
window = 1000  # Window size in milliseconds
downsample = 1
channels = [1]
interval = 30
# Initialize a queue for audio data
q = queue.Queue()

Audio Input :

  • In this part of the code, first, the information of the input device with ``device'' number is extracted.
    • Then the sampling rate is calculated for the desired device.
    • The length of the data required for sound visualization is also calculated according to the window'', downsample'' and ``samplerate'' parameters.
    • Finally, a graph with specified dimensions (8 x 4) is created and graph lines are drawn with green color (0, 1, 0.29).
# Query device information
device_info = sd.query_devices(device, 'input')
samplerate = device_info["default_samplerate"]
# Calculate the length of the data buffer
length = int(window * samplerate / (1000 * downsample))
# Initialize the plot data
plotdata = np.zeros((length, len(channels)))
# Create the plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.set_title("Audio To Plot \nGITHUB : Jokernets")
lines = ax.plot(plotdata, color=(0, 1, 0.29))

Define :

  1. Audio callback function ('audio_callback'):

    • This function is probably used to manage audio data.
    • takes "indata", "frames", "time" and "status" as parameters.
    • Inside the function, it looks like it will pull down the audio data and store it in a queue (q).
    • The downsample factor is defined as 'downsample'.
    • The processed audio data is then used for visualization.
  2. update plot function ('update_plot'):

    • This function updates a graph (probably a real-time audio visualization).
    • retrieves the audio data from the queue (q) and modifies it.
    • The moved data is then assigned to "plotdata".
    • Finally, it updates the y data of each line in the graph.
  3. Global variables:

    • plotdata: a numpy array (with leading zeros) to store audio data.
    • q: a queue to store the sampled audio data.
    • `lines': a list of Line2D objects (possibly to draw).

Please note that some placeholders (such as 'lines', 'dataprot' and actual rendering code) should be replaced with actual data and objects. This piece is meant to be integrated into a larger program that handles audio imaging.

# Audio callback function
def audio_callback(indata, frames, time, status):
    q.put(indata[::downsample, [0]])
# Update plot function
def update_plot(frame):
    global plotdata
    while True:
        try:
            data = q.get_nowait()
        except queue.Empty:
            break
        shift = len(data)
        plotdata = np.roll(plotdata, -shift, axis=0)
        plotdata[-shift:, :] = data
    for column, line in enumerate(lines):
        line.set_ydata(plotdata[:, column])
    return lines

Set Plot:

ax.set_facecolor((0, 0, 0)): This line of code sets the background color of an object (axis) of the set in the map. Here, the color specified by RGB (0, 0, 0) is black. In other words, this line of code will make the background black. ani = FuncAnimation(fig, update_plot, interval=interval, blit=True): This line of code is used to create an animation in the plot. This line of code contains the FuncAnimation function and some statements of the fig object (the object containing the graph), the update_plot function (which is responsible for changing the graph), the distance between each animation frame (distance) and blit=True (ie). only from changes that have changed) as input.

# Set the plot background color
ax.set_facecolor((0, 0, 0))
# Create the audio stream
stream = sd.InputStream(device=device, channels=max(channels), samplerate=samplerate, callback=audio_callback)
# Start the animation
ani = FuncAnimation(fig, update_plot, interval=interval, blit=True)
# Show the plot
with stream:
    plt.show()





More Examples and Showcase 👑

Video image of the APP 📺

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐌𝐞🎈🎃

Buy Me A Coffee https://www.linkedin.com/in/mohammadfallahnejad/

audio-to-plot's People

Contributors

imfallah avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.