Famitracker / Famitone sfx combo, possible issues?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Famitracker / Famitone sfx combo, possible issues?
by on (#225841)
I'm not sure if they are issues or not so before digging into a codebase that I don't know to find a problem that may not be one, I should first ask if other people encountered the issue.

1) The first issue I encountered is while playing songs. In general it works fine but depending which song is played first, the result is different. For example, song A always work fine. Song B works fine after song A. If song B is played first, it's like some channel are not "active" yet.

Inside Famitracker the song is fine but not with the driver. If a sound fx is played then the channel works. Maybe it's a envelop not set thing, I'm not sure yet.

2) Another issue but I need to re-test in what situation, if the song comes to an end by itself, some issues occur. I forgot if sfx don't work anymore or the next song is affected. I will retest and update that issue later.

3) Last one, the issue I had with more than 256 bytes, if I play manually both channel at the same time it actually works but when a song is actually playing, sometime the sfx is not played properly. If no music, it always works fine, which give me the impression that this issue could happen with other sfx in the future.

Those are the issues I have and that I need to investigate the cause. For sfx, when I first tried to do it myself with driver 3.0, I remember modifying the famitracker driver to "stop" a channel from playing. I had a binary mask that told which channel should be stopped. I think the current example from Shiru may not be doing that and just put data of SFX over music data, which could be the cause of #3. I will try to dug up the code I did to retry to stop a channel that way but I'm not sure if the current famitone sfx driver is aware "when" a sfx finish. If not, I won't be able to re-use that way of stopping channel.

Thank you for any information on the subject.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225859)
At the end of this topic, I posted some modification for the famitracker/famitone-sfx combo that may help some of your issues. I don't know if you already apply them ?

For #1, make sure your instrument are correctly initialized at the beginning of the track in famitracker.

Hope it helps :)
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225860)
Quote:
If song B is played first, it's like some channel are not "active" yet.


I had this same problem on Vigilante Ninja 2. It only affected 1 channel of 1 song, and it only happened sometimes.

I never figured out how to fix it. After a few seconds the channel did start working correctly.

Quote:
make sure your instrument are correctly initialized at the beginning of the track


Hmm, do you think re initializing the music before changing songs might have fixed it?
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225865)
@glutock

This was my guess that something was missing in the song but since I'm not the one that made them I don't know much about the details ^^;; I will check them, just in case.

But the bug mostly happen if I ask the game to skip parts to test. If it start from the beginning, the first song fixes everything. Still, I should check.

One of them was that it changed the "tone" (forgot the name", V00 something) of the noise channel, affecting the rest. I just added back something extra at the end and everything was fine. I think I will need to review the songs, just in case.

I will check your topic. The thing I'm concerned the most is sfx that may not play properly, maybe that topic talks about it.

edit:

dougeff wrote:
Hmm, do you think re initializing the music before changing songs might have fixed it?


I don't think it would fix it. I separated my songs as 1 file per song and re-init on every state and the problem occurred. It is more like the song is missing something that inside the editor is not an issue.

edit2:

@glutock

I did a quick check and didn't know this topic. I will apply to changes to my current build and see how it goes. What I would like to know is if you had issues where sfx didn't play as expected. It feels like the music channel is not stopped when the sfx play and causes issue. I could be wrong but that's one possibility.

One SFX is bigger than 256 bytes. Since it uses 2 channels, I decided to create 2 sfx and play them at the same time (technically not true but you know what I mean :)). It's erratic so I'm tempted to try update the engine to allows bigger since with the example given by rainwarrior but there is a chance that this issue will persist.

Will test the update and update this thread if it did improve my issues. Thanks again! Merci beaucoup!
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225868)
dougeff wrote:
Hmm, do you think re initializing the music before changing songs might have fixed it?


I don't think so since the init/load routines only resets vars and init pointers. But it can't hurt.

I just compared my version of the famitracker/famitone combo and the one on Shiru's website and they're a bit different.
I'll try to clean my code and share it here if you're interested.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225869)
Yes, very interested ;) I want to do a diff and see what has changed.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225870)
So I took Shiru's example on his website, add updated his code with what I've done for Twin Dragons.
This may not be perfect, but I never heard any music/sfx glitches in the game so far.

Hope it helps :)
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225871)
I see, thanks!

I applied the modifications you mentioned on NA already so I will do a diff to see if anything else is missing. Maybe my issue is just the way I'm using it in the hope to avoid modifying for more than 256 bytes causes an issue. Sometime it works fine, sometime it doesn't. Maybe it's a concurrency issue since I'm setting 2 effects that I really want to be run at the same time and it may get out of sync. Will do a few tests about that.

If I find anything later on the subject I will share it.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225878)
dougeff wrote:
Quote:
If song B is played first, it's like some channel are not "active" yet.


I had this same problem on Vigilante Ninja 2. It only affected 1 channel of 1 song, and it only happened sometimes.

I never figured out how to fix it. After a few seconds the channel did start working correctly.

Quote:
make sure your instrument are correctly initialized at the beginning of the track


Hmm, do you think re initializing the music before changing songs might have fixed it?

I've found pausing to be a problem generically with album compilations, which might be a similar problem.

I a 2A03 chanel is halted by a 0 bit in $4015, or length counter running out, both cases will reset the length counter to 0, and the channel won't start producing sound again until the high frequency register is written again to reload the length counter. (Silencing with volume 0 doesn't have this problem.) So a symptom of this, is that if "pause" is setting $4015 to 0, after resuming the square/triangle channels won't turn back on until the music code knows to write the high byte again (depends when A-3/A-2/A-1 is crossed in the music next). Normally Famitracker won't write to that high byte unless it knows its changed (since it causes a phase reset pop otherwise).

So if your SFX have use of the length counter (probably not?) or write to $4015 (maybe? is there a C00 getting picked up?) this might describe your problem.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225896)
@glutock

Did you modify the ft_driver in any way to fix issues? I just did a very quick review of the code (still need to do a deeper one when the kids will be sleeping..) and there is extra code that I don't have in the effect section and other places. Some of the update you mentioned on NA (to skip checking song stop in ft_music_play but to check instead in update) is not done so I'm not sure what is important or not ^^;;;

Once I check in more details your current driver I may have some question on the subject. I could blindly just add the code but usually I don't find that appropriate.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225906)
My guess is that Shiru didn't use the last version at the time. That may explain the differences in the effect section for example, and other places.

I forgot some changes, so here's the updated version in attachement. Should be right this time.
It doesn't 100% match what I said on the NA topic, because I made other changes for Twin Dragons.

Also this might help :
Code:
var_PlayerFlags
; t------p
; t : 0 - playing / 1 - stop
; p : 0 - playing / 1 - pause
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#225917)
Depending when he tested it then yes, it could have been the reason for the differences. I didn't follow the driver since 3.0 so I'm not much aware of what happened these days ^^;;;

As for the flag, is it something you changed or it was already like that? So when $00 then playing but if $01 then it's means pause and if $80 means stop.

Thank you again for sharing your modifications, I really appreciate it!

edit:

I just finished the merge with your driver and it's working except now the music is a lot faster, like it's running for pal or something? ^^;; Is it possible that the $80 modification cause the ntsc/pal setting to be different? That's my only guess for now.

edit2:

The change to $80 had an issue with another place in my own code and I fixed it.

I took your sample and tested the affected sfx with the song that had issue. In your version, the dual channel sfx split in 2 for avoiding size limit is actually working "but" sfx doesn't stop the channel while the sfx is playing so in the middle of the sfx playing you still have some part of the song playing, which feels odd/broken. So something is different from the version I used to start my code and your version. For 1 channel sfx, it doesn't seems to cause much issue. I guess by looking at both of them I should be able to figure a solution that fix my issue. Long time ago, with ft3.0, what I was doing is stopping the data to be put in that channel with a flag, so the channel was stopped until the sfx was over but it seems that the famitone way "mix" the music and sfx when possible, even thought the sfx is not finished yet.

I will try to figure out something out of it.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226033)
Sorry for not answering sooner.

The PAL/NTSC check may be wrong. Depending on your region detection routine, you may have to change this part (in init.s and the famitone part).

I don't remember why exactly I modified the "var_PlayerFlags" flag, maybe something specific in my game where I wanted to know if the music was paused or stopped. Don't know :(

I never used oversized sfx, how did you do that ?
Feel free to send me a song and sfx so I can test it.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226055)
Don't worry! Every one is busy and this is one of my many issues on my todo list ;)

For Pal/NTSC, I found the issue so everything is fine. For now I do not support both for my current testing but eventually may so. For the flag, well, as long that it works, I'm fine with it :lol: The only difference left that I need to figure out later is the way that the data is fed to the register is completely different than the version I have. This may be one of the reason why the effects works fine in your version but "may" cause possible issue in the song played (IF the sfx is not the issue in the first place) but in my version, the sfx played is wrong but the music seems fine. Since I adapted the code to remove it from neslib/crt-nmi, I would need to create a sample with my own code (I can do it later if required).

I included a sample of your code with my own makeFile. The files are organised so the makeFile can compile any files you sent to it without editing it in any folder/sub folder in src (very useful for quick testing). The cfg was adapted for cc65 2.17 and the necessary files for creating the runtime.lib are included too. The data folder contains some sample music from a well known game and with some sfx to reproduce the possible error. The FTM are included too since I'm not the one that made then anyway (very nice music and sfx by the way!).

You will see that the music channel 1/2 doesn't start at the beginning. This is not specific to your version but a problem in general. Since I did not make the song, I don't know what cause it after checking the ftm ^^;;; But this is not the issue at hand. After playing a few time the channel 1/2 should work now that they are activated. If you play sfx with left pad, 2 sfx are played at the same time. If you listen carefully while trying a few time, you will see that the song channel 1/2 are still playing to some degree while the SFX is playing. I would expect the song data to be completely stopped until the sfx is finished playing (unless the driver was updated to mix both at the same time).

As for bigger than 256 bytes, I didn't implement/test yet: for now, I'm avoiding the issue by "splitting" a multi-channel sfx in 2 sfx and playing them at the same time. It seems to be doable, except for the song still playing issue.

If you cannot compile because of lack of make environment, an already compiled nes file is included.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226064)
Just tested the rom for now, and it seems that you can't play sfx before playing a song.
I don't know if it helps, but in my init code I have this :

Code:
    ; initialize sound APU
    ldx #$00
apu_clear_loop:
    sta $4000, X            ; write 0 to most APU registers
    inx
    cpx #$13
    bne apu_clear_loop
    ldx #$00
    stx $4015               ; turn off square/noise/triangle/DPCM channels

    ; acknowledge/disable both APU IRQs
    ; (frame counter and DMC completion)
    lda #$40
    sta $4017  ; APU IRQ: OFF!
    lda $4015  ; APU IRQ: ACK!

    ; enable all channels but DPCM
    lda #$0F
    sta $4015

    ;previous pulse period MSB, to not write it when not changed
    lda #$ff
    sta PREV_4003
    sta PREV_4007

    ; stop music
    lda #$80
    sta var_PlayerFlags
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226065)
Just tested and it fix the part were songs start with some channel disabled. Great! I had a feeling it was something like that, just didn't know much about APU to know if was only those 2 lines (LDA #$0F , STA $4015). Bugs list is getting lower, thanks!

Only sfx left then. My guess is something from your current code will fix my current issue, just need to analyse and check why the code is different in mine (where did I get that sample 2 months ago ^^;;;). And why in your with my data the song seems to continue somehow.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226396)
Sorry for the double post. This is just a follow-up since I found the cause with glutock regarding my "issue".

This is more or less one since this is the current behavior of both drivers combined. When playing SFX, a check is done regarding if music or SFX is louder. Based on the level, the one the highest wins. In one of my case, this had an undesired effect and had to adapt the code to my own needs.

I don't have any issue anymore. Thanks glutock for the support!
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226397)
Ah, I've never liked that particular behaviour of Famitone.

There was a thread about this a while ago, if you're interested in the argument:
https://forums.nesdev.com/viewtopic.php?f=6&t=14876
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226399)
I checked the title and I wouldn't have known that it was related to my issue (I thought it was a bug in my code at first). I will check that thread to see what was discussed, thanks!
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226499)
Not sure if this is my error or Shiru's.

the famitracker driver, to determine PAL mode, expects X to be #1 at init for PAL mode, and !1 for NTSC.

Shiru's example code (I think) uses blarg's code to determine system, and pushes the high bit to the NTSC mode variable. So #$80 for NTSC and #$00 for PAL.

I'll have to double check these details. But I was having trouble getting my ninja game to play PAL notes in PAL mode.
Re: Famitracker / Famitone sfx combo, possible issues?
by on (#226500)
X=1 for INIT is an NSF convention to let the player no what mode it's in. (NSF emulators are not required to have a PPU, so there's really no way for it to test the system internally.) Famitracker relies on it because it's primarily designed to make NSF files.