[tuxdroid-svn] r647 - api/python/trunk
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2007-10-30 09:02:44
|
Author: remi Date: 2007-10-30 10:02:42 +0100 (Tue, 30 Oct 2007) New Revision: 647 Modified: api/python/trunk/tuxapi_class.py Log: A Modified: api/python/trunk/tuxapi_class.py =================================================================== --- api/python/trunk/tuxapi_class.py 2007-10-29 15:55:44 UTC (rev 646) +++ api/python/trunk/tuxapi_class.py 2007-10-30 09:02:42 UTC (rev 647) @@ -5193,6 +5193,7 @@ tux.micro.on tux.micro.off tux.micro.capture_start + tux.micro.capture_start_free tux.micro.capture_stop """ @@ -5210,6 +5211,7 @@ self.__capture_length = 0 self.__capture_buffer = [] self.__capture_idx = 0 + self.__capture_mutex = threading.Lock() def destroy(self): self.off() @@ -5219,14 +5221,18 @@ self.__thread.join() def __on_capture_buffer(self, buffer): + self.__capture_mutex.acquire() if not self.__capturing: + self.__capture_mutex.release() return for val in buffer: self.__capture_buffer.append(val) if self.__capture_length != 0: if (self.__capture_length * 8000) <= len(self.__capture_buffer): + self.__capture_mutex.release() self.capture_stop() return + self.__capture_mutex.release() def capture_start(self, out_path, length = 0): """ @@ -5243,6 +5249,25 @@ >>> tux.micro.capture_start('/home/remi/Desktop/test.wav', 10.0) >>> tux.micro.capture_start('/home/remi/Desktop/test.wav') """ + self.capture_start_free(out_path, length) + while self.__capturing: + time.sleep(0.05) + + def capture_start_free(self, out_path, length = 0): + """ + Write the stream in a wav file. + + Parameters: + "out_path" as string : path of the wave file + "length" as float : duration of the capture in seconds + When this parameters is omitted, the length + is infinite. You must stop the recording + with 'tux.micro.capture_stop()' + + Examples: + >>> tux.micro.capture_start_free('/home/remi/Desktop/test.wav', 10.0) + >>> tux.micro.capture_start_free('/home/remi/Desktop/test.wav') + """ if self.__capturing: return False if not self.__state: @@ -5260,9 +5285,11 @@ """ if not self.__capturing: return + self.__capture_mutex.acquire() self.__capturing = False self.on_buffer.disconnect(self.__capture_idx) time.sleep(0.2) + self.__capture_mutex.release() self.__write_capture() self.on_capture_stop.notify() @@ -5270,16 +5297,18 @@ try: import wave except: - print "ERROR : import wave" return freqech = 8000 sound_str = "" for val in self.__capture_buffer: sound_str += chr(val) - wavfile = wave.open(self.__capture_path, 'w') - wavfile.setparams((1, 1, freqech , len(self.__capture_buffer), 'NONE', 'not compressed')) - wavfile.writeframes(sound_str) - wavfile.close() + try: + wavfile = wave.open(self.__capture_path, 'w') + wavfile.setparams((1, 1, freqech , len(self.__capture_buffer), 'NONE', 'not compressed')) + wavfile.writeframes(sound_str) + wavfile.close() + except: + pass def on(self): """ @@ -5333,15 +5362,15 @@ def __send_energy_monitoring(self, buffer): def log_value(value): - return int(math.log(value) / math.log(255) * 255) + if value == 0: + return 0 + return int(math.log(value) / math.log(128) * 128) try: e_total = 0 for val in buffer: e_total += abs(val - 127) - e_total /= 200 - if e_total > 255: - e_total = 255 + e_total /= 800 e_total = log_value(e_total) e_table = [] @@ -5351,9 +5380,7 @@ i_e = i_b + 200 for j in range(i_b, i_e): e_m += abs(buffer[j] - 127) - e_m /= 50 - if e_m > 255: - e_m = 255 + e_m /= 200 e_m = log_value(e_m) e_table.append(e_m) except: |