@@ -50,6 +50,20 @@ def ua_data_test():
5050 write (s + "\r \n " )
5151 write ("\r \n " )
5252
53+ #
54+ # ByteVector
55+ # Assign bytes/bytearray/memoryview to a ByteVector
56+ # Copy the content of a ByteVector to a bytearray/memoryview
57+ #
58+ bv = pj .ByteVector ()
59+ bv .assign_from_bytes (bytearray ([1 , 2 , 3 ]))
60+ assert bv .size () == 3
61+ assert bv [0 ] == 1 and bv [1 ] == 2 and bv [2 ] == 3
62+
63+ ba = memoryview (bytearray (5 ))
64+ bv .copy_to_bytearray (ba [1 :])
65+ assert ba == b'\x00 \x01 \x02 \x03 \x00 '
66+
5367#
5468# Exception test
5569#
@@ -78,39 +92,45 @@ def write(self, entry):
7892 write ("This is Python:" + entry .msg + "\r \n " )
7993
8094class AMP (pj .AudioMediaPort ):
81- frames = deque ()
95+ buffers = deque ()
8296
8397 def onFrameRequested (self , frame ):
84- if len (self .frames ):
85- # Get a frame from the queue and pass it to PJSIP
86- frame_ = self .frames .popleft ()
98+ if len (self .buffers ):
99+ # Get a buffer from the queue
100+ buffer_out = self .buffers .popleft ()
101+ # Copy the buffer to PJSIP's frame
87102 frame .type = pj .PJMEDIA_TYPE_AUDIO
88- frame .buf = frame_
103+ frame .buf . assign_from_bytes ( buffer_out )
89104
90105 def onFrameReceived (self , frame ):
91- frame_ = pj .ByteVector ()
92- for i in range (frame .buf .size ()):
106+ # Get the incoming audio buffer from PJSIP's frame
107+ buffer_in = bytearray (frame .buf .size ())
108+ frame .buf .copy_to_bytearray (buffer_in )
109+ # Prepare an output buffer
110+ buffer_out = bytearray (len (buffer_in ))
111+
112+ # Now you can process these byte arrays directly, e.g. use numpy
113+ for i in range (len (buffer_in )):
93114 if (i % 2 == 1 ):
94115 # Convert it to signed 16-bit integer
95- x = frame . buf [i ] << 8 | frame . buf [i - 1 ]
116+ x = buffer_in [i ] << 8 | buffer_in [i - 1 ]
96117 x = struct .unpack ('<h' , struct .pack ('<H' , x ))[0 ]
97118
98119 # Amplify the signal by 50% and clip it
99120 x = int (x * 1.5 )
100- if ( x > 32767 ) :
121+ if x > 32767 :
101122 x = 32767
102- else :
103- if (x < - 32768 ):
104- x = - 32768
123+ elif x < - 32768 :
124+ x = - 32768
105125
106126 # Convert it to unsigned 16-bit integer
107127 x = struct .unpack ('<H' , struct .pack ('<h' , x ))[0 ]
108128
109- # Put it back in the vector in little endian order
110- frame_ . append (x & 0xff )
111- frame_ . append (( x & 0xff00 ) >> 8 )
129+ # Put it in the output buffer in little endian order
130+ buffer_out [ i - 1 ] = (x & 0xff )
131+ buffer_out [ i ] = ( x & 0xff00 ) >> 8
112132
113- self .frames .append (frame_ )
133+ self .buffers .append (buffer_out )
114134
115135#
116136# Testing log writer callback
0 commit comments