From 37d377649da5e55d887778cea37521c17e35c3cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 14 Dec 2019 20:43:56 +0100 Subject: [PATCH] Audio --- README.md | 21 +++++++++++++++++++++ index.html | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 806928c..7a035fb 100644 --- a/README.md +++ b/README.md @@ -2730,6 +2730,7 @@ framerate = .getframerate() # Number of frames per second. nchannels = .getnchannels() # Number of samples per frame. sampwidth = .getsampwidth() # Sample size in bytes. nframes = .getnframes() # Number of frames. + = .getparams() # Immutable collection of above. = .readframes(nframes) # Returns next 'nframes' frames. ``` @@ -2738,6 +2739,7 @@ nframes = .getnframes() # Number of frames. .setframerate() # 44100 for CD, 48000 for video. .setnchannels() # 1 for mono, 2 for stereo. .setsampwidth() # 2 for CD quality sound. +.setparams() # Sets all parameters. .writeframes() # Appends frames to file. ``` * **Bytes object contains a sequence of frames, each consisting of one or more samples.** @@ -2802,6 +2804,25 @@ samples_f = (add_noise(f) for f in read_wav_file('test.wav')) write_to_wav_file('test.wav', samples_f) ``` +#### Plays a WAV file: +```python +# $ pip3 install simpleaudio +import wave, simpleaudio +with wave.open('test.wav', 'rb') as file: + p = file.getparams() + frames = file.readframes(p.nframes) + simpleaudio.play_buffer(frames, p.nchannels, p.sampwidth, p.framerate) +``` + +### Text to Speech +```python +# $ pip3 install pyttsx3 +import pyttsx3 +engine = pyttsx3.init() +engine.say('Sally sells seashells by the seashore.') +engine.runAndWait() +``` + Synthesizer ----------- diff --git a/index.html b/index.html index 3057220..31653e1 100644 --- a/index.html +++ b/index.html @@ -2320,12 +2320,14 @@ framerate = <Wave_read>.getframerate() # Number of samples per frame. sampwidth = <Wave_read>.getsampwidth() # Sample size in bytes. nframes = <Wave_read>.getnframes() # Number of frames. +<params> = <Wave_read>.getparams() # Immutable collection of above. <bytes> = <Wave_read>.readframes(nframes) # Returns next 'nframes' frames.
<Wave_write> = wave.open('<path>', 'wb')
 <Wave_write>.setframerate(<int>)                # 44100 for CD, 48000 for video.
 <Wave_write>.setnchannels(<int>)                # 1 for mono, 2 for stereo.
 <Wave_write>.setsampwidth(<int>)                # 2 for CD quality sound.
+<Wave_write>.setparams(<params>)                # Sets all parameters.
 <Wave_write>.writeframes(<bytes>)               # Appends frames to file.
 
    @@ -2381,6 +2383,21 @@ samples_f = (add_noise(f) for f 'test.wav', samples_f) +

    Plays a WAV file:

    # $ pip3 install simpleaudio
    +import wave, simpleaudio
    +with wave.open('test.wav', 'rb') as file:
    +    p = file.getparams()
    +    frames = file.readframes(p.nframes)
    +    simpleaudio.play_buffer(frames, p.nchannels, p.sampwidth, p.framerate)
    +
    + +

    Text to Speech

    # $ pip3 install pyttsx3
    +import pyttsx3
    +engine = pyttsx3.init()
    +engine.say('Sally sells seashells by the seashore.')
    +engine.runAndWait()
    +
    +

    #Synthesizer

    Plays Popcorn by Gershon Kingsley:

    # $ pip3 install simpleaudio
     import simpleaudio, math, struct
     from itertools import chain, repeat