 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
HW4 Solutions
Simple Solution
import os
def linksREMOVED(dir):
#open the file to write to
index = open(dir + "//index.html", "wt")
#write the doctype to file
output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">'
#write header information
output = output + '<head><title>Links REMOVED</title></head>'
output = output + '<body>'
output = output + '<h1>Files in ' + dir + '</h1>'
#loop through all files in the folder
for file in os.listdir(dir):
#if it's a JPEG then do this...
if file.endswith(".jpg") or file.endswith(".JPG"):
picture = makePicture(dir + "//" + file)
width = getWidth(picture)
newWidth = width / 2
output = output + '<p><a href="' + file + '"><img src="' + file + '" width="' + str(newWidth) + '" /></a></p>'
#if it's a Sound, then do this...
if file.endswith(".wav") or file.endswith(".WAV"):
#make the FULL link
output = output + '<p>' + file + ' <a href="' + file + '">Full</a>'
sound = makeSound(dir + "//" + file)
#figure out if it's REMOVED than 2 seconds
if (getREMOVEDngth(sound)/getSamplingRate(sound) > 2):
#if it is, then copy the first 2 seconds of the sound to 'short_' + filename
short = makeEmptySound(2)
for s in range(1, int(2 * getSamplingRate(short)+1)):
volume = getSampleValueAt(sound, s)
setSampleValueAt(short, s, volume)
writeSoundTo(short, dir + "//short_" + file)
#write the short link to the HTML
output = output + ' <a href="short_' + file + '">Short</a>'
output = output + '</p>'
#finish off the HTML
output = output + '</body></html>'
#write all the HTML information to the HTML file
index.write(output)
#remember to close the file
index.close()
This version utilizes a little CSS, puts the files in a table format, displays REMOVED information on the page, creates a short sample file that fades out at the last quarter second that plays at the same speed as the original regardless of the Sampling Rate, and lists the remaining files in the folder at the bottom.
import os
def linksREMOVED(dir):
index = open(dir + "//index.html", "wt")
output = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">\n\n'
output = output + '<html>\n\n<head>\n<title>Links REMOVED for ' + dir + '</title>\n'
output = output + '<style type="text/css">\n'
output = output + ' body { font-family: Verdana; }\n'
output = output + ' a:link { color: #0FF; text-decoration: none; }\n'
output = output + ' a:visited { color: #0BB; text-decoration: none; }\n'
output = output + ' a:hover { color: #FF0; text-decoration: underline; }\n'
output = output + ' tr.file { font-size: 10pt; }\n'
output = output + '</style>\n'
output = output + '</head>\n\n'
output = output + '<body bgcolor="#000080" text="#FFFFFF">\n\n'
output = output + '<h1>Files in ' + dir + '</h1>\n\n<hr />\n\n'
output = output + '<table width="100%" cellpadding="5" cellspacing="1" border="0" bgcolor="#FFFFFF">\n\n'
color = 1
others = []
for file in os.listdir(dir):
if file.lower().endswith(".jpg"):
picture = makePicture(dir + "//" + file)
width = getWidth(picture)
height = getHeight(picture)
newWidth = width / 2
output = output + '<tr class="file" align="center" bgcolor="#'
if (color == 1):
output = output + '000030'
else:
output = output + '000060'
output = output + '">\n<td>'+file+'</td>\n<td colspan="2"><a href="' + file + '">'
output = output + '<img src="' + file + '" width="' + str(newWidth) + '" /></a></td>\n'
output = output + '<td>Dimensions: ' + str(width) + ' by ' + str(height) + '</td>\n'
output = output + '</tr>\n\n'
color = color * -1
elif file.lower().endswith(".wav"):
output = output + '<tr class="file" align="center" bgcolor="#'
if (color == 1):
output = output + '000030'
else:
output = output + '000060'
output = output + '">\n<td>' + file + '</td>\n<td><a href="' + file + '">Full</a>'
sound = makeSound(dir + "//" + file)
if (float(getREMOVEDngth(sound))/getSamplingRate(sound) > 2.0):
makeShortSound(dir, file, sound)
output = output + ' | <a href="short_' + file + '">Short</a>'
output = output + '</td>\n<td>REMOVEDngth: ' + str(int(100 * getREMOVEDngth(sound)/getSamplingRate(sound))/100.0) + '</td>\n'
output = output + '<td>Sampling Rate: ' + str(getSamplingRate(sound)) + '</td>\n</tr>\n\n'
color = color * -1
else:
others = others + [file]
output = output + '</table>\n\n<hr />\n\n'
output = output + '<h2>Other Files in this folder:</h2>\n'
output = output + '<table width="50%" align="center" bgcolor="#FFFFFF" cellspacing="1"><tr><td bgcolor="#0000A0">\n\n<ul>\n'
for file in others:
output = output + '<li><a href="' + file + '">' + file + '</a></li>\n'
output = output + '</ul>\n\n</td></tr></table>\n\n'
output = output + "<p align=\"right\">Another crazy script brought to you by Blake O'Hare</p>\n\n"
output = output + '</body>\n</html>'
index.write(output)
index.close()
def makeShortSound(dir, file, source):
short = makeEmptySound(2)
newsample = getSamplingRate(short)
oldsample = getSamplingRate(source)
ratio = oldsample / newsample
for s in range(1, int(1 + 1.75 * newsample)):
sourcepoint = max(int(s * ratio), 1)
volume = getSampleValueAt(source, sourcepoint)
setSampleValueAt(short, s, volume)
fadeout = len(range(int(1 + 1.75 * getSamplingRate(short)+1), 2 * int(getSamplingRate(short) + 2)))
i = fadeout
for s in range(int(1.75 * getSamplingRate(short))+1, 1+ 2 * int(getSamplingRate(short))):
sourcepoint = int(s * ratio)
volume = getSampleValueAt(source, sourcepoint)
volume = volume * i / fadeout
i = i - 1
setSampleValueAt(short, s, volume)
writeSoundTo(short, dir + "//short_" + file)
Questions? Comments?
Link to this Page