def auto_treshold(): """ Finds a noise treshold in a room """ print("Finding treshold...") start = time.time() p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE) num_silent = 0 snd_started = False r = array('h') global THRESHOLD while 1: # little endian, signed short snd_data = array('h', stream.read(CHUNK_SIZE)) if byteorder == 'big': snd_data.byteswap() r.extend(snd_data) maximum = max(snd_data) if maximum > THRESHOLD: THRESHOLD = maximum if time.time() - start >= 5: break THRESHOLD *= 2 print("Auto threshold finished: " + str(THRESHOLD))
def record(): """ Record a word or words from the microphone and return the data as an array of signed shorts. Normalizes the audio, trims silence from the start and end, and pads with 0.5 seconds of blank sound to make sure VLC et al can play it without getting chopped off. """ start = time.time() while time.time() - start < 2: {} print("Recording started") p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE) num_silent = 0 snd_started = False r = array('h') while 1: # little endian, signed short snd_data = array('h', stream.read(CHUNK_SIZE)) if byteorder == 'big': snd_data.byteswap() r.extend(snd_data) silent = is_silent(snd_data) if silent and snd_started: num_silent += 1 elif not silent and not snd_started: snd_started = True if snd_started and num_silent > 30: break print("Recording done") sample_width = p.get_sample_size(FORMAT) stream.stop_stream() stream.close() p.terminate() r = normalize(r) r = trim(r) # r = add_silence(r, 0.5) return sample_width, r
def initProject(self): # 创建时间 createTime = long(time.time() * 1000) return { "op": "ACT", "createTime": createTime, "status": "draft", "source": "spider", "sourceWebsite": "archdaily", "createUser": { "userId": "57f5bb5eafd1196165f255d0", "realName": "马小良" }, "type": "product", "hasOwner": "Y", "ownerTime": createTime, "updateTime": createTime, "updateUser": { "userId": "57f5bb5eafd1196165f255d0", "realName": "马小良" }, "opening": "N", "author": { "id": "57f5bb5eafd1196165f255d0", "name": "马小良" }, "tag": [], "category": [] }
def record(): start = time.time() while time.time() - start < 2: {} print("Recording started") p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE) num_silent = 0 snd_started = False r = array('h') while 1: # little endian, signed short snd_data = array('h', stream.read(CHUNK_SIZE)) if byteorder == 'big': snd_data.byteswap() r.extend(snd_data) silent = is_silent(snd_data) if silent and snd_started: num_silent += 1 elif not silent and not snd_started: snd_started = True if snd_started and num_silent > 180: break sample_width = p.get_sample_size(FORMAT) stream.stop_stream() stream.close() p.terminate() r = normalize(r) r = trim(r) # r = add_silence(r, 0.5) return sample_width, r
def setPictures(self, projectId, originImgs): # 图片信息为空时 # if (undefined == originImgs || null == originImgs || originImgs.length <= 0) { if originImgs is None: return # 图片ID pictureId = str(ObjectId()) # 图片类型内容 picture = { "_id": pictureId, "parentType": "product", "op": "ACT", "createTime": long(time.time() * 1000), "status": "released", "source": "spider", "sourceWebsite": "archdaily", "createUser": { "userId": "57f5bb5eafd1196165f255d0", "realName": "马小良" }, "author": { "id": "57f5bb5eafd1196165f255d0", "name": "马小良" }, "type": "picture", "fkTag": [ projectId # 项目ID ], "tag": [{ "name": pictureId }], "picture": [] } # 设置图片信息 for img in originImgs: # oss 上的image id if "ossImgUrl" in img and img["ossImgUrl"] is not None: picture["picture"].append({ "id": img["ossImgUrl"], "watermark": img["copyright"] }) self.logger.debug("picture: %s", str(picture)) # 保存图片信息 self.mongoclient.db['b_content_spider'].save(picture)