eKorpus

Source code for ekorpus.controllers.sounds

"""The :mod:`sounds` contains various methods for playing sound files.
"""
import os
import pylons
import shutil
import thread
from ekorpus.lib.base import *

root = 'ekorpus/public/'
upload_folder = 'files/'
rec_folder = 'recordings/'
seg_folder = 'segments/'
seg_ext = '.wav'

slice_creation_lock = thread.allocate_lock()

[docs]def save(wave, recording): name = str(recording.id)+seg_ext full_path = root+upload_folder+rec_folder+name f = open(full_path, 'wb') shutil.copyfileobj(wave.file, f) wave.file.close() f.close() recording.file=upload_folder+rec_folder+name
[docs]def slice(first, last): # @todo: throw 404 if file can not be created # @todo: sync creation # @todo: multisegment slices name = upload_folder+seg_folder+str(first) if last: name = name +'-'+ str(last) name = name+seg_ext file_path = root+name #h.log("Sound file: "+os.path.abspath(file_path)+" "+`os.path.exists(file_path)`) us = False if slice_creation_lock.locked(): print "### ACK:",`slice_creation_lock` us = slice_creation_lock.acquire() #us = True try: if not os.path.exists(file_path): if not us: print "### ACK:",`slice_creation_lock` us = slice_creation_lock.acquire() #us = True if not os.path.exists(file_path): segment = model.Segment.get_by(id=first) recording = segment.segmentation.recording segmentation = segment.segmentation if segment: end = segment.end if segmentation.type=="sentence" : end = end + 1 start = float(segment.start)/1000.0 end = float(segment.end)/1000.0 length = end-start sox_path = pylons.config['sox_path'] os.spawnl(os.P_WAIT, sox_path, 'sox', root+recording.file, file_path, 'trim', str(start), str(length), 'pad', str(0), str(0.1)) print "### trim:",str(start), str(length) statinfo = os.stat(file_path) except: abort(404) finally: if us: print "### REL:",`slice_creation_lock` us = False slice_creation_lock.release() return '/'+name
[docs]class SoundsController(BaseController):
[docs] def index(self): return Response('nope')
[docs] def segment(self, first, last): """Return a *redirect* to concecutive segments of a recording file.""" if not first: abort(404) id = first compressed = id.endswith(".mp3") or id.endswith(".ogg") if id.endswith(seg_ext) or compressed: id = id[:-4] if id.find('.')!=-1: abort(404) if first.startswith('i_'): try: id = id[2:] item = model.Item.get_by(id=id) if not item: return id = item.first_id; except: abort(404) elif first.startswith('s_'): id = id[2:] if compressed: return h.redirect_to(str('/'+upload_folder+seg_folder+first)) return h.redirect_to(slice(id,None))
[docs] def recording(self, id): """Return a *redirect* to full recording file.""" recording = model.Recording.get_by(id=id) if recording.file: return h.redirect_to('/'+recording.file) return