How to use save_buffer method in hypothesis

Best Python code snippet using hypothesis

RecordUtil.py

Source:RecordUtil.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2import wave3import numpy as np4from pyaudio import PyAudio, paInt165class RecordUtil:6 NUM_SAMPLES = 2000 # pyaudio内置缓冲大小7 SAMPLING_RATE = 8000 # 取样频率8 LEVEL = 500 # 声音保存的阈值9 COUNT_NUM = 20 # NUM_SAMPLES个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音10 SAVE_LENGTH = 8 # 声音记录的最小长度:SAVE_LENGTH * NUM_SAMPLES 个取样11 TIME_COUNT = 16 # 录音时间,单位s12 Voice_String = []13 def savewav(self, filename):14 wf = wave.open(filename, 'wb')15 wf.setnchannels(1)16 wf.setsampwidth(2)17 wf.setframerate(self.SAMPLING_RATE)18 wf.writeframes(np.array(self.Voice_String).tostring())19 # wf.writeframes(self.Voice_String.decode())20 wf.close()21 def recoder(self):22 pa = PyAudio()23 stream = pa.open(format=paInt16, channels=1, rate=self.SAMPLING_RATE, input=True,24 frames_per_buffer=self.NUM_SAMPLES)25 save_count = 026 save_buffer = []27 time_count = self.TIME_COUNT28 while True:29 time_count -= 130 # print time_count31 # 读入NUM_SAMPLES个取样32 string_audio_data = stream.read(self.NUM_SAMPLES)33 # 将读入的数据转换为数组34 audio_data = np.fromstring(string_audio_data, dtype=np.short)35 # 计算大于LEVEL的取样的个数36 large_sample_count = np.sum(audio_data > self.LEVEL)37 print('正在录音...')38 # print(np.max(audio_data))39 # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块40 if large_sample_count > self.COUNT_NUM:41 save_count = self.SAVE_LENGTH42 else:43 save_count -= 144 if save_count < 0:45 save_count = 046 if save_count > 0:47 # 将要保存的数据存放到save_buffer中48 # print save_count > 0 and time_count >049 save_buffer.append(string_audio_data)50 else:51 # print save_buffer52 # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻53 # print "debug"54 if len(save_buffer) > 0:55 self.Voice_String = save_buffer56 save_buffer = []57 print("Recode a piece of voice successfully!")58 return True59 if time_count == 0:60 if len(save_buffer) > 0:61 self.Voice_String = save_buffer62 save_buffer = []63 print("Recode a piece of voice successfully!")64 return True65 else:66 return False67if __name__ == "__main__":68 r = RecordUtil()69 r.recoder()...

Full Screen

Full Screen

pyaudio.py

Source:pyaudio.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2import pyaudio3import numpy as np 4from datetime import datetime 5import wave6class recoder:7 NUM_SAMPLES = 2000 #pyaudio内置缓冲大小8 SAMPLING_RATE = 8000 #取样频率9 LEVEL = 500 #声音保存的阈值10 COUNT_NUM = 20 #NUM_SAMPLES个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音11 SAVE_LENGTH = 8 #声音记录的最小长度:SAVE_LENGTH * NUM_SAMPLES 个取样12 TIME_COUNT = 60 #录音时间,单位s13 Voice_String = []14 def savewav(self,filename):15 wf = wave.open(filename, 'wb') 16 wf.setnchannels(1) 17 wf.setsampwidth(2) 18 wf.setframerate(self.SAMPLING_RATE) 19 wf.writeframes(np.array(self.Voice_String).tostring()) 20 # wf.writeframes(self.Voice_String.decode())21 wf.close() 22 def recoder(self):23 pa = pyaudio.PyAudio() 24 stream = pa.open(format=paInt16, channels=1, rate=self.SAMPLING_RATE, input=True, 25 frames_per_buffer=self.NUM_SAMPLES) 26 save_count = 0 27 save_buffer = [] 28 time_count = self.TIME_COUNT29 while True:30 time_count -= 131 # print time_count32 # 读入NUM_SAMPLES个取样33 string_audio_data = stream.read(self.NUM_SAMPLES) 34 # 将读入的数据转换为数组35 audio_data = np.fromstring(string_audio_data, dtype=np.short)36 # 计算大于LEVEL的取样的个数37 large_sample_count = np.sum( audio_data > self.LEVEL )38 print(np.max(audio_data))39 # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块40 if large_sample_count > self.COUNT_NUM:41 save_count = self.SAVE_LENGTH 42 else: 43 save_count -= 144 if save_count < 0:45 save_count = 0 46 if save_count > 0 : 47 # 将要保存的数据存放到save_buffer中48 #print save_count > 0 and time_count >049 save_buffer.append( string_audio_data ) 50 else: 51 #print save_buffer52 # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻53 #print "debug"54 if len(save_buffer) > 0 : 55 self.Voice_String = save_buffer56 save_buffer = [] 57 print("Recode a piece of voice successfully!")58 return True59 if time_count==0: 60 if len(save_buffer)>0:61 self.Voice_String = save_buffer62 save_buffer = [] 63 print("Recode a piece of voice successfully!")64 return True65 else:66 return False67if __name__ == "__main__":68 r = recoder()69 r.recoder()...

Full Screen

Full Screen

add_draconian_flags.py

Source:add_draconian_flags.py Github

copy

Full Screen

1import dq11s.save2import sys3import struct45DRACONIAN_FLAG_IDENTIFIER = "DLC_00".encode()6DRACONIAN_FLAG_OFFSET_FROM_IDENTIFIER = -0x307DRACONIAN_FLAG_STRUCT = struct.Struct('<IIIIIIII')8DRACONIAN_FLAGS_TO_ADD = [9 1, # flag 010 1, # flag 111 1, # flag 212 1, # flag 313 1, # flag 414 1, # flag 515 1, # flag 616 1, # flag 717]1819if __name__ == "__main__":20 save_path = sys.argv[1]2122 with open(save_path, 'rb') as save_file:23 save_buffer = save_file.read()2425 save_is_encrypted, save_version = dq11s.save.get_save_is_encrypted_and_version(save_buffer)2627 if save_is_encrypted is None:28 print("file not recognized")29 exit(-1)3031 if save_is_encrypted:32 save_buffer, is_verified = dq11s.save.get_save_decrypt(save_buffer, save_version)33 if not is_verified:34 print("failed to verify save decryption")35 exit(-2)3637 draconian_identifier_offset = save_buffer.find(DRACONIAN_FLAG_IDENTIFIER)38 if draconian_identifier_offset == -1:39 print("failed to find flag location")40 exit(-3)4142 draconian_offset = draconian_identifier_offset + DRACONIAN_FLAG_OFFSET_FROM_IDENTIFIER4344 save_buffer = save_buffer[:draconian_offset] + DRACONIAN_FLAG_STRUCT.pack(*DRACONIAN_FLAGS_TO_ADD) \45 + save_buffer[draconian_offset + DRACONIAN_FLAG_STRUCT.size:]4647 with open(save_path, 'wb') as out_file:48 out_file.write(dq11s.save.get_save_encrypt(save_buffer, save_version))49 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful