Архив метки: wav

Python заготовка для создания wav-файлов без дополнительных модулей

"""Create wav file."""
import os
import sys

file_name = input("WAV file name: ")
if os.path.exists(file_name):
    print("ERROR: File name is busy")
    sys.exit(1)
with open(file_name, "wb") as wav_file:
    wav_file.write(
        bytes(
            (
                0x52, 0x49, 0x46, 0x46,  # RIFF
                # 0x24, 0x08, 0x00, 0x00,  # Chunk size = 2084
                0xf4, 0x08, 0x00, 0x00,  # Chunk size = ???
                0x57, 0x41, 0x56, 0x45,  # WAVE
                0x66, 0x6d, 0x74, 0x20,  # fmt
                0x10, 0x00, 0x00, 0x00,  # Subchunk1 size = 16
                0x01, 0x00,  # Audio Format = 1 (PCM)
                0x02, 0x00,  # Num Channels = 2
                # 0x01, 0x00,  # Num Channels = 1
                0x22, 0x56, 0x00, 0x00,  # Sample Rate = 22050
                0x88, 0x58, 0x01, 0x00,  # Byte Rate = 88200
                0x04, 0x00,  # Block Align = 4
                0x10, 0x00,  # Bits per sample = 16
                0x64, 0x61, 0x74, 0x61,  # data
                # 0x00, 0x08, 0x00, 0x00,  # subchunk size = 2048
                0x00, 0xf8, 0x00, 0x00,  # subchunk size = ???

                0x00, 0x00,  # sample 1 left
                0x00, 0x00,  # sample 1 right

                0x24, 0x17,  # sample 2 left
                0x1e, 0xf3,  # sample 2 right

                0x3c, 0x13,  # sample 3 left
                0x3c, 0x14,  # sample 3 right

                0x16, 0xf9,
                0x18, 0xf9,

                0x34, 0xe7,
                0x23, 0xa6,

                0x3c, 0xf2,
                0x24, 0xf2,

                0x11, 0xce,
                0x1a, 0x0d,
            )
        )
    )
    for _ in range(1000000):
        wav_file.write(
            bytes(
                (
                    0x16, 0xf9,
                    0x18, 0xf9,

                    0x34, 0xe7,
                    0x23, 0xa6,

                    0x3c, 0xf2,
                    0x24, 0xf2,

                    0x11, 0xce,
                    0x1a, 0x0d,
                )
            )
        )

Python Генерация звуков через PyAudio

Следующий код генерирует и проигрывает звук:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Генерация звуков через pyaudio.

За основу взят https://medium.com/py-bits/sound-generation-python-904e54f5398d

sudo apt-get install python3-pyaudio
"""

import pyaudio
import math
PyAudio = pyaudio.PyAudio
BITRATE = 5000
FREQUENCY = 10000
LENGTH = 5
if FREQUENCY > BITRATE:
    BITRATE = FREQUENCY + 100
NUMBER_OF_FRAMES = int(BITRATE * LENGTH)
WAVEDATA = ''
for x in range(NUMBER_OF_FRAMES):
    WAVEDATA = WAVEDATA + chr(
        int(
            math.sin(
                x
                / (
                    (
                        BITRATE / FREQUENCY
                    ) / math.pi
                )
            ) * 127 + 128
        )
    )
REST_FRAMES = NUMBER_OF_FRAMES % BITRATE
for x in range(REST_FRAMES):
    WAVEDATA = WAVEDATA + chr(128)
# print(WAVEDATA)
# óYªQ±J¹CÀ<Æ5Í/Ó)Ù$Þãèìð
# öùûýþþþþýüúø	õ
# óYªQ±J¹CÀ<Æ5Í/Ó)Ù$ÞãèìðÛ&Ö,Ð2Ê9Ã?¼FµN®U¦\lt|xph
# öùûýþþþþýüúø	õ
# ...
p = PyAudio()
stream = p.open(
    format=p.get_format_from_width(1),
    channels=2,
    rate=BITRATE,
    output=True
)
stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()