FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • ssb22/gradint
  • st822/gradint
2 results
Show changes
Showing
with 12 additions and 280 deletions
#!/usr/bin/env python
# Program to support splitting a long sound file into
# several little ones.
# Needs 'sox' - if Windows, download from
# sox.sourceforge.net
# (e.g. http://prdownloads.sourceforge.net/sox/sox12172.zip
# - note gives a "select a mirror" dialogue) and put sox.exe
# in the same directory or on the path
# -----------------------
# lowpri: 2nd sort key by length ? (only matters if adding a lot of new words & phrases at same time)
import time,os,sndhdr,sys
try: import winsound
except: winsound=None
macsound = (sys.platform.find("mac")>=0 or sys.platform.find("darwin")>=0)
if macsound: sys.stderr.write("Warning: You need to have qtplay (from gradint or wherever) in your PATH for this to work\n")
def rawcut(allData,fromSecs,toSecs,rate=22050,bits=16,channels=1):
return allData[secbyte(fromSecs,rate,channels,bits):secbyte(toSecs,rate,channels,bits)]
def secbyte(sec,rate,channels,bits):
# Convert a time in seconds to a byte offset in the raw
# data
# Note: Result MUST be a multiple of bytesPerSample
# 'sec' is not necessarily an integer
sampleNo = int(0.5+sec*rate) # nearest integer sample no
bytesPerSample = channels*int(bits/8)
return sampleNo * bytesPerSample
def readTimings(langs):
if macsound: time.sleep(1) # OS X hack due to qtplay delay (1sec on an Intel 2GHz Core Duo running OSX 10.5)
sys.stdout.write("Starting clock\n")
# Now using time.time() rather than time.clock()
# due to clock units confusion
# Just have to hope the system is accurate enough
offset = time.time()
ret = [] ; ip=''
start = offset
while not ip=='q':
ip = raw_input(langs[len(ret)%len(langs)]+": ")
t = time.time()
if ip=="c" and ret: ret[-1]=(ret[-1][0],t-offset)
elif not ip: ret.append((start-offset,t-offset))
start = t
sys.stdout.write("Finishing at %f seconds\n" % (t-offset,))
return ret
def instructions():
sys.stdout.write("Press Return between samples\n")
sys.stdout.write("Enter 'c' to change the time of the last Return to this one\n")
sys.stdout.write("Enter 'x' to omit this bit (e.g. silence)\n")
sys.stdout.write("Enter 'q' when done (AFTER stopping last sample)\n")
sys.stdout.write("PRESS RETURN TO START\n")
raw_input()
def getParams():
wavFile=raw_input("Enter filename of main recording: ")
header = sndhdr.what(wavFile)
if not header:
sys.stdout.write("Problem opening that file\n")
return None
(wtype,rate,channels,wframes,bits) = header
sys.stdout.write("WAV file is %d-bit\n" % (bits,))
if bits==8: soxBits="-b -u" # unsigned
elif bits==16: soxBits="-w -s" # signed
elif bits==32: soxBits="-l -s" # signed
else:
sys.stdout.write("Unsupported bits per sample '%s'\n" % (bits,))
return None
soxParams = "-t raw %s -r %d -c %d" % (soxBits,rate,channels)
rawFile = wavFile + ".raw"
convertToRaw(soxParams,wavFile,rawFile)
lang1=lang2=None
while not lang1: lang1=raw_input("Enter first language on recording (e.g. zh): ")
interleaved=input("Are two languages interleaved? (1/0): ") # (horrible hack)
if interleaved:
while not lang2: lang2=raw_input("Enter second language on recording (e.g. en): ")
else:
lang2=lang1
sys.stdout.write("OK - should run this program again for other language's recording\n")
return soxParams,wavFile,rawFile,lang1,lang2,rate,bits,channels
def convertToWav(soxParams,rawFile,wavFile):
os.system("sox %s \"%s\" \"%s\"" % (soxParams,rawFile,wavFile))
def convertToRaw(soxParams,wavFile,rawFile):
os.system("sox \"%s\" %s \"%s\"" % (wavFile,soxParams,rawFile))
def main():
tuple=None
while not tuple: tuple=getParams()
soxParams,wavFile,rawFile,lang1,lang2,rate,bits,channels = tuple
mainLoop(soxParams,wavFile,rawFile,lang1,lang2,rate,bits,channels)
os.unlink(rawFile)
# Set lang1 & lang2 equal if not interleaving
def mainLoop(soxParams,wavFile,rawFile,lang1="zh",lang2="en",rate=22050,bits=16,channels=1):
allData=open(rawFile,"rb").read()
open(wavFile,"rb").read() # to cache before starting clock and 'play' (especailly because just loaded the separate raw data) (could also play from raw data if got sox)
instructions()
# Start sound asynchronously - hope for the best that
# the first clock reading is near enough to the actual
# start of the sound
if winsound: winsound.PlaySound(wavFile,winsound.SND_FILENAME | winsound.SND_ASYNC)
elif macsound: os.spawnlp(os.P_NOWAIT,"qtplay","qtplay",wavFile)
# else: os.spawnlp(os.P_NOWAIT,"play","play",wavFile)
# Problem: What if 'play' o/p's at slightly less than the correct rate - will think the cuts are further on in the file than they really are. (e.g. 16000Hz on a z61p Cygwin, "time play" shows it takes slightly longer than sox thinks the file is)
# Better convert to 44100 just to make sure.
else: os.system("sox \"%s\" -r 44100 -t wav - | play -t wav - &" % wavFile)
# Read timings, cut up, and write out the samples
samples = [ rawcut(allData,s,f,rate,bits,channels) for s,f in readTimings([lang1,lang2]) ]
formatString = "%0"+str(len(str(int(len(samples)/(2-(lang2==lang1))-1))))+"d_%s"
# (pad with 0s as necessary so it's in order)
# (len(samples)-1 gives highest number, so len(str(l..))
# gives number of digits in it)
for i in range(len(samples)):
if i%2: lang=lang2
else: lang=lang1
if lang1==lang2: c=i
else: c=int(i/2)
fname = formatString % (c,lang)
f=open(fname, "wb")
f.write(samples[i])
f.close()
convertToWav(soxParams,fname,fname+".wav")
os.unlink(fname)
sys.stdout.write("Written %s.wav\n" % (fname,))
if __name__=="__main__":
main()
#!/usr/bin/env python
# Program to strip any silence from the beginning/end of a
# sound file (must be real 0-bytes not background noise)
# (This is useful as a "splitter" post-processor when
# getting samples from CD-ROMs e.g. "Colloquial Chinese" -
# don't use audacity here because some versions of audacity
# distort 8-bit audio files)
# Needs 'sox' + splitter
from splitter import *
for wavFile in sys.argv[1:]:
# Figure out sox parameters
header = sndhdr.what(wavFile)
if not header: raise IOError("Problem opening %s" % (wavFile,))
(wtype,rate,channels,wframes,bits) = header
if bits==8: soxBits="-b -u" # unsigned
elif bits==16: soxBits="-w -s" # signed
elif bits==32: soxBits="-l -s" # signed
else: raise Exception("Unsupported bits per sample")
soxParams = "-t raw %s -r %d -c %d" % (soxBits,rate,channels)
rawFile = wavFile + ".raw"
# Now ready to convert to raw, and read it in
convertToRaw(soxParams,wavFile,rawFile)
o=open(rawFile,"rb")
allData=o.read()
o.close()
# Now figure out how many samples we can take out
bytesPerSample = channels*int(bits/8)
if bytesPerSample==1: silenceVal=chr(128)
else: silenceVal=chr(0)
startIdx = 0
while startIdx < len(allData):
if not allData[startIdx]==silenceVal: break
startIdx = startIdx + 1
startIdx = int(startIdx/bytesPerSample) * bytesPerSample
endIdx = len(allData)
while endIdx:
if not allData[endIdx-1]==silenceVal: break
endIdx = endIdx - 1
endIdx = endIdx - len(allData) # put it into -ve notatn
endIdx = int(endIdx/bytesPerSample) * bytesPerSample
endIdx = endIdx + len(allData) # avoid 0
sys.stderr.write("Debugger: Clipping %s to %d:%d\n" % (wavFile,startIdx,endIdx))
allData = allData[startIdx:endIdx]
# Write back the file, and convert it back to wav
o=open(rawFile,"wb")
o.write(allData)
o.close()
convertToWav(soxParams,rawFile,wavFile)
# Clean up
os.unlink(rawFile)
all:
echo "Please run 'make' in the directory above, not here."
exit 1
# This file is part of the source code of
# gradint v0.9945 (c) 2002-2009 Silas S. Brown. GPL v3+.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# program to "thin down" the gradint .py for low memory environments
# by taking out some of the code that's unused on that platform
import sys
if "s60" in sys.argv: # S60 version
version = "S60"
to_omit = [
"if use_unicode_filenames:", # WinCE
"if paranoid_file_management:",
'if not extsep==".":',
"if macsound:","elif macsound:",
"if winsound:",
"if unix:",
"if winsound or mingw32:",
"elif unix and not macsound:",
"if gotSox and unix:",
"class SoundCollector(object):",
"def lame_endian_parameters():",
"def soundfile_to_data(file,soxParams):",
# TODO SH sound collector will have problem with indentation due to """..""" strings
"def decode_mp3(file):",
"class Mp3FileCache(object):",
"if outputFile:",
"class InputSourceManager(object):",
"def wavToMp3(directory):",
"def makeMp3Zips(baseDir,outDir,zipNo,direc=None):",
"class RecorderControls:",
"def doRecWords():",
"if app:","elif app:",
"def addStatus(widget,status,mouseOnly=0):",
"def removeStatus(widget):",
"def addButton(parent,text,command,packing=None,status=None):",
"def addLabel(row,label):",
"def CXVMenu(e):",
"def selectAll(e):",
"def selectAllButNumber(e):",
"def addTextBox(row,wide=0):",
"def addLabelledBox(row,wide=0):",
"def addRow(parent,wide=0):",
"def addRightRow(widerow):",
"def make_output_row(parent):",
"def select_userNumber(N,updateGUI=1):",
"def updateUserRow(fromMainMenu=0):",
"def renameUser(i,radioButton,parent,cancel=0):",
"def deleteUser(i):",
"def setupScrollbar(parent,rowNo):",
"def focusButton(button):",
"class ExtraButton(object):",
"def make_extra_buttons_waiting_list():",
"def startTk():",
"def guiVocabList(parsedVocab):",
"def synchronizeListbox(listbox,masterList):",
"if useTK:",
"def openDirectory(dir):",
"if winCEsound:",
"def check_for_slacking():",
"def gui_outputTo_end():",
"def gui_outputTo_start():",
]
else: assert 0, "Unrecognised version on command line"
revertToIndent = -1
lCount = -1
for l in sys.stdin.xreadlines():
lCount += 1
if lCount==2: print "\n# NOTE: this version has been automatically TRIMMED for "+version+" (some non-"+version+" code taken out)\n"
l=l.rstrip()
assert not "\t" in l, "can't cope with tabs"
indentLevel=-1
for i in range(len(l)):
if not l[i]==" ":
indentLevel = i ; break
if indentLevel<0 or indentLevel==len(l) or (revertToIndent>=0 and indentLevel>revertToIndent): continue
revertToIndent = -1
if (l+"#")[:l.find("#")].strip() in to_omit:
print (l+"#")[:l.find("#")]+" pass # trimmed"
revertToIndent = indentLevel
else: print l
File deleted
File deleted
File deleted
File deleted
開頭
开端
今日個堂上完啦
今天的课结束了
而家我哋要等一陣,然後翻溫。喺第一課我哋仲未學習好多嘅詞語,所以停頓會比較長,但係喺未來嘅課程,我哋唔會有咁長嘅停頓
现在我们要等, 然后复习。 在第一课我们还没有学习很多词语所以停顿比较长。 但是在未来的课我们没有这样长的停顿。
意思係
意思是
而家請講
现在请说
請講
请说