goxr3plus / java-stream-player

🌌Java Advanced Audio Controller Library (WAV, AU, AIFF, MP3, OGG VORBIS, FLAC, MONKEY's AUDIO and SPEEX audio formats )
GNU General Public License v3.0
145 stars 33 forks source link

Unit tests for gain, new getter for logarithmic gain level. #32

Closed HelgeStenstrom closed 5 years ago

HelgeStenstrom commented 5 years ago

The logarithmic gain getter gets the gain directly from the SourceDataLine. We cannot know for sure that it will be logarithmic, but it seems to behave that way.

The pull request is draft, because it changes the interface of the StreamPlayer, by adding a new public method with a name that has not been reviewed.

HelgeStenstrom commented 5 years ago

From my tests, I saw that the gain range on the dB scale was -80 to 6.02 dB. That means that 0 dB is at half of the available linear range. Now these numbers are not visible in the source code, but are properties of the SourceDataLine that I have. It might even be different on Mac and Windows, or depend on the sound card, I don’t know.

Logarithmic dB scale is probably better to work with in an application than a linear scale, for things like control knobs.

Note that the original gain setting method cannot be called with a linear gain of zero, since that will be converted to minus infinity dB, except that some Exception will be thrown instead.

goxr3plus commented 5 years ago

I need to think about it, I don't fully understand the new change yet, you seem professional on this :)

HelgeStenstrom commented 5 years ago

https://en.wikipedia.org/wiki/Decibel

goxr3plus commented 5 years ago

Thank you i am reading it :)!!!

HelgeStenstrom commented 5 years ago

I haven't read the Wikipedia article myself, but can say this: dB (decibel) is generally a measure of power ratios. A power ratio in dB, of P1/P2 = 10 * log10(P1/P2).

Since power is proportional to the square of the amplitude, given two amplitudes V1 and V2, the power ratio can be expressed as 10 log10( (V1V1) / (V2V2) ) = 20 log10(V1/V2).

A doubled amplitude is approximately 6 dB. 20*log10(2) = 6.02.

In this case, the scale chosen in the implementation of SourceDataLine on my computer is that 0 dB is 6.02 dB below the maximum gain, which means that the linear maximum gain is 2.

Reading the JavaDoc for SourceDataLine, properties for maximum gain, I see nothing about there bein a logarithmic dB scale. There is apparently no guarantee. On my MacBook, the gain scale is logarithmic, but I cannot know if it is so on other computers.

goxr3plus commented 5 years ago

I will merge for now :)