Skip to content

Instantly share code, notes, and snippets.

@jrjuarez
Last active Jul 24, 2022
Embed
What would you like to do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import struct
import sys
import wave
import numpy as np
import matplotlib.pyplot as plt
def read_samples(wave_file, nb_frames):
frame_data = wave_file.readframes(nb_frames)
if frame_data:
sample_width = wave_file.getsampwidth()
nb_samples = len(frame_data) // sample_width
format = {1: '%db', 2: '<%dh', 4: '<%dl'}[sample_width] \
% nb_samples
return struct.unpack(format, frame_data)
else:
return ()
if __name__ == '__main__':
buffer_size = 16324
norm_value = 2147
input_wave_file = wave.open('data.wav', 'rb')
nb_frames = input_wave_file.getnframes()
sync_cnt = 0
hsync_cnt = 0
vsync_cnt = 0
line_signal = []
smp_cnt = 0
img_cnt = 0
image = np.zeros((241, 385))
for frame_window in range(nb_frames // buffer_size + 1):
samples = read_samples(input_wave_file, buffer_size)
if samples:
for sample in samples:
if sample < -10000:
sync_cnt += 1
else:
if sync_cnt > 300:
vsync_cnt += 1
if vsync_cnt == 1:
print ('Found VSYNC ', hsync_cnt)
if vsync_cnt == 3:
plt.imsave('decode' + format(img_cnt, '05d') + '.png'
, image, cmap='binary')
image = np.zeros((241, 385))
vsync_cnt = 0
img_cnt += 1
hsync_cnt = 0
sync_cnt = 0
elif sync_cnt > 20:
#print ('Found HSYNC ', smp_cnt)
if smp_cnt > 0:
hsync_cnt += 1
vsync_cnt = 0
sync_cnt = 0
smp_cnt = 0
else:
if sample > 2000:
image[hsync_cnt][smp_cnt] = sample
smp_cnt += 1
sync_cnt = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment