Skip to content

Commit

Permalink
Set bit depth to 32
Browse files Browse the repository at this point in the history
24 would be sufficient, but there's no such thing as int24 in numpy. So
that's a bit trickier.
  • Loading branch information
evenbrenden committed Apr 14, 2021
1 parent 15d3cba commit 8b0866e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SampleScanner is a command-line tool to turn MIDI instruments (usually hardware)
- Clipping detection at sample time
- 100% Python to enable cross-platform compatibility
- Has been known to work in Windows, Mac OS and Linux
- Currently 32-bit recording only (must be supported by audio interface)

## Installation

Expand Down
4 changes: 2 additions & 2 deletions lib/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy

neg80point8db = 0.00009120108393559096
bit_depth = 16
bit_depth = 32
default_silence_threshold = (neg80point8db * (2 ** (bit_depth - 1))) * 4
NUMPY_DTYPE = numpy.int16 if bit_depth == 16 else numpy.int24
NUMPY_DTYPE = numpy.int16 if bit_depth == 16 else numpy.int32
SAMPLE_RATE = 48000

EXIT_ON_CLIPPING = True
Expand Down
9 changes: 5 additions & 4 deletions lib/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

CHUNK_SIZE = 1024
NUM_CHANNELS = 2
FORMAT = pyaudio.paInt16 if bit_depth == 16 else pyaudio.paInt24
PA_FORMAT = pyaudio.paInt16 if bit_depth == 16 else pyaudio.paInt32
FORMAT_CHAR = 'h' if bit_depth == 16 else 'i'

if not sys.platform == "win32":
GO_UP = "\033[F"
Expand Down Expand Up @@ -87,7 +88,7 @@ def record(
input_device_index = get_input_device_index(p, audio_interface_name)

stream = p.open(
format=FORMAT,
format=PA_FORMAT,
channels=NUM_CHANNELS,
rate=sample_rate,
input=True,
Expand Down Expand Up @@ -216,7 +217,7 @@ def record(
for chunk in data:
r = numpy.concatenate((r, chunk), axis=1)

sample_width = p.get_sample_size(FORMAT)
sample_width = p.get_sample_size(PA_FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
Expand Down Expand Up @@ -258,7 +259,7 @@ def save_to_file(path, sample_width, data, sample_rate=SAMPLE_RATE):
write_chunk_size = 512
for chunk_start in range(0, len(flattened), write_chunk_size):
chunk = flattened[chunk_start:chunk_start + write_chunk_size]
packstring = '<' + ('h' * len(chunk))
packstring = '<' + (FORMAT_CHAR * len(chunk))
wf.writeframes(pack(packstring, *chunk))
wf.close()

Expand Down
6 changes: 3 additions & 3 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

def test_sample_value_to_db():
assert str(sample_value_to_db(0)) == "-inf"
assert sample_value_to_db(1) == -90.30899869919435
assert sample_value_to_db(1) == -186.63859731166835


def test_percent_to_db():
assert percent_to_db(0.000030518) == -90.30887862628592
assert percent_to_db(4.656612873077392e-10) == -186.63859731166835
assert str(percent_to_db(0)) == "-inf"


def test_dbfs_as_percent():
minimum_16_bit_dbfs = -90.30899869919435
minimum_16_bit_dbfs = -186.63859731166835
assert dbfs_as_percent(minimum_16_bit_dbfs) == 0
assert dbfs_as_percent(0) == 1

0 comments on commit 8b0866e

Please sign in to comment.