@ -15,20 +15,20 @@ def parser():
parser . add_argument ( " -b " , " --bottom " , help = " Bottom frequency range. Default value: 200. " , type = int )
parser . add_argument ( " -t " , " --top " , help = " Top frequency range. Default value: 20000. " , type = int )
parser . add_argument ( " -p " , " --pixels " , help = " Pixels per second. Default value: 30. " , type = int )
parser . add_argument ( " -s " , " --sampling " , help = " Sampling rate. Default value: 44100. " , type = int )
parser . add_argument ( " -s " , " --sampling " , help = " Sample rate. Default value: 44100. " , type = int )
# Create "arguments" object based on user input
arguments = parser . parse_args ( )
# Default values
min_freq = 200
max_freq = 20000
sampling _rate = 44100
pixels = 30
output = " out.wav "
rotate = False
invert = False
min_freq = 200
max_freq = 20000
sample _rate = 44100
pixels = 30
output = " out.wav "
rotate = False
invert = False
# Check arguments values
if arguments . output :
@ -40,7 +40,7 @@ def parser():
if arguments . pixels :
pixels = arguments . pixels
if arguments . sampling :
sampling _rate = arguments . sampling
sample _rate = arguments . sampling
if arguments . rotate :
rotate = True
if arguments . invert :
@ -50,12 +50,12 @@ def parser():
print ( ' Input file: %s . ' % arguments . INPUT_FILE )
print ( ' Frequency range: %d - %d . ' % ( min_freq , max_freq ) )
print ( ' Pixels per second: %d . ' % pixels )
print ( ' Sampling rate: %d . ' % sampling _rate )
print ( ' Sampling rate: %d . ' % sample _rate )
print ( ' Rotate Image: %s . ' % ( ' Yes ' if rotate else ' No ' ) )
return ( arguments . INPUT_FILE , output , min_freq , max_freq , pixels , sampling _rate , rotate , invert )
return ( arguments . INPUT_FILE , output , min_freq , max_freq , pixels , sample _rate , rotate , invert )
def convert ( INPUT_FILE , output , min_freq , max_freq , pixels , sampling _rate , rotate , invert ) :
def convert ( INPUT_FILE , output , min_freq , max_freq , pixels , sample _rate , rotate , invert ) :
image = Image . open ( INPUT_FILE ) . convert ( ' L ' )
# rotate image if requested
@ -67,12 +67,12 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota
image = ImageOps . invert ( image )
output = wave . open ( output , ' w ' )
output . setparams ( ( 1 , 2 , sampling _rate , 0 , ' NONE ' , ' not compressed ' ) )
output . setparams ( ( 1 , 2 , sample _rate , 0 , ' NONE ' , ' not compressed ' ) )
freq_range = max_freq - min_freq
interval = freq_range / image . size [ 1 ]
samples = sampling _rate / / pixels
samples = sample _rate / / pixels
data = array . array ( ' h ' )
# Converting process start
@ -84,7 +84,7 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota
yinv = image . size [ 1 ] - y - 1
amplitude = image . getpixel ( ( x , y ) )
if ( amplitude > 0 ) :
row . append ( gen_wave ( yinv * interval + min_freq , amplitude , samples , sampling _rate ) )
row . append ( gen_wave ( yinv * interval + min_freq , amplitude , samples , sample _rate ) )
for i in range ( samples ) :
for j in row :
@ -110,8 +110,8 @@ def convert (INPUT_FILE, output, min_freq, max_freq, pixels, sampling_rate, rota
print ( " Conversion progress: 100 % " )
print ( " Success. Completed in %d seconds. " % int ( time_end - time_start ) )
def gen_wave ( frequency , amplitude , samples , sampling _rate ) :
cycles = samples * frequency / sampling _rate
def gen_wave ( frequency , amplitude , samples , sample _rate ) :
cycles = samples * frequency / sample _rate
wave = [ ]
for i in range ( samples ) :
x = math . sin ( float ( cycles ) * 2 * math . pi * i / float ( samples ) ) * float ( amplitude )