def test_play(self):
        ab_desc = [(493.88, 0.3, 0.5, 0.1), (440, 2.0, 0.3, 0.2)]
        ab = basic_wave.Wave(ab_desc)
        ab_copy = basic_wave.Wave(ab_desc)

        for t in range(1, int(basic_wave.FRAMERATE / ab_desc[0][0] + 1)):
            expected_play_frame = ab_copy.calculate_frame()
            epf_bytes = basic_wave._limit(int(expected_play_frame)).to_bytes(
                2, byteorder='little', signed=True)
            self.assertEqual(ab.play(), epf_bytes)
 def test_add_t_value(self):
     a_desc = [(440, 2.0, 0.3, 0.2)]
     a = basic_wave.Wave(a_desc, t=100)
     b_desc = [(493.88, 0.3, 0.5, 0.1)]
     b = basic_wave.Wave(b_desc)
     ab = a + b
     self.assertEqual(100, ab.t)
     ba = b + a
     self.assertEqual(0, ba.t)
     a += b
     self.assertEqual(100, a.t)
    def test_calculate(self):
        ab_desc = [(493.88, 0.3, 0.5, 0.1), (440, 2.0, 0.3, 0.2)]
        ab = basic_wave.Wave(ab_desc)
        ab_copy = basic_wave.Wave(ab_desc)

        for t in range(0, int(basic_wave.FRAMERATE / ab_desc[0][0] + 1)):
            frame = 0
            for b_wave in ab_copy.frequencies:
                frame += b_wave.calculate_frame(t)
            offset = 0.0
            for b_wave in ab_copy.frequencies:
                offset += b_wave.offset
            expected_frame = frame * 32767.0 + offset * 32767.0
            self.assertEqual(ab.calculate_frame(), expected_frame)
 def test_init_wav_desc(self):
     ab_desc = [(493.88, 0.3, 0.5, 0.1, [], 1.0),
                (440, 2.0, 0.3, 0.2, [], 0.1)]
     ab = basic_wave.Wave(ab_desc)
     # Ignore order of frequencies
     self.assertIn(*ab.frequencies[0]._wave_description(), ab_desc)
     self.assertIn(*ab.frequencies[1]._wave_description(), ab_desc)
 def test_wave_description(self):
     ab_desc = [(493.88, 0.0, 0.5, 0.0, [], 0.7),
                (440, 0.0, 0.7, 0.0, [], 0.4)]
     ab = basic_wave.Wave(ab_desc)
     self.assertEqual(ab._wave_description(), ab_desc)
 def test_init_t_value(self):
     ab_desc = [(493.88, 0.3, 0.5, 0.1), (440, 2.0, 0.3, 0.2)]
     ab = basic_wave.Wave(ab_desc, t=100)
     self.assertEqual(ab.t, 100)
     ab = basic_wave.Wave(ab_desc)
     self.assertEqual(0, ab.t)
 def test_init_wav_desc_does_not_contain_tuble_NotImplemented(self):
     with self.assertRaises(NotImplementedError):
         basic_wave.Wave(['No wave desc'])
 def test_init_wav_desc_not_a_list_NotImplemented(self):
     with self.assertRaises(NotImplementedError):
         basic_wave.Wave('No wave desc')
 def test_add_return(self):
     a_w = basic_wave.BasicWave(440)
     b_w = basic_wave.BasicWave(493.88)
     x_w = basic_wave.Wave([(770, 0.0, 1.0, 0.0)])
     self.assertIsInstance(a_w + b_w, basic_wave.Wave)
     self.assertIsInstance(a_w + x_w, basic_wave.Wave)
Example #10
0
CHANNELS = 2
SAMPLEWIDTH = 2
FRAMERATE = 44100
NFRAMES = 5*FRAMERATE

a = basic_wave.BasicWave(440)
a_p = basic_wave.BasicWave(440, phi=math.pi/2)

with wave.open('a.wav', 'wb') as wav:
    wav.setparams((CHANNELS, SAMPLEWIDTH, FRAMERATE, NFRAMES,
                   'NONE', 'not compressed'))
    frames = []
    for i in range(NFRAMES):
        frames.append(a.play())
        frames.append(a_p.play())
    wav.writeframes(b''.join(frames))

bc = basic_wave.Wave([(493.88, 0.0, 1.0, 0.0), (523.25, 0.0, 1.0, 0.0)])
bc_p = basic_wave.Wave([(493.88, math.pi/2, 1.0, 0.0),
                        (523.25, math.pi/2, 1.0, 0.0)])

with wave.open('bc.wav', 'wb') as wav:
    wav.setparams((CHANNELS, SAMPLEWIDTH, FRAMERATE, NFRAMES,
                   'NONE', 'not compressed'))
    frames = []
    for i in range(NFRAMES):
        frames.append(bc.play())
        frames.append(bc_p.play())
    wav.writeframes(b''.join(frames))