root/experimental/components/rc2007_gui/WorkModules.py @ 5360

Revision 5360, 8.5 KB (checked in by jgaeddert, 6 years ago)

basics for playing stuff on the speaker

Line 
1
2'''
3/****************************************************************************
4
5Copyright 2007 Virginia Polytechnic Institute and State University
6
7This file is part of the OSSIE USRP_Commander_GUI.
8
9OSSIE USRP_Commander_GUI is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14OSSIE USRP_Commander_GUI is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with OSSIE USRP_Commander_GUI; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23****************************************************************************/
24
25'''
26
27#!/usr/bin/env python
28import threading
29import standardInterfaces__POA
30import standardInterfaces as SI
31
32class txWorkClass:
33
34    def __init__(self, rc2007_gui_ref):
35        '''Initialization.  Sets a reference to parent. 
36        Initializes the buffer.  Starts the Process data
37        thread, which waits for data to be added to the buffer'''
38
39        self.rc2007_gui_ref = rc2007_gui_ref
40   
41        self.voice_data_queue = []
42        self.voice_data_queue_lock = threading.Lock()
43        self.voice_data_signal = threading.Event()
44
45        self.is_running = True
46
47        modulation_scheme = SI.ModulationScheme(SI.ModulationScheme.PSK, 2)
48
49        packet_id = 0
50        sampling_frequency = 0.0
51        carrier_frequency = 0.0
52        signal_bandwidth = 0.0
53        signal_strength = 0.0
54        eom = True
55        self.default_metadata = SI.MetaData(modulation_scheme,
56                                            packet_id,
57                                            sampling_frequency,
58                                            carrier_frequency,
59                                            signal_bandwidth,
60                                            signal_strength,
61                                            eom)   
62
63        self.CVSD_e_thread = threading.Thread(target=self.CVSD_encode)
64        self.CVSD_e_thread.start()
65       
66        self.process_thread = threading.Thread(target=self.Process)
67        self.process_thread.start()
68   
69    def __del__(self):
70        '''Destructor'''
71        pass
72   
73    def CVSD_encode(self):
74        # get voice from mic
75
76        # do CVSD encoding, write to void_data
77        voice_data = []
78        self.AddVoiceData(voice_data)
79
80
81    def AddVoiceData(self, data):
82        '''Generally called by parent.  Adds data to a buffer.
83        The Process() method will wait for this buffer to be set.
84        '''
85        self.voice_data_queue_lock.acquire()
86        self.voice_data_queue.insert(0, data)
87        self.voice_data_queue_lock.release()
88        self.voice_data_signal.set()
89   
90    def SendTextData(self,text):
91        # TODO: pack/depack
92        metadata = self.default_metadata
93
94        self.rc2007_gui_ref.to_radio_port_servant.send_data(text, metadata)
95   
96    def Release(self):
97        self.is_running = False
98        self.voice_data_signal.set()
99       
100    def Process(self):
101        while self.is_running:
102            self.voice_data_signal.wait()  # wait for data to be aded to the
103                                     # buffer in self.AddVoiceData()
104            while len(self.voice_data_queue) > 0:
105                # get the data from the buffer:
106                self.voice_data_queue_lock.acquire()
107                (data, metadata) = self.voice_data_queue.pop()
108                self.voice_data_queue_lock.release()
109               
110                # forwarding voice data:
111            if self.rc2007_gui_ref.talk_flag:
112                    if self.rc2007_gui_ref.to_radio_port_active:
113                        self.rc2007_gui_ref.to_radio_port_servant.send_data(
114                                                            data, metadata)   
115
116            self.voice_data_signal.clear()  # done reading the buffer
117               
118
119
120class rxWorkClass:
121
122    def __init__(self, rc2007_gui_ref):
123        '''Initialization.  Sets a reference to parent. 
124        Initializes the buffer.  Starts the Process data
125        thread, which waits for data to be added to the buffer'''
126
127        self.rc2007_gui_ref = rc2007_gui_ref
128   
129        self.data_queue = []
130        self.data_queue_lock = threading.Lock()
131        self.data_signal = threading.Event()
132
133        self.is_running = True
134
135        self.channels = 2
136        self._setup_sound()
137
138        self.process_thread = threading.Thread(target=self.Process)
139        self.process_thread.start()
140
141    def _setup_sound(self):
142        self.timing_diff = 1000
143        self.ossspeed = 100000
144        self.osschannels = 1
145        self.ossfmt = ossaudiodev.AFMT_S16_LE
146        self.CORBA_being_used = False
147
148        self.sound_driver = ossaudiodev.open('w')
149        self.osschannels = self.sound_driver.channels(2)
150        #ossfmt = sound_driver.setfmt(ossaudiodev.AFMT_U8)
151        self.ossfmt = self.sound_driver.setfmt(ossaudiodev.AFMT_S16_LE)
152        self.ossspeed = self.sound_driver.speed(50000)
153        self.sound_driver.nonblock()
154   
155    def __del__(self):
156        '''Destructor'''
157        pass
158   
159    def AddMetaData(self, data, meta_data):
160        '''Generally called by parent.  Adds data to a buffer.
161        The Process() method will wait for this buffer to be set.
162        '''
163        self.data_queue_lock.acquire()
164        self.data_queue.insert(0, (data, meta_data) )
165        self.data_queue_lock.release()
166        self.data_signal.set()
167   
168    def CVSDDecode(self, data):
169        # data is real char
170        # CVSD decode
171        left_channel = []
172        right_channel = left_channel
173
174        self.playAudio(left_channel, right_channel)
175
176
177    def playAudio(self,Left_channel, Right_channel):
178            # play on speaker
179            my_string = ''
180            if Right_channel[0]==0:     # using the left channel
181                for y in range(0,len(Left_channel)):
182                    upper_val = Left_channel[y]/256
183                    lower_val = Left_channel[y] - (upper_val * 256)
184                    my_string += struct.pack('h', Left_channel[y])
185                    #my_string += struct.pack('B',lower_val)
186                    #my_string += struct.pack('b',upper_val)
187                    if self.channels == 2:
188                        my_string += '\0'
189                        my_string += '\0'
190                    else:
191                        my_string += struct.pack('h', Left_channel[y])
192                        #my_string += struct.pack('B',lower_val)
193                        #my_string += struct.pack('b',upper_val)
194            else:     # using the right channel
195                for y in range(0,len(Right_channel)):
196                    upper_val = Right_channel[y]/256
197                    lower_val = Right_channel[y] - (upper_val * 256)
198                    my_string += struct.pack('h', Right_channel[y])
199                    #my_string += struct.pack('B',lower_val)
200                    #my_string += struct.pack('b',upper_val)
201                    if self.channels == 2:
202                        my_string += '\0'
203                        my_string += '\0'
204                    else:
205                        my_string += struct.pack('h', Right_channel[y])
206                        #my_string += struct.pack('B',lower_val)
207                        #my_string += struct.pack('b',upper_val)
208            self.sound_driver.writeall(my_string)
209
210    def Release(self):
211        self.is_running = False
212        self.data_signal.set()
213       
214    def Process(self):
215        while self.is_running:
216            self.data_signal.wait()  # wait for data to be aded to the
217                                     # buffer in self.AddVoiceData()
218            while len(self.data_queue) > 0:
219                # get the data from the buffer:
220                self.data_queue_lock.acquire()
221                (data, metadata) = self.data_queue.pop()
222                self.data_queue_lock.release()
223       
224                voice = False
225                text = False
226
227                # TODO: look at the metadata and decide if it's voice or text       
228                # forwarding voice data:
229            if voice:
230                    self.CVSDDecode(data)
231
232                if text:
233                    self.rc2007_gui_ref.prnt_app.frame.DisplayText(data)
234
235            self.data_signal.clear()  # done reading the buffer
236
237
238
239
Note: See TracBrowser for help on using the browser.