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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
#!/usr/bin/python
# Audio Tools, a module and set of tools for manipulating audio data
# Copyright (C) 2007-2015 Brian Langenberger
# 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 2 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.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import unittest
import audiotools
import tempfile
import os
import os.path
from hashlib import md5
import random
import decimal
import test_streams
import subprocess
try:
from configparser import SafeConfigParser
except ImportError:
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read("test.cfg")
def do_nothing(self):
pass
# add a bunch of decorator metafunctions like LIB_CORE
# which can be wrapped around individual tests as needed
for section in parser.sections():
for option in parser.options(section):
if parser.getboolean(section, option):
vars()["%s_%s" % (section.upper(),
option.upper())] = lambda function: function
else:
vars()["%s_%s" % (section.upper(),
option.upper())] = lambda function: do_nothing
def BLANK_PCM_Reader(length, sample_rate=44100, channels=2,
bits_per_sample=16, channel_mask=None):
from audiotools.decoders import SameSample
if channel_mask is None:
channel_mask = int(audiotools.ChannelMask.from_channels(channels))
return SameSample(sample=1,
total_pcm_frames=length * sample_rate,
sample_rate=sample_rate,
channels=channels,
channel_mask=channel_mask,
bits_per_sample=bits_per_sample)
class EXACT_RANDOM_PCM_Reader(object):
def __init__(self, pcm_frames,
sample_rate=44100, channels=2, bits_per_sample=16,
channel_mask=None):
self.sample_rate = sample_rate
self.channels = channels
if channel_mask is None:
self.channel_mask = \
int(audiotools.ChannelMask.from_channels(channels))
else:
self.channel_mask = channel_mask
self.bits_per_sample = bits_per_sample
self.total_frames = pcm_frames
self.original_frames = self.total_frames
self.read = self.read_opened
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def read_opened(self, pcm_frames):
if self.total_frames > 0:
frames_to_read = min(pcm_frames, self.total_frames)
frame = audiotools.pcm.FrameList(
os.urandom(frames_to_read *
(self.bits_per_sample // 8) *
self.channels),
self.channels,
self.bits_per_sample,
True,
True)
self.total_frames -= frame.frames
return frame
else:
return audiotools.pcm.empty_framelist(
self.channels, self.bits_per_sample)
def read_closed(self, pcm_frames):
raise ValueError("unable to read closed stream")
def close(self):
self.read = self.read_closed
def reset(self):
self.read = self.read_opened
self.total_frames = self.original_frames
class RANDOM_PCM_Reader(EXACT_RANDOM_PCM_Reader):
def __init__(self, length,
sample_rate=44100, channels=2, bits_per_sample=16,
channel_mask=None):
EXACT_RANDOM_PCM_Reader.__init__(
self,
pcm_frames=length * sample_rate,
sample_rate=sample_rate,
channels=channels,
bits_per_sample=bits_per_sample,
channel_mask=channel_mask)
def EXACT_BLANK_PCM_Reader(pcm_frames, sample_rate=44100, channels=2,
bits_per_sample=16, channel_mask=None):
from audiotools.decoders import SameSample
if channel_mask is None:
channel_mask = int(audiotools.ChannelMask.from_channels(channels))
return SameSample(sample=1,
total_pcm_frames=pcm_frames,
sample_rate=sample_rate,
channels=channels,
channel_mask=channel_mask,
bits_per_sample=bits_per_sample)
def EXACT_SILENCE_PCM_Reader(pcm_frames, sample_rate=44100, channels=2,
bits_per_sample=16, channel_mask=None):
from audiotools.decoders import SameSample
if channel_mask is None:
channel_mask = int(audiotools.ChannelMask.from_channels(channels))
return SameSample(sample=0,
total_pcm_frames=pcm_frames,
sample_rate=sample_rate,
channels=channels,
channel_mask=channel_mask,
bits_per_sample=bits_per_sample)
class MD5_Reader(audiotools.PCMReader):
def __init__(self, pcmreader):
audiotools.PCMReader.__init__(
self,
sample_rate=pcmreader.sample_rate,
channels=pcmreader.channels,
channel_mask=pcmreader.channel_mask,
bits_per_sample=pcmreader.bits_per_sample)
self.pcmreader = pcmreader
self.md5 = md5()
def __repr__(self):
return "MD5Reader(%s,%s,%s)" % (self.sample_rate,
self.channels,
self.bits_per_sample)
def reset(self):
if hasattr(self.pcmreader, "reset"):
self.pcmreader.reset()
self.md5 = md5()
def read(self, pcm_frames):
framelist = self.pcmreader.read(pcm_frames)
self.md5.update(framelist.to_bytes(False, True))
return framelist
def close(self):
self.pcmreader.close()
def digest(self):
return self.md5.digest()
def hexdigest(self):
return self.md5.hexdigest()
class Variable_Reader(audiotools.PCMReader):
def __init__(self, pcmreader):
audiotools.PCMReader.__init__(
self,
sample_rate=pcmreader.sample_rate,
channels=pcmreader.channels,
channel_mask=pcmreader.channel_mask,
bits_per_sample=pcmreader.bits_per_sample)
self.pcmreader = audiotools.BufferedPCMReader(pcmreader)
self.md5 = md5()
self.range = range(self.channels * (self.bits_per_sample // 8),
4096)
def read(self, pcm_frames):
return self.pcmreader.read(random.choice(self.range))
def close(self):
self.pcmreader.close()
class Join_Reader(audiotools.PCMReader):
# given a list of 1 channel PCM readers,
# combines them into a single reader
# a bit like PCMCat but across channels instead of PCM frames
def __init__(self, pcm_readers, channel_mask):
if len({r.sample_rate for r in pcm_readers}) != 1:
raise ValueError("all readers must have the same sample rate")
if len({r.bits_per_sample for r in pcm_readers}) != 1:
raise ValueError("all readers must have the same bits per sample")
if {r.channels for r in pcm_readers} != {1}:
raise ValueError("all readers must be 1 channel")
assert(isinstance(channel_mask, int))
audiotools.PCMReader.__init__(
self,
sample_rate=pcm_readers[0].sample_rate,
channels=len(pcm_readers),
channel_mask=channel_mask,
bits_per_sample=pcm_readers[0].bits_per_sample)
self.pcm_readers = pcm_readers
self.readers = map(audiotools.BufferedPCMReader, pcm_readers)
def read(self, pcm_frames):
return audiotools.pcm.from_channels(
[r.read(pcm_frames) for r in self.pcm_readers])
def reset(self):
for r in self.pcm_readers:
r.reset()
self.readers = map(audiotools.BufferedPCMReader, self.pcm_readers)
def close(self):
for r in self.pcm_readers:
r.close()
class FrameCounter:
def __init__(self, channels, bits_per_sample, sample_rate, value=0):
self.channels = channels
self.bits_per_sample = bits_per_sample
self.sample_rate = sample_rate
self.value = value
def __repr__(self):
return "FrameCounter(%d %d %d %d)" % \
(self.channels,
self.bits_per_sample,
self.sample_rate,
self.value)
def update(self, f):
self.value += len(f)
def __int__(self):
return int(round(decimal.Decimal(self.value) /
(self.channels *
(self.bits_per_sample // 8) *
self.sample_rate)))
# probstat does this better, but I don't want to require that
# for something used only rarely
def Combinations(items, n):
if n == 0:
yield []
else:
for i in range(len(items)):
for combos in Combinations(items[i + 1:], n - 1):
yield [items[i]] + combos
def Possibilities(*lists):
if len(lists) == 0:
yield ()
else:
remainder = list(Possibilities(*lists[1:]))
for item in lists[0]:
for rem in remainder:
yield (item,) + rem
from_channels = audiotools.ChannelMask.from_channels
# these are combinations that tend to occur in nature
SHORT_PCM_COMBINATIONS = \
((11025, 1, int(from_channels(1)), 8),
(22050, 1, int(from_channels(1)), 8),
(22050, 1, int(from_channels(1)), 16),
(32000, 2, int(from_channels(2)), 16),
(44100, 1, int(from_channels(1)), 16),
(44100, 2, int(from_channels(2)), 16),
(48000, 1, int(from_channels(1)), 16),
(48000, 2, int(from_channels(2)), 16),
(48000, 6, int(audiotools.ChannelMask.from_fields(
front_left=True,
front_right=True,
front_center=True,
low_frequency=True,
back_left=True,
back_right=True)), 16),
(192000, 2, int(from_channels(2)), 24),
(96000, 6, int(audiotools.ChannelMask.from_fields(
front_left=True,
front_right=True,
front_center=True,
low_frequency=True,
back_left=True,
back_right=True)), 24))
with open("test_cover1.jpg", "rb") as c:
TEST_COVER1 = c.read()
with open("test_cover2.png", "rb") as c:
TEST_COVER2 = c.read()
with open("test_cover3.jpg", "rb") as c:
TEST_COVER3 = c.read()
with open("test_cover4.png", "rb") as c:
TEST_COVER4 = c.read()
# this is a very large, plain BMP encoded as bz2
with open("huge.bmp.bz2", "rb") as c:
HUGE_BMP = c.read()
from test_formats import *
from test_core import *
from test_metadata import *
from test_utils import *
if (__name__ == '__main__'):
unittest.main()
|