OriCat101 / SoundWaveTorrentizer

3 stars 2 forks source link

Strugles with Special characters #11

Open OriCat101 opened 6 months ago

OriCat101 commented 6 months ago

Tried to process a Japanese album and this is what it spat at me

%appdata%\Local\Programs\PyCharm Professional\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:126: UserWarning: Glyph 12505 (\N{KATAKANA LETTER BE}) missing from current font.
        FigureCanvasAgg.draw(self)

Ill add a font which should fix this

OriCat101 commented 6 months ago

Not as easy of a fix I hoped it will be, I'll have to look into it further

def save_spectrogram_plot(audio, file_path):
    """
    Save a spectrogram plot of the audio file.

    Parameters:
    - audio (AudioSegment): The audio segment.
    - file_path (str): The path of the audio file.

    Returns:
    - str: The path to the saved spectrogram plot.
    """
    import os

    # Ensure the frame rate is not zero to avoid division by zero
    if audio.frame_rate == 0:
        raise ValueError("Audio frame rate cannot be zero.")

    # Convert audio segment to mono (single channel)
    mixed_channel = audio.set_channels(1)

    # Add a custom font to the plot for better unicode support
    script_dir = os.path.dirname(os.path.abspath(__file__))
    parent_dir = os.path.dirname(script_dir)
    fpath = Path(os.path.join(parent_dir, "fonts", "NotoSans-Regular.ttf"))
    print(fpath)

    # This is what fixes the divide by zero error
    samples = np.array(mixed_channel.get_array_of_samples())
    if mixed_channel.sample_width == 2:
        samples = samples.astype(np.int16)
    elif mixed_channel.sample_width == 4:
        samples = samples.astype(np.float32)

    # Create the plot
    plt.specgram(
        samples,
        Fs=mixed_channel.frame_rate,
        cmap='viridis',
        NFFT=2000,  # Increase the number of FFT points for higher frequency resolution
        noverlap=100,  # Increase the overlap for smoother spectrogram
    )

    plt.title(label=f'Mixed Channel Spectrogram: {os.path.basename(file_path)}', font=fpath)
    plt.xlabel('Time (s)')
    plt.ylabel('Frequency (Hz)')

    # Save the plot to a temporary file
    tmp_folder = tempfile.mkdtemp()
    spectrogram = os.path.join(tmp_folder,
                               f"{os.path.splitext(os.path.basename(file_path))[0]}_mixed_channel_spectrogram.png")

    plt.savefig(spectrogram)
    plt.close()

    return spectrogram