1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from __future__ import division
28
29 import numpy
30
32 """A class that mimics audiolab.sndfile but generates noise instead of reading
33 a wave file. Additionally it can be told to have a "broken" header and thus crashing
34 in the middle of the file. Also useful for testing ultra-short files of 20 samples."""
35
36 - def __init__(self, num_frames, has_broken_header=False):
37 self.seekpoint = 0
38 self.num_frames = num_frames
39 self.has_broken_header = has_broken_header
40
41 - def seek(self, seekpoint):
42 self.seekpoint = seekpoint
43
45 return self.num_frames
46
49
52
54 if self.has_broken_header and self.seekpoint + frames_to_read > self.num_frames // 2:
55 raise IOError()
56
57 num_frames_left = self.num_frames - self.seekpoint
58 if num_frames_left < frames_to_read:
59 will_read = num_frames_left
60 else:
61 will_read = frames_to_read
62 self.seekpoint += will_read
63 return numpy.random.random(will_read)*2 - 1
64
65
67 """
68 Return a valid uri (file scheme) from absolute path name of a file
69
70 >>> path2uri('/home/john/my_file.wav')
71 'file:///home/john/my_file.wav'
72
73 >>> path2uri('C:\Windows\my_file.wav')
74 'file:///C%3A%5CWindows%5Cmy_file.wav'
75 """
76 import urlparse, urllib
77
78 return urlparse.urljoin('file:', urllib.pathname2url(path))
79
80
82 """
83 Check a media source as a valid file or uri and return the proper uri
84 """
85
86 import gst
87
88 if gst.uri_is_valid(source):
89 uri_protocol = gst.uri_get_protocol(source)
90 if gst.uri_protocol_is_supported(gst.URI_SRC, uri_protocol):
91 return source
92 else:
93 raise IOError('Invalid URI source for Gstreamer')
94
95
96 import os.path
97 if os.path.exists(source):
98
99 pathname = os.path.abspath(source)
100
101 uri = path2uri(pathname)
102
103 return get_uri(uri)
104 else:
105 raise IOError('Failed getting uri for path %s: not such file or directoy' % source)
106
107 return uri
108
138
139
140
141 if __name__ == "__main__":
142
143 from tests.unit_timeside import run_test_module
144
145 from tests import test_decoder_utils
146
147 run_test_module(test_decoder_utils)
148