Skip to content

Commit

Permalink
Add lameenc test script (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Aug 18, 2023
1 parent cac0014 commit c31018d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions server/pypi/packages/lameenc/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest


class TestLameenc(unittest.TestCase):

# https://github.com/chrisstaite/lameenc/blob/master/README.md
def test_basic(self):
import lameenc
import os

BIT_RATE = 128
SAMPLE_RATE = 16000
CHANNELS = 2
BITS_PER_SAMPLE = 16

encoder = lameenc.Encoder()
encoder.set_bit_rate(BIT_RATE)
encoder.set_in_sample_rate(SAMPLE_RATE)
encoder.set_channels(CHANNELS)
encoder.set_quality(2) # 2-highest, 7-fastest

# Generate 1 second of noise
pcm = os.urandom(SAMPLE_RATE * CHANNELS * BITS_PER_SAMPLE // 8)

mp3_data = encoder.encode(pcm)
mp3_data += encoder.flush()

# Header starts at a byte boundary with an 11-bit sync word, which is all 1s.
self.assertEqual(mp3_data[0], 0xff)
self.assertIn(mp3_data[1] & 0xf0, [0xe0, 0xf0])

# Length should be approximately equal to bit rate.
byte_rate = BIT_RATE * 1000 // 8
self.assertGreater(len(mp3_data), byte_rate * 0.75)
self.assertLess(len(mp3_data), byte_rate * 1.25)

0 comments on commit c31018d

Please sign in to comment.