Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleans up & renames swar2freq() method as swar_to_frequency() #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

onstash
Copy link
Contributor

@onstash onstash commented Dec 31, 2017

This cleaning up and renaming was done keep readability and performance in mind. It is more of an enhancement. 馃槃

@onstash
Copy link
Contributor Author

onstash commented Dec 31, 2017

Feedback is welcome for my approach. cc @amoghpj @reckoner165 馃槂

@@ -4,82 +4,98 @@
import struct
import pyaudio

def swar2freq(swar,basefreq):
SWAR_FREQUENCY_MAPPER = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should move SWAR_FREQUENCY_MAPPER and VALID_SWAR_INPUT subsets into a separate notes.txt or so since they're constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping them as a global dict and set (respectively) is better than loading them from a text / JSON file as the latter involves loading of files which in turn involves more computation (comparitively) than the former. 馃槄


}

"""
For any other input, including captialization, this function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to use capitalization for some thing else. Not sure right now, but I might consider using capital letters for shuddh swars and small letters for komal/teevr swars.

first_swar = swar[0]
is_swar_valid = first_swar in VALID_SWAR_INPUTS
if not is_swar_valid:
return SWAR_FREQUENCY_MAPPER.get('0')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never seen get() before. Learned something new! Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two reasons why dict.get() is used to fetch data -

  • If a key is absent in dict[key], it will raise a KeyError which needs to be handled downstream by a try-except block.
  • You can specify a fallback value -
    For example if the name "Foo" is unavailable in a dict BABY_NAMES (not a great baby name, I know 馃槤), then BABY_NAMES.get("Foo") would return None. If you have a unique fallback say "Bar", you can specify that like this BABY_NAMES.get("Foo", "Bar"). dict.get(key, fallback_value) is what is recommended by experienced developers for better readability and debuggability (I think I made this word up).

dict.get(key, fallback_value) is the same as

value = dict.get(key)
if value is None:
    value = <default_value>

return int(shuddh_frequency * base_frequency_float)

if swar_length == 2:
if first_swar in VALID_SWAR_INPUTS_SUBSET_1:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable name first_swar doesn't make sense. The first character (< or >) specifies the octave. Call this octave specifier maybe? @onstash

Copy link
Contributor Author

@onstash onstash Jan 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By all means. I called it first_swar because it was the first entity in swar list. 馃槄

'0': 0,
}
VALID_SWAR_INPUTS_SUBSET_1 = set(['<', '>'])
VALID_SWAR_INPUTS_SUBSET_2 = set(['s', 'r', 'g', 'm', 'p', 'd', 'n'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely seems very systematic, but can you explain why this needs to be done this way? The type conversions from list to set to list back to set?

Copy link
Contributor Author

@onstash onstash Jan 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read somewhere that looking up entities in a set is faster than looking up entities in a list. Unfortunately, I am unable to find the blog post / stackoverflow discussion link related to this. On the other hand, the gain from the "lookup is faster in set" theory might be notable if the data size is large enough. We're operating with < 100 entities. In that case, this is an unnecessary premature optimization. I will revert this list to set conversion as it has introduced confusion and possibly premature optimization instead of solving a (non-existent) performance issue. Sorry for the confusion. 馃槄

@onstash
Copy link
Contributor Author

onstash commented Jan 4, 2018

@amoghpj @reckoner165 Please point out where variable names don't make sense from a musical perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants