-
Notifications
You must be signed in to change notification settings - Fork 2
/
au_send.py
executable file
·84 lines (63 loc) · 2.17 KB
/
au_send.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pyaudio
import sys
import struct
import math
import StringIO
import scipy.signal
import wave
import numpy
import random
import array
from au_filter import Filter
from au_defs import *
total_sample_count = 0
lowpass = Filter( 0, 500 )
def send( samples, stream, samples_per_chunk ):
return raw_send( modulate_float( samples,
samples_per_chunk ),
stream )
def expand( samples, factor ):
out = []
for s in samples:
for i in range( factor ):
if i == 0:
out.append( s )
else:
out.append( 0 )
return out
def raw_send( chunks, stream ):
for chunk in chunks:
stream.write( chunk )
# Send one chunk of I samples, modulated onto the carrier frequency
def modulate_float( samples, carrier_freq, samples_per_chunk ):
global total_sample_count
global lowpass
sample_count = 0
chunk_data = [ "" ]
chunk_number = 0
# Write payload
samples = lowpass( samples )
for s in samples:
chunk_data[ chunk_number ] += struct.pack( 'f', ((s * AMPLITUDE) + DC) * math.cos( total_sample_count * 2 * math.pi * carrier_freq / SAMPLES_PER_SECOND ) )
total_sample_count += 1
sample_count += 1
if sample_count == samples_per_chunk:
chunk_number += 1
chunk_data.append( "" )
sample_count = 0
return chunk_data
def modulate_frame( samples, existing=False ):
if existing:
if len(samples) > len(existing):
existing = numpy.hstack( (existing, numpy.zeros( len(samples) - len(existing) )) )
else:
existing = numpy.zeros( len(samples) )
sample_num = random.randint( 0, 1024 )
args = numpy.arange( sample_num, sample_num + len(samples) ) * CARRIER_CYCLES_PER_SECOND * 2 * math.pi / SAMPLES_PER_SECOND
existing += numpy.cos(args) * lowpass( samples )
return existing
def write_wav( filename, samples ):
wave_file = wave.open( filename, "w" )
wave_file.setparams( (1, 2, 8000, 0, "NONE", "NONE") )
wave_file.writeframes( array.array( 'h', [ int(0.5 + 16383.0*x) for x in samples ] ).tostring() )
wave_file.close()