amandaghassaei / LaserCutRecord

generate vector cutting paths from digital audio to make a working record
http://www.instructables.com/id/Laser-Cut-Record/
55 stars 23 forks source link

.wav to .txt not working #2

Open Kayda opened 4 weeks ago

Kayda commented 4 weeks ago

I understand this is quite an old repo, but I don't seem to be able to convert the .wav to a text file. the command line pops up and then disappeard but I'm not getting any confirmation or error showing. And no .txt file is appearing.

PeterLPLabs commented 2 weeks ago

This likely no longer works because of changes in Python 3. I've written a version that works with Python 3.x:

`#this code unpacks and repacks data from:

16 bit stereo wav file at 44100hz sampling rate

to:

a txt file with mono channel of offsets from the center of a record groove

import wave import math import struct

fileName = "test.wav" #file to be imported (change this) print("Converting " + fileName)

read file and get data

w = wave.open(fileName, 'rb')#updated to bytes in python3

samplewidth = w.getsampwidth() print("Sample width in bytes: " + str(samplewidth))

framerate = w.getframerate() print ("Frame rate / sampling frequency (target: 44100): " + str(framerate))

numframes = w.getnframes() print ("Total number of frames in .WAV file: " + str(numframes))

numchannels = w.getnchannels() print ("Channels (1-mono, 2-stereo): " + str(numchannels)) offset = numchannels * samplewidth #how many bytes to skip to separate the channels

print("Progress")

read the audio frames from the wave file

frame = w.readframes(numframes) framelist = list(frame) print (".")

initialize lists of each byte. Hardcoding for mono or stereo. future release, use array for channels

monochannelbytes1 = framelist[:numframes:offset] #first of two-byte pair per channel monochannelbytes2 = framelist[1:numframes:offset] #second of two-byte pair per channel print (".")

shift second byte to the left by eight binary digits, so we can create a 16-bit number

byte2shift = [i*2**8 for i in monochannelbytes2] print (".")

add the channel bytes for a list of 16-bit sums

combinedmonochannel = [x + y for x, y in zip(monochannelbytes1,byte2shift)] print (".")

calculate the values of the groove as diplacement from center (0) of groove, from -32768 to 32768 (2^15)

audiotext = [x - 216 if x > 215 else 0 if x == 2**15 else x for x in combinedmonochannel] print (".")

import csv #python library for creating csv files from python data types

write audiotext to txt file

textfilename = fileName[:-3] + "txt" #replace wav with txt with open(textfilename, 'w') as f:

Create a CSV writer object that will write to the file 'f'

textfile = csv.writer(f)

# Write data to the CSV file
textfile.writerow(audiotext)

f.close() print ("---------") print ("wav2txt complete for " + textfilename)`