User:Commander Keane/Audio workflow/xTestMic2.py

From Wiktionary, the free dictionary
Jump to navigation Jump to search
  1. !/usr/bin/python
  2. -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals

import pywikibot from pywikibot import i18n, pagegenerators from pywikibot.bot import (

   MultipleSitesBot, FollowRedirectPageBot, ExistingPageBot

) import pyaudio import msvcrt import wave import os.path

class test():

   def recTest(self):


       masterList=['test']
       upto = 0
       fileNameGlobal = 'C:\\Users\\jim\\pywikitest\\TESTING456\\test456.txt'
       
       FORMAT = pyaudio.paInt16
       CHANNELS = 2
       RATE = 44100
       CHUNK = 1024
       RECORD_SECONDS = 4
       #input_device_index = 2
        
       audio = pyaudio.PyAudio()
       # start Recording
       stream = audio.open(format=FORMAT, channels=CHANNELS,
                       rate=RATE, input=True,
                       frames_per_buffer=CHUNK, input_device_index=2)
       #print "recording..."
       frames = []
       print()     
       print ("         RECORDING  - Press SPACE to save and continue, ")
       print ("                      [k] to skip or [q] to quit")
       print ("                      [p] to pause")
       #newChar="g"
       
       
       while 1:
           data = stream.read(CHUNK)
           frames.append(data)
           if msvcrt.kbhit():
               newChar = msvcrt.getch().decode('utf-8')
               if newChar==" ":
                   break
               else:
                   return newChar                
                
       print()           
       print ("         FINISHED RECORDING")
       print()


       fileTitle=masterList[upto]
       # stop Recording
       stream.stop_stream()
       stream.close()
       audio.terminate()
       
       #newFileName = 'C:\\Users\\jim\\pywikitest2\\' + str(fileTitle)+ '.wav'
       index2=fileNameGlobal.rfind('.txt')
       trimmed2=fileNameGlobal[0:index2+0]



       #newFileName = trimmed2.decode('utf8') + u'\\' + fileTitle.decode('utf8')+ u'.wav' #create new folder for each file list
       newFileName = r'C:\Users\jim\pywikibot\Command shortcuts\Mic Test.wav'
       #print newFileName
       newDir = trimmed2 + '\\'
       if os.path.isdir(newDir)==False:
           os.mkdir(trimmed2)
           
       #print newDir
       num=1
       
       if os.path.isfile(newFileName):
           
           while os.path.isfile(newFileName):
               num=num+1
               newFileName= trimmed2 + '\\' + fileTitle + str(num) + '.wav'
               #print newFileName
       #newFileName = 'C:\\Users\\jim\\pywikitest2\\' + str(fileTitle)+ '.wav'
       


       waveFile = wave.open(newFileName, 'wb')
       waveFile.setnchannels(CHANNELS)
       waveFile.setsampwidth(audio.get_sample_size(FORMAT))
       waveFile.setframerate(RATE)
       waveFile.writeframes(b.join(frames))
       waveFile.close
       if msvcrt.kbhit():
               newChar = msvcrt.getch().decode('utf-8')
               return newChar


       return newChar    

def main(*args):

   print("Test678")
   bot = test().recTest()
   

if __name__ == '__main__':

   main()