fix AY freq-to-tone calculation#443
Open
ahihi wants to merge 1 commit into
Open
Conversation
telephon
reviewed
Jun 25, 2026
| // Approximate empirical... | ||
| //^(109300 / (freq - 3.70727)) | ||
| ^(110300 / (freq - 0.5)) | ||
| *freqtotone { |freq, s=nil| |
Member
There was a problem hiding this comment.
Great! Thanks for looking into this ... Maybe it would be clearer to just pass the sample rate into the freqtotone method? Then we can also use the method in a synthDef (using the SampleRateugen). And if none is given, it can still be calculated as you do it.
Note that usually we call the argument server and there is no need to specify the default to nil, since the default is nil.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #348, and by extension https://codeberg.org/musikinformatik/SuperDirt/issues/281
AY.freqtotoneis implemented with an "approximate empirical" formula:sc3-plugins/source/AYUGens/sc/AY.sc
Lines 19 to 23 in 1c9bb52
it is reasonably accurate at a sample rate of 44100 Hz, but at other rates it is severely out of tune.
i noticed that the ayemu code calculates a value
ChipTacts_per_outcount, which depends on the chip frequency of the emulator as well as the sample rate:sc3-plugins/source/AYUGens/AY_libayemu/src/ay8912.c
Line 369 in 1c9bb52
the
AY_Ctorconstructor does not set the chip frequency:sc3-plugins/source/AYUGens/AY_UGen.cpp
Lines 49 to 51 in 1c9bb52
therefore we fall back to the default frequency of
1773400, defined here:sc3-plugins/source/AYUGens/AY_libayemu/src/ay8912.c
Line 17 in 1c9bb52
so, some real-world values of
ChipTacts_per_outcountare: (keeping in mind this is all integer division)1773400/44100/8 = 51773400/48000/8 = 41773400/96000/8 = 2now we can start to see where the magic number
110300infreqtotonecomes from; it is very close to44100*5/2 = 110250. this hints at a general formula for the conversion constant:sampleRate * chipTacts_per_outcount / 8and indeed, this formula seems to work :) notes now sound in tune at both 44100 and 48000 Hz sample rates. (though due to the integer calculations, i guess there will always be some error.)
the PR just implements this calculation in
AY.freqtotone.