Problem combining music and sounds in µpython


#1

There is a problem when combining music from sd with a sound effect: everytime the sound effect plays, the music is interrupted.
If you would like to check for yourself:
During the game, everytime ‘A’ is pressed, the sound effect is played
bin:
ufo.bin (167.1 KB)
ufointro.snd (230.8 KB)

It seems something is wrong with the mixing.


#2

Source please ?


#3

Source:

In fact I have discussed this already with @Hanski.
He noticed the same effect in his frogitto game
It is more noticeable here because the sound effect has a longer duration.


#4

Found the reason for the bug in mixing sfx and streaming music.

@jonne : Do you remember reason for this?

            ifdef PROJ_SDFS_STREAMING
                int32_t s = (int32_t(output) + int32_t(sfxSample)) - 128;
            #else
                int32_t s = (127 + int32_t(sfxSample)) - 128;
            #endif

Music streaming is normally using PFFS, not SDFS.


#5

Actually the PROJ_SDFS_STREAMING was intended to just signify that output is an actual value coming from data read from SD

If not, and as you can see, output is replaced with value of 127. This was to allow the uPy games to stream from flash memory, without needing a value in the variable output


#6

Ok. The name is a bit misleading as it makes you think about SDFilesystem library only.

It also takes SDFilesystem header into build in PokittoSound.h, but it is not usually needed for streaming.

#ifdef PROJ_SDFS_STREAMING
#include "SDFSDisk.h"
#endif

#7

You mean PokittoSound.cpp probably.

Is this problem then solved now or have I totally misunderstood?


#8

Don’t you mean another flag? That one was made specifically to make steaming work with sdfs, since pffs can’t be used simultaneously with sdfs in games like abbaye.


#9

Right.

I have it working now in my environment. Once we agree about the official fix I will make a PR to PokittoLib.


#10

Yeah sure, you don’t need to rush.

I was wondering where this piece of code is coming from? I was looking in the pokittolib source but was unable to find.


#11

@Hanski , @FManga … this streaming part came into the library during the Abbaye development. I would like you to propose the “official” fix, because clearly I am not up to speed (using the wrong defines etc) how you did it.

@Hanski, I have merged all PR’s recently. Any time you want to push your fixes I will merge them ASAP


#12

I would like to use a different define than PROJ_SDFS_STREAMING and remove that. Does anybody know if it is used in some projects? Rayne? Abbaye? I would not like to break anything.

Proposal:

  1. The SD streaming audio has been used a long time so I would like that be the default. Audio effect played via PlaySfx() is mixed with SD streaming audio by default.

  2. If PlaySfx() is wanted to be used without the SD streaming, there could be a new define: PROJ_SD_STREAMING_DISABLED. That would ignore SD streaming buffers in mixing. So, to be able to use PlaySfx() alone you need to define: PROJ_ENABLE_SOUND, PROJ_STREAMING_MUSIC and PROJ_SD_STREAMING_DISABLED.


#13

Don’t remove it. The point of that flag is to allow games to use either PFFS and SDFS without breaking streaming. If everybody (including PokittoDisk) was using SDFS then yes, we could remove it.


#14

Ok, but that flag should not affect to mixing. In the mixing phase it should not matter how the streaming buffers have been filled: by using SDFS or PFFS, or even just generated buffer content programmatically.

@Shdwwzrd, are you using playSFX() in code without PROJ_SDFS_STREAMING in the my_settings.h file in Rayne?

On the other hand, in Python Editor this does not matter as I am filling the streaming buffers with 127 every time the sound module is initialized. So PROJ_SD_STREAMING_DISABLED will not be needed there, independent of whether the sd music streaming is actually used or not.


#15

Yes, Rayne is using playSFX() and PROJ_SDFS_STREAMING is not in my_settings.h


#16

@jonne I have now a new pull request in the queue. That corrects the mixing problem in SD streaming and SFXs. It also contains changes related to Python Editor feature development.

Ok, when my PR has been merged to PokittoLib you need to add
#define PROJ_DISABLE_MIXING_SFX_WITH_SD_STREAMING
to my_settings.h.


#17

why a project define is needed? i think this will be very confusing, we could simply add an variable we set it when we there is SDFS Stream and reset when there is not.


#18

I’d like to comment on whether a define or a variable would be better,
but frankly even after reading this I have no clue what the actual problem is,
let alone what the proposed solution does.


#19

This is a setting which does not change in run time. It could be a global variable, but in my opinion a define suits as well.


#20

I prefer compile time defines because checking a variable is wasting some cycles. and in high freguency interrupt calls, it matters