Skip to content

Commit 37d3776

Browse files
committed
Audio
1 parent 68cb5af commit 37d3776

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,7 @@ framerate = <Wave_read>.getframerate() # Number of frames per second.
27302730
nchannels = <Wave_read>.getnchannels() # Number of samples per frame.
27312731
sampwidth = <Wave_read>.getsampwidth() # Sample size in bytes.
27322732
nframes = <Wave_read>.getnframes() # Number of frames.
2733+
<params> = <Wave_read>.getparams() # Immutable collection of above.
27332734
<bytes> = <Wave_read>.readframes(nframes) # Returns next 'nframes' frames.
27342735
```
27352736

@@ -2738,6 +2739,7 @@ nframes = <Wave_read>.getnframes() # Number of frames.
27382739
<Wave_write>.setframerate(<int>) # 44100 for CD, 48000 for video.
27392740
<Wave_write>.setnchannels(<int>) # 1 for mono, 2 for stereo.
27402741
<Wave_write>.setsampwidth(<int>) # 2 for CD quality sound.
2742+
<Wave_write>.setparams(<params>) # Sets all parameters.
27412743
<Wave_write>.writeframes(<bytes>) # Appends frames to file.
27422744
```
27432745
* **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'))
28022804
write_to_wav_file('test.wav', samples_f)
28032805
```
28042806

2807+
#### Plays a WAV file:
2808+
```python
2809+
# $ pip3 install simpleaudio
2810+
import wave, simpleaudio
2811+
with wave.open('test.wav', 'rb') as file:
2812+
p = file.getparams()
2813+
frames = file.readframes(p.nframes)
2814+
simpleaudio.play_buffer(frames, p.nchannels, p.sampwidth, p.framerate)
2815+
```
2816+
2817+
### Text to Speech
2818+
```python
2819+
# $ pip3 install pyttsx3
2820+
import pyttsx3
2821+
engine = pyttsx3.init()
2822+
engine.say('Sally sells seashells by the seashore.')
2823+
engine.runAndWait()
2824+
```
2825+
28052826

28062827
Synthesizer
28072828
-----------

index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,12 +2320,14 @@
23202320
nchannels = &lt;Wave_read&gt;.getnchannels() <span class="hljs-comment"># Number of samples per frame.</span>
23212321
sampwidth = &lt;Wave_read&gt;.getsampwidth() <span class="hljs-comment"># Sample size in bytes.</span>
23222322
nframes = &lt;Wave_read&gt;.getnframes() <span class="hljs-comment"># Number of frames.</span>
2323+
&lt;params&gt; = &lt;Wave_read&gt;.getparams() <span class="hljs-comment"># Immutable collection of above.</span>
23232324
&lt;bytes&gt; = &lt;Wave_read&gt;.readframes(nframes) <span class="hljs-comment"># Returns next 'nframes' frames.</span>
23242325
</code></pre>
23252326
<pre><code class="python language-python hljs">&lt;Wave_write&gt; = wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, <span class="hljs-string">'wb'</span>)
23262327
&lt;Wave_write&gt;.setframerate(&lt;int&gt;) <span class="hljs-comment"># 44100 for CD, 48000 for video.</span>
23272328
&lt;Wave_write&gt;.setnchannels(&lt;int&gt;) <span class="hljs-comment"># 1 for mono, 2 for stereo.</span>
23282329
&lt;Wave_write&gt;.setsampwidth(&lt;int&gt;) <span class="hljs-comment"># 2 for CD quality sound.</span>
2330+
&lt;Wave_write&gt;.setparams(&lt;params&gt;) <span class="hljs-comment"># Sets all parameters.</span>
23292331
&lt;Wave_write&gt;.writeframes(&lt;bytes&gt;) <span class="hljs-comment"># Appends frames to file.</span>
23302332
</code></pre>
23312333
<ul>
@@ -2381,6 +2383,21 @@
23812383
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, samples_f)
23822384
</code></pre></div>
23832385

2386+
<div><h4 id="playsawavfile">Plays a WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
2387+
<span class="hljs-keyword">import</span> wave, simpleaudio
2388+
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'test.wav'</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
2389+
p = file.getparams()
2390+
frames = file.readframes(p.nframes)
2391+
simpleaudio.play_buffer(frames, p.nchannels, p.sampwidth, p.framerate)
2392+
</code></pre></div>
2393+
2394+
<div><h3 id="texttospeech">Text to Speech</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pyttsx3</span>
2395+
<span class="hljs-keyword">import</span> pyttsx3
2396+
engine = pyttsx3.init()
2397+
engine.say(<span class="hljs-string">'Sally sells seashells by the seashore.'</span>)
2398+
engine.runAndWait()
2399+
</code></pre></div>
2400+
23842401
<div><h2 id="synthesizer"><a href="#synthesizer" name="synthesizer">#</a>Synthesizer</h2><div><h4 id="playspopcornbygershonkingsley">Plays Popcorn by Gershon Kingsley:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install simpleaudio</span>
23852402
<span class="hljs-keyword">import</span> simpleaudio, math, struct
23862403
<span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> chain, repeat

0 commit comments

Comments
 (0)