
import wave
import numpy as np

def generate_sample_file(test_freqs, test_amps, chunk=4096, samplerate=44100):

   filename = 'Sample_'

   x = np.arange(chunk)
   y = np.zeros(chunk)
   for test_freq,test_amp in zip(test_freqs,test_amps):
      filename += str(test_freq) + 'Hz@' + str(test_amp)
      y = np.add(y, np.sin(2 * np.pi * test_freq * x / samplerate) * test_amp)

   filename += '.wav'
   y = y.astype('i2')

   wave_writer = wave.open(filename, mode='wb')

   wave_writer.setnchannels(1)
   wave_writer.setsampwidth(2)
   wave_writer.setframerate(samplerate)

   for x in range(0,8):
      wave_writer.writeframes(y)


import wave
import argparse
import numpy as np

def generate_sample_file(test_freqs, test_amps, chunk=4096, samplerate=44100):
   
   filename = 'Sample'

   x = np.arange(chunk)
   y = np.zeros(chunk)
   for test_freq,test_amp in zip(test_freqs,test_amps):
      filename += '_' + str(test_freq) + 'Hz@' + str(test_amp)
      y = np.add(y, np.sin(2 * np.pi * test_freq * x / samplerate) * test_amp)

   filename += '.wav'
   y = y.astype('i2')

   wave_writer = wave.open(filename, mode='wb')

   wave_writer.setnchannels(1)
   wave_writer.setsampwidth(2)
   wave_writer.setframerate(samplerate)

   for x in range(0,8):
      wave_writer.writeframes(y)
                       
if __name__ == '__main__':
   parser = argparse.ArgumentParser(description='Write a wave file containing Numpy generated sine waves')
   parser.add_argument('--freqs', nargs='+', type=int)
   parser.add_argument('--amps', nargs='+', type=int)
   args = parser.parse_args()
   
   generate_sample_file(args.freqs, args.amps)
   

Add a main function with command line arguments
Add a main function with command line arguments

Now able to generate wave files from command line
  
  import wave
+ import argparse
  import numpy as np
  
  def generate_sample_file(test_freqs, test_amps, chunk=4096, samplerate=44100):
- 
+    
-    filename = 'Sample_'
+    filename = 'Sample'
  
     x = np.arange(chunk)
     y = np.zeros(chunk)
     for test_freq,test_amp in zip(test_freqs,test_amps):
-       filename += str(test_freq) + 'Hz@' + str(test_amp)
+       filename += '_' + str(test_freq) + 'Hz@' + str(test_amp)
        y = np.add(y, np.sin(2 * np.pi * test_freq * x / samplerate) * test_amp)
  
     filename += '.wav'
     y = y.astype('i2')
  
     wave_writer = wave.open(filename, mode='wb')
  
     wave_writer.setnchannels(1)
     wave_writer.setsampwidth(2)
     wave_writer.setframerate(samplerate)
  
     for x in range(0,8):
        wave_writer.writeframes(y)
+                        
+ if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Write a wave file containing Numpy generated sine waves')
+    parser.add_argument('--freqs', nargs='+', type=int)
+    parser.add_argument('--amps', nargs='+', type=int)
+    args = parser.parse_args()
+    
+    generate_sample_file(args.freqs, args.amps)
+    
  
Add a main function with command line arguments
## Code Before:

import wave
import numpy as np

def generate_sample_file(test_freqs, test_amps, chunk=4096, samplerate=44100):

   filename = 'Sample_'

   x = np.arange(chunk)
   y = np.zeros(chunk)
   for test_freq,test_amp in zip(test_freqs,test_amps):
      filename += str(test_freq) + 'Hz@' + str(test_amp)
      y = np.add(y, np.sin(2 * np.pi * test_freq * x / samplerate) * test_amp)

   filename += '.wav'
   y = y.astype('i2')

   wave_writer = wave.open(filename, mode='wb')

   wave_writer.setnchannels(1)
   wave_writer.setsampwidth(2)
   wave_writer.setframerate(samplerate)

   for x in range(0,8):
      wave_writer.writeframes(y)

## Instruction:
Add a main function with command line arguments
## Code After:

import wave
import argparse
import numpy as np

def generate_sample_file(test_freqs, test_amps, chunk=4096, samplerate=44100):
   
   filename = 'Sample'

   x = np.arange(chunk)
   y = np.zeros(chunk)
   for test_freq,test_amp in zip(test_freqs,test_amps):
      filename += '_' + str(test_freq) + 'Hz@' + str(test_amp)
      y = np.add(y, np.sin(2 * np.pi * test_freq * x / samplerate) * test_amp)

   filename += '.wav'
   y = y.astype('i2')

   wave_writer = wave.open(filename, mode='wb')

   wave_writer.setnchannels(1)
   wave_writer.setsampwidth(2)
   wave_writer.setframerate(samplerate)

   for x in range(0,8):
      wave_writer.writeframes(y)
                       
if __name__ == '__main__':
   parser = argparse.ArgumentParser(description='Write a wave file containing Numpy generated sine waves')
   parser.add_argument('--freqs', nargs='+', type=int)
   parser.add_argument('--amps', nargs='+', type=int)
   args = parser.parse_args()
   
   generate_sample_file(args.freqs, args.amps)
   
