Générateur de fichier .wav à partir d'un fichier .k7 pour Thomson TO7/70.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 lines
3.0 KiB

#!/usr/bin/env python
#
# Converts TO7 K7 files into wav file.
#
# History:
# 15/01/2010 Original code by Doug Letougth.
# 16/01/2010 Reworked by Samuel Devulder to speed it up and make it work with http://nostalgies.thomsonistes.org/archives/k7tools-2.1a-dosexe.zip
#
import sys
import os
import wave
import math
import struct
# wav freq
freq = 44100
# number of bits per sample (8 or 16)
nbits = 8
# number of channels (1 = mono, 2 = stereo)
nchans = 1
# transmission rate (bits per seconds)
bps = 900
# relative intensity level for each bit (not necessary but helps reading the curves visualy)
vol0 = 0.6
vol1 = 0.9
# Clear screen (avalibale only on posix systems)
if os.name == "posix":
print "\x1b[1;1H\033[J"
# Some ansi escape characters for verbose display
ansi_esc="\x1b[1;1H"
else:
# No ansi for non-POSIX compliant systems
ansi_esc=""
if len( sys.argv ) != 3 :
exit("Usage : k7towav.py <input .k7 file> <output .wav file>")
else:
input_k7 = sys.argv[1]
output_wav = sys.argv[2]
if not os.path.isfile(input_k7) :
exit("File not found : " + input_k7)
if os.path.isfile(output_wav) :
exit("File already exists : " + output_wav)
# TODO : Add some code here for asking for overwrite
# Return the byte as a list of bits
def bstr(n):
return ''.join([str(n >> x & 1) for x in (0,1,2,3,4,5,6,7)])
# 2pi
two_pi = 2*math.pi
# length of a bit
samples_per_bit = int(freq / bps)
# 4.5khz signal sample
#bit0 = [ math.sin( two_pi*5*x/samples_per_bit ) for x in range(samples_per_bit) ]
# 6.3khz signal sample
#bit1 = [ math.sin( two_pi*7*x/samples_per_bit ) for x in range(samples_per_bit) ]
bit0 = []
bit1 = []
for x in range(samples_per_bit) :
sig0 = math.sin( two_pi*5*x/samples_per_bit )
sig1 = math.sin( two_pi*7*x/samples_per_bit )
if sig0 >= 0:
bit0.append(1)
else :
bit0.append(-1)
if sig1 > 0:
bit1.append(1)
else :
bit1.append(-1)
# scale
scale = (2**nbits - 1)/2
# build up string representing the frames for bit0 and bit1 (all channels have the same value).
sig_0 = ''
for x in bit0:
for y in range(nchans):
sig_0 += struct.pack('B', int(scale*(1+x*vol0)))
sig_1 = ''
for x in bit1:
for y in range(nchans):
sig_1 += struct.pack('B', int(scale*(1+x*vol1)))
# Byte counter (for display purpose)
bytenum=0
k7 = open(input_k7,"rb")
wav = wave.open(output_wav,"wb")
wav.setparams((nchans, nbits>>3, freq, 0, 'NONE', 'noncompressed'))
while 1 :
byte = k7.read(8)
if not byte:
break
bytenum=bytenum+1
# bit counter (for display purpose)
bitnum=0
for c in byte :
bbyte = bstr(ord(c))
bitnum=bitnum+1
# Start bit is 0
wav.writeframesraw(sig_0)
# Byte processing
for bit in bbyte :
if bit == "0" :
wav.writeframesraw(sig_0)
else:
wav.writeframesraw(sig_1)
if (bytenum*8 + bitnum) % 123 ==0:
print ansi_esc+"Processing bit # "+str(bitnum)+" from byte # "+str(bytenum)+" ("+bit+") "+str(wav.tell()/1024*2)+" Kbytes"
# Ending bits are 1 (2 bits)
wav.writeframesraw(sig_1)
wav.writeframesraw(sig_1)
k7.close()
wav.close()