From 3912027f5221c71759453489c0821e11690dce9d Mon Sep 17 00:00:00 2001 From: Brandon Skari Date: Sun, 31 Jan 2016 19:18:47 -0700 Subject: [PATCH] Hide Python imports from ipython --- src/python/rpitx/__init__.py | 44 ++---------------------------------- src/python/rpitx/_hidden.py | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 src/python/rpitx/_hidden.py diff --git a/src/python/rpitx/__init__.py b/src/python/rpitx/__init__.py index 6bc48ba..739af6e 100644 --- a/src/python/rpitx/__init__.py +++ b/src/python/rpitx/__init__.py @@ -1,44 +1,4 @@ """Python interface to rpitx.""" -from pydub import AudioSegment -import StringIO -import _rpitx -import array -import logging -import os -import wave - - -def broadcast_fm(file_, frequency): - """Play a music file over FM.""" - - logging.basicConfig() - logger = logging.getLogger('rpitx') - - def _reencode(file_name): - """Returns an AudioSegment file reencoded to the proper WAV format.""" - original = AudioSegment.from_file(file_name) - if original.channels > 2: - raise ValueError('Too many channels in sound file') - if original.channels == 2: - # TODO: Support stereo. For now, just overlay into mono. - logger.info('Reducing stereo channels to mono') - left, right = original.split_to_mono() - original = left.overlay(right) - - return original - - raw_audio_data = _reencode(file_) - - wav_data = StringIO.StringIO() - wav_writer = wave.open(wav_data, 'w') - wav_writer.setnchannels(1) - wav_writer.setsampwidth(2) - wav_writer.setframerate(48000) - wav_writer.writeframes(raw_audio_data.raw_data) - wav_writer.close() - - raw_array = array.array('c') - raw_array.fromstring(wav_data.getvalue()) - array_address, length = raw_array.buffer_info() - _rpitx.broadcast_fm(array_address, length, frequency) +# To avoid import pollution with ipython, hide functions in another module +from _hidden import broadcast_fm diff --git a/src/python/rpitx/_hidden.py b/src/python/rpitx/_hidden.py new file mode 100644 index 0000000..0c1a43d --- /dev/null +++ b/src/python/rpitx/_hidden.py @@ -0,0 +1,43 @@ +"""Hides imports and other irrelevant things so that ipython works nicely.""" + +from pydub import AudioSegment +import StringIO +import _rpitx +import array +import logging +import wave + + +def broadcast_fm(file_, frequency): + """Play a music file over FM.""" + + logging.basicConfig() + logger = logging.getLogger('rpitx') + + def _reencode(file_name): + """Returns an AudioSegment file reencoded to the proper WAV format.""" + original = AudioSegment.from_file(file_name) + if original.channels > 2: + raise ValueError('Too many channels in sound file') + if original.channels == 2: + # TODO: Support stereo. For now, just overlay into mono. + logger.info('Reducing stereo channels to mono') + left, right = original.split_to_mono() + original = left.overlay(right) + + return original + + raw_audio_data = _reencode(file_) + + wav_data = StringIO.StringIO() + wav_writer = wave.open(wav_data, 'w') + wav_writer.setnchannels(1) + wav_writer.setsampwidth(2) + wav_writer.setframerate(48000) + wav_writer.writeframes(raw_audio_data.raw_data) + wav_writer.close() + + raw_array = array.array('c') + raw_array.fromstring(wav_data.getvalue()) + array_address, length = raw_array.buffer_info() + _rpitx.broadcast_fm(array_address, length, frequency)