Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pyaudiotest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ture Pålsson
pyaudiotest
Commits
b0e062ba
Commit
b0e062ba
authored
5 years ago
by
Ture Pålsson
Browse files
Options
Downloads
Patches
Plain Diff
Encapsulate things a bit
parent
9430ac1f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
audio.py
+32
-20
32 additions, 20 deletions
audio.py
with
32 additions
and
20 deletions
audio.py
+
32
−
20
View file @
b0e062ba
...
@@ -4,10 +4,15 @@ import pyaudio
...
@@ -4,10 +4,15 @@ import pyaudio
import
sys
import
sys
import
time
import
time
p
=
pyaudio
.
PyAudio
()
class
Voice
:
def
__init__
(
self
,
frame_rate
):
self
.
current_frame
=
0
self
.
frame_rate
=
frame_rate
current_frame
=
0
def
generate
(
self
,
n
):
frame_rate
=
44100
data
=
make_frames
(
self
.
current_frame
,
n
,
self
.
frame_rate
)
self
.
current_frame
+=
n
return
data
def
square
(
x
,
duty
=
0.5
):
def
square
(
x
,
duty
=
0.5
):
x1
=
x
%
1
x1
=
x
%
1
...
@@ -26,7 +31,7 @@ def xsin(x):
...
@@ -26,7 +31,7 @@ def xsin(x):
def
A
(
t
,
level
=
16384
,
decay_rate
=
3
):
def
A
(
t
,
level
=
16384
,
decay_rate
=
3
):
return
level
*
math
.
exp
(
-
decay_rate
*
t
)
return
level
*
math
.
exp
(
-
decay_rate
*
t
)
def
make_frames
(
k0
,
n
):
def
make_frames
(
k0
,
n
,
frame_rate
):
f
=
440
f
=
440
T
=
1
/
frame_rate
T
=
1
/
frame_rate
F
=
f
/
frame_rate
F
=
f
/
frame_rate
...
@@ -39,10 +44,14 @@ def make_frames(k0, n):
...
@@ -39,10 +44,14 @@ def make_frames(k0, n):
for
k
in
range
(
k0
,
k0
+
n
))
for
k
in
range
(
k0
,
k0
+
n
))
return
data
.
tobytes
()
return
data
.
tobytes
()
def
main
():
frame_rate
=
44100
p
=
pyaudio
.
PyAudio
()
v
=
Voice
(
frame_rate
)
def
callback
(
in_data
,
frame_count
,
time_info
,
status
):
def
callback
(
in_data
,
frame_count
,
time_info
,
status
):
global
current_frame
data
=
v
.
generate
(
frame_count
)
data
=
make_frames
(
current_frame
,
frame_count
)
current_frame
+=
frame_count
return
(
data
,
pyaudio
.
paContinue
)
return
(
data
,
pyaudio
.
paContinue
)
stream
=
p
.
open
(
format
=
pyaudio
.
paInt16
,
stream
=
p
.
open
(
format
=
pyaudio
.
paInt16
,
...
@@ -60,3 +69,6 @@ stream.stop_stream()
...
@@ -60,3 +69,6 @@ stream.stop_stream()
stream
.
close
()
stream
.
close
()
p
.
terminate
()
p
.
terminate
()
if
__name__
==
'
__main__
'
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment