File: numpy_test.py

package info (click to toggle)
sndobj 2.6.7%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,952 kB
  • sloc: ansic: 55,663; cpp: 21,625; python: 391; makefile: 130; java: 22; sh: 21
file content (47 lines) | stat: -rwxr-xr-x 1,186 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from sndobj import SndObj, SndRTIO, HarmTable, Oscili, SND_OUTPUT
from scipy import zeros, pi, sin, float32
from pylab import plot, show
    
# ---------------------------------------------------------------------------
# Test PushIn

# Create 1 frame of a sine wave in a numpy array
sine = zeros(256, dtype=float32)
for i in range(sine.size):
    sine[i] = 0.5 * sin((2 * pi * i) / sine.size)
sine *= 32768

obj = SndObj()
obj.PushIn(sine)

outp = SndRTIO(1, SND_OUTPUT)
outp.SetOutput(1, obj)

# Repeatedly output the 1 frame of sine wave
duration = outp.GetSr() * 2  # 2 seconds
i = 0
vector_size = outp.GetVectorSize()
while i < duration:
    outp.Write()
    i += vector_size

# ---------------------------------------------------------------------------    
# Test PopOut

# copy a sine wave generated by a SndObj into a numpy array
output_size = 2048
harm = HarmTable(10000,1,1)
osc = Oscili(harm, 440, 10000)
output = zeros(output_size, dtype=float32)

duration = output_size  # output_size samples
i = 0
vector_size = osc.GetVectorSize()
while i < duration:
    osc.DoProcess()
    osc.PopOut(output[i:i+vector_size])
    i += vector_size

# plot the array
plot(output)
show()