1# Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Converts values pulled from the microcontroller into audio files.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import struct 22# import matplotlib.pyplot as plt 23 24import numpy as np 25import soundfile as sf 26 27 28def new_data_to_array(fn): 29 vals = [] 30 with open(fn) as f: 31 for n, line in enumerate(f): 32 if n != 0: 33 vals.extend([int(v, 16) for v in line.split()]) 34 b = ''.join(map(chr, vals)) 35 y = struct.unpack('<' + 'h' * int(len(b) / 2), b) 36 37 return y 38 39 40data = 'captured_data.txt' 41values = np.array(new_data_to_array(data)).astype(float) 42 43# plt.plot(values, 'o-') 44# plt.show(block=False) 45 46wav = values / np.max(np.abs(values)) 47sf.write('captured_data.wav', wav, 16000) 48