nothing@1: from os.path import join nothing@1: from rdflib import Graph, BNode, Namespace, RDF, RDFS, Literal, URIRef, XSD, OWL nothing@1: import glob, wave nothing@1: nothing@1: class Adc2004Converter: nothing@1: def __init__(self): nothing@1: self.data_dir = "./data" nothing@1: self.destination = "./rdf" nothing@1: nothing@1: def run(self): nothing@1: for path in glob.glob(self.data_dir + "/*REF.txt"): nothing@1: self.createGraph() nothing@1: with open(path, "r") as adc_file: nothing@1: data = adc_file.read() nothing@1: adc_file.close() nothing@1: audio_data = self.get_audio_data(path.replace("REF.txt", ".wav")) nothing@1: self.convert(data, audio_data) nothing@1: write_path = self.destination + "/" + path.split("/")[-1].split(".")[0] + ".n3" nothing@1: self.graph.serialize(write_path, format="n3") nothing@1: nothing@1: def bindNamespaces(self): nothing@1: self.ns = { nothing@1: 'afv': Namespace("https://w3id.org/afo/vocab/1.1#"), nothing@1: 'afo': Namespace("https://w3id.org/afo/onto/1.1#"), nothing@1: 'tl': Namespace("http://purl.org/NET/c4dm/timeline.owl#"), nothing@1: 'event': Namespace("http://purl.org/NET/c4dm/event.owl#"), nothing@1: 'mo': Namespace("http://purl.org/ontology/mo/"), nothing@1: 'sxsd': Namespace("https://www.w3.org/TR/speech-synthesis11/synthesis-nonamespace.xsd#") nothing@1: } nothing@1: for key in self.ns: nothing@1: self.graph.bind(key, self.ns[key]) nothing@1: nothing@1: def createGraph(self): nothing@1: self.graph = Graph() nothing@1: self.bindNamespaces() nothing@1: nothing@1: def convert(self, data, audio_data): nothing@1: self.signal = BNode() nothing@2: self.file = URIRef(audio_data['path'].split("/")[-1]) nothing@1: self.timeline = BNode() nothing@1: self.interval = BNode() nothing@1: duration = audio_data['n_frames'] / audio_data['f_rate'] nothing@1: nothing@1: self.graph.add(( self.signal, RDF.type, self.ns['mo']['Signal'] )) nothing@1: self.graph.add(( self.file, RDF.type, self.ns['mo']['AudioFile'] )) nothing@1: self.graph.add(( self.timeline, RDF.type, self.ns['mo']['Timeline'] )) nothing@1: self.graph.add(( self.interval, RDF.type, self.ns['tl']['Interval'] )) nothing@1: self.graph.add(( self.file, self.ns['mo']['encodes'], self.signal )) nothing@1: self.graph.add(( self.signal, self.ns['mo']['sample_rate'], Literal(audio_data['f_rate']) )) nothing@1: self.graph.add(( self.signal, self.ns['mo']['channels'], Literal(audio_data['n_channels']) )) nothing@1: self.graph.add(( self.signal, self.ns['mo']['time'], self.interval )) nothing@1: self.graph.add(( self.interval, self.ns['tl']['duration'], Literal(str(duration), datatype=XSD.duration) )) nothing@1: self.graph.add(( self.interval, self.ns['tl']['timeline'], self.timeline )) nothing@1: nothing@1: index = 0 nothing@1: for row in data.split("\n"): nothing@1: if row != "": nothing@1: time, freq = row.split(" ") nothing@1: event_id = BNode("event_" + str(index)) nothing@1: interval_id = BNode() nothing@1: self.graph.add(( event_id, RDF.type, self.ns['afv']['FundamentalFrequency'] )) nothing@1: self.graph.add(( event_id, self.ns['afo']['value'], Literal(str(float(freq)), datatype=self.ns['sxsd']['hertz.number']) )) nothing@1: self.graph.add(( event_id, self.ns['event']['time'], interval_id )) nothing@1: self.graph.add(( interval_id, self.ns['tl']['at'], Literal(time, datatype=XSD.float) )) nothing@1: self.graph.add(( interval_id, self.ns['tl']['duration'], Literal((256.0/44100.0), datatype=XSD.duration) )) nothing@1: self.graph.add(( interval_id, self.ns['tl']['timeline'], self.timeline )) nothing@1: index += 1 nothing@1: nothing@1: def get_audio_data(self, path): nothing@1: audio_data = {} nothing@1: wave_read = wave.open(path, 'rb') nothing@1: audio_data['n_channels'] = wave_read.getnchannels() nothing@1: audio_data['n_frames'] = wave_read.getnframes() nothing@1: audio_data['s_width']= wave_read.getsampwidth() nothing@1: audio_data['f_rate'] = wave_read.getframerate() nothing@1: audio_data['path'] = path nothing@1: wave_read.close() nothing@1: return audio_data nothing@1: nothing@1: def main(): nothing@1: Adc2004Converter().run() nothing@1: nothing@1: if __name__ == "__main__": nothing@1: main()