I've trying to implement transition from an amount of space to another which is similar to acceleration and deceleration, except i failed and the only thing that i got from this was this infinite stack of mess, here is a screenshot showing this in action:
you can see a very black circle here, which are in reality something like 100 or 200 circles stacked on top of each other
and i reached this result using this piece of code:
def Place_circles(curve, circle_space, cs, draw=True, screen=None):
curve_acceleration = []
if type(curve) == tuple:
curve_acceleration = curve[1][0]
curve_intensity = curve[1][1]
curve = curve[0]
#print(curve_intensity)
#print(curve_acceleration)
Circle_list = []
idx = [0,0]
for c in reversed(range(0,len(curve))):
for p in reversed(range(0,len(curve[c]))):
user_dist = circle_space[curve_intensity[c]] + curve_acceleration[c] * p
dist = math.sqrt(math.pow(curve[c][p][0] - curve[idx[0]][idx[1]][0],2)+math.pow(curve [c][p][1] - curve[idx[0]][idx[1]][1],2))
if dist > user_dist:
idx = [c,p]
Circle_list.append(circles.circles(round(curve[c][p][0]), round(curve[c][p][1]), cs, draw, screen))
This place circles depending on the intensity (a number between 0 and 2, random) of the current curve, which equal to an amount of space (let's say between 20 and 30 here, 20 being index 0, 30 being index 2 and a number between these 2 being index 1).
This create the stack you see above and isn't what i want, i also came to the conclusion that i cannot use acceleration since the amount of time to move between 2 points depend on the amount of circles i need to click on, knowing that there are multiple circles between each points, but not being able to determine how many lead to me being unable to the the classic acceleration formula.
So I'm running out of options here and ideas on how to transition from an amount of space to another.
any idea?
PS: i scrapped the idea above and switched back to my master branch but the code for this is still available in the branch i created here https://github.com/Mrcubix/Osu-StreamGenerator/tree/acceleration .
So now I'm back with my normal code that don't possess acceleration or deceleration.
TL:DR i can't use acceleration since i don't know the amount of circles that are going to be placed between the 2 points and make the time of travel vary (i need for exemple to click circles at 180 bpm of one circle every 0.333s) so I'm looking for another way to generate gradually changing space.
First, i took my function that was generating the intensity for each curves in [0 ; 2]
Then i scrapped the acceleration formula as it's unusable.
Now i'm using a basic algorithm to determine the maximum amount of circles i can place on a curve.
Now the way my script work is the following:
i first generate a stream (multiple circles that need to be clicked at high bpm)
this way i obtain the length of each curves (or segments) of the polyline.
i generate an intensity for each curve using the following function:
def generate_intensity(Circle_list: list = None, circle_space: int = None, Args: list = None):
curve_intensity = []
if not Args or Args[0] == "NewProfile":
prompt = True
while prompt:
max_duration_intensity = input("Choose the maximum amount of curve the change in intensity will occur for: ")
if max_duration_intensity.isdigit():
max_duration_intensity = int(max_duration_intensity)
prompt = False
prompt = True
while prompt:
intensity_change_odds = input("Choose the odds of occurence for changes in intensity (1-100): ")
if intensity_change_odds.isdigit():
intensity_change_odds = int(intensity_change_odds)
if 0 < intensity_change_odds <= 100:
prompt = False
prompt = True
while prompt:
min_intensity = input("Choose the lowest amount of spacing a circle will have: ")
if min_intensity.isdigit():
min_intensity = float(min_intensity)
if min_intensity < circle_space:
prompt = False
prompt = True
while prompt:
max_intensity = input("Choose the highest amount of spacing a circle will have: ")
if max_intensity.isdigit():
max_intensity = float(max_intensity)
if max_intensity > circle_space:
prompt = False
prompt = True
if Args:
if Args[0] == "NewProfile":
return [max_duration_intensity, intensity_change_odds, min_intensity, max_intensity]
elif Args[0] == "GenMap":
max_duration_intensity = Args[1]
intensity_change_odds = Args[2]
min_intensity = Args[3]
max_intensity = Args[4]
circle_space = ([min_intensity, circle_space, max_intensity] if not Args else [Args[0][3],circle_space,Args[0][4]])
count = 0
for idx, i in enumerate(Circle_list):
if idx == len(Circle_list) - 1:
if random.randint(0,100) < intensity_change_odds:
if random.randint(0,100) > 50:
curve_intensity.append(2)
else:
curve_intensity.append(0)
else:
curve_intensity.append(1)
if random.randint(0,100) < intensity_change_odds:
if random.randint(0,100) > 50:
curve_intensity.append(2)
count += 1
else:
curve_intensity.append(0)
count += 1
else:
if curve_intensity:
if curve_intensity[-1] == 2 and not count+1 > max_duration_intensity:
curve_intensity.append(2)
count += 1
continue
elif curve_intensity[-1] == 0 and not count+1 > max_duration_intensity:
curve_intensity.append(0)
count += 1
continue
elif count+1 > 2:
curve_intensity.append(1)
count = 0
continue
else:
curve_intensity.append(1)
else:
curve_intensity.append(1)
curve_intensity.reverse()
if curve_intensity.count(curve_intensity[0]) == len(curve_intensity):
print("Intensity didn't change")
return circle_space[1]
print("\n")
return [circle_space, curve_intensity]
with this, i obtain 2 list, one with the spacing i specified, and the second one is the list of randomly generated intensity.
from there i call another function taking into argument the polyline, the previously specified spacings and the generated intensity:
def acceleration_algorithm(polyline, circle_space, curve_intensity):
new_circle_spacing = []
for idx in range(len(polyline)): #repeat 4 times
spacing = []
Length = 0
best_spacing = 0
for p_idx in range(len(polyline[idx])-1): #repeat 1000 times / p_idx in [0 ; 1000]
# Create multiple list containing spacing going from circle_space[curve_intensity[idx-1]] to circle_space[curve_intensity[idx]]
spacing.append(np.linspace(circle_space[curve_intensity[idx]],circle_space[curve_intensity[idx+1]], p_idx).tolist())
# Sum distance to find length of curve
Length += abs(math.sqrt((polyline[idx][p_idx+1][0] - polyline[idx][p_idx][0]) ** 2 + (polyline [idx][p_idx+1][1] - polyline[idx][p_idx][1]) ** 2))
for s in range(len(spacing)): # probably has 1000 list in 1 list
length_left = Length # Make sure to reset length for each iteration
for dist in spacing[s]: # substract the specified int in spacing[s]
length_left -= dist
if length_left > 0:
best_spacing = s
else: # Since length < 0, use previous working index (best_spacing), could also jsut do `s-1`
if spacing[best_spacing] == []:
new_circle_spacing.append([circle_space[1]])
continue
new_circle_spacing.append(spacing[best_spacing])
break
return new_circle_spacing
with this, i obtain a list with the space between each circles that are going to be placed,
from there, i can Call Place_circles() again, and obtain the new stream:
def Place_circles(polyline, circle_space, cs, DoDrawCircle=True, surface=None):
Circle_list = []
curve = []
next_circle_space = None
dist = 0
for c in reversed(range(0, len(polyline))):
curve = []
if type(circle_space) == list:
iter_circle_space = iter(circle_space[c])
next_circle_space = next(iter_circle_space, circle_space[c][-1])
for p in reversed(range(len(polyline[c])-1)):
dist += math.sqrt((polyline[c][p+1][0] - polyline[c][p][0]) ** 2 + (polyline [c][p+1][1] - polyline[c][p][1]) ** 2)
if dist > (circle_space if type(circle_space) == int else next_circle_space):
dist = 0
curve.append(circles.circles(round(polyline[c][p][0]), round(polyline[c][p][1]), cs, DoDrawCircle, surface))
if type(circle_space) == list:
next_circle_space = next(iter_circle_space, circle_space[c][-1])
Circle_list.append(curve)
return Circle_list
the result is a stream with varying space between circles (so accelerating or decelerating), the only issue left to be fixed is pygame not updating the screen with the new set of circle after i call Place_circles(), but that's an issue i'm either going to try to fix myself or ask in another post
the final code for this feature can be found on my repo : https://github.com/Mrcubix/Osu-StreamGenerator/tree/Acceleration_v02
I'm a Python newbie attempting to create a slot machine simulator that mimics the payouts of the real machines. I'm running into an issue in calculating the line payouts, and I'm sure there's a smarter way of iterating through the lines and calculating them.
Defining some constants that I'll be using:
SymbolMap = ["Boats","Bonus","Buoys","Clams","Light Houses","Lobsters","Scatter","Seagulls","Starfish","Tuna","Wilds"]
#The ints in reels below are a simpler way to express the SymbolMap above
Reels = [[9,8,3,4,6,3,8,1,5,6,2,3,8,2,3,8,5,4,3,10,7,8,10,1,3,0,8,9,3,8,9,5,3,8,0,4,3,8,0,9,2,7,5,3,8,0,7],
[3,2,4,3,2,4,1,7,3,0,7,9,0,1,8,7,10,1,7,4,5,10,2,3,1,7,3,6,5,9,7,6,8,3,0,5,7,3,1,8,7,2,4,3,9,7,0],
[0,8,3,1,4,0,5,8,1,4,8,1,9,8,3,7,8,10,1,4,7,8,9,3,0,9,8,1,9,4,8,6,4,5,7,8,6,2,9,5,1,8,4,7,2,0,9],
[7,9,2,7,6,2,8,7,9,10,2,9,8,5,7,9,10,5,4,2,7,0,3,8,4,7,0,3,2,7,0,4,8,9,7,2,8,3,2,7,8,3,5,10,2,7,8],
[3,10,0,5,2,8,4,9,8,4,7,10,9,2,0,3,9,2,8,3,6,2,8,9,3,2,0,4,9,5,4,7,3,5,8,0,4,9,7,8,4,3,5,7,8,3,7]]
# Lines are the row to look for on each reel. i.e. Lines[0] is a straight line of the 2nd row.
# Lines[3] starts in top left corner of matrix, and forms inverted V shape.
Lines = [[1,1,1,1,1],
[0,0,0,0,0],
[2,2,2,2,2],
[0,1,2,1,0],
[2,1,0,1,2],
[2,2,1,0,0],
[0,0,1,2,2],
[1,2,1,0,1],
[1,0,1,2,1],
[2,1,1,1,0],
[0,1,1,1,2],
[1,2,2,1,0],
[1,0,0,1,2],
[1,1,2,1,0],
[1,1,0,1,2]]
#Payouts are how many credits won for symbols in a row. For example, Symbols[0] is Boats.
#2 boats is 0 credits, 3 boats is 25 credits, 4 boats is 100 credits, 5 boats is 500 credits.
#They must be continuous and from left to right. I.e. BOAT-BOAT-CLAM-BOAT-BOAT on a payline wins 0.
#Similarly, CLAM-CLAM-BOAT-BOAT-BOAT wins 0.
Payouts = [[0,25,100,500],
[0,0,0,0],
[0,25,100,500],
[0,5,30,200]]
#Initializing a 3X5 matrix to represent reels
SpinValues = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
#Initializing message
Message = ''
#Initializing TotalWin
TotalWin = 0
Spin logic, which properly generates a 3X5 matrix of random numbers. Is there a better way to handle the "if first 3 symbols match" portion, since I'll have to repeat again for 4 symbols and 5 symbols? Since each line is only entitled to one payout, I'll start with the 5 symbol payouts, then work down toward 2. I started with 3 because it's the most common and will be easiest to test. I'll also have to account for a wild equaling any symbol, which I haven't tried to tackle yet. Likewise, there is a scatter pay (meaning if you have X number of Scatter symbols anywhere in the matrix, you get a payout. That part will be easy). There is also a bonus game, which I'll be working on later:
def spin(linesPlayed, wager):
for i, object in enumerate(Reels):
length = len(Reels[i])
StopValue = random.randint(0,length-1)
SpinValues[1][i] = Reels[i][StopValue]
if StopValue == 0:
SpinValues[0][i] = Reels[i][-1]
else:
SpinValues[0][i] = Reels[i][StopValue - 1]
if StopValue == len(Reels[i])-1:
SpinValues[2][i] = Reels[i][0]
else:
SpinValues[2][i] = Reels[i][StopValue +1]
print(SpinValues[0])
print("\n")
print(SpinValues[1])
print("\n")
print(SpinValues[2])
for i in range(linesPlayed):
#if first 3 symbols match
if SpinValues[Lines[i][0]] == SpinValues[Lines[i][1]] == SpinValues[Lines[i][2]]:
PayTable(i,wager,3,SpinValues[Lines[i][0]])
#if first 4 symbols match
#if first 5 symbols match
#handle scatter pay
#wilds?
#handle bonus trigger
Handling wins:
def PayTable(i,wager,symbolCount,symbol):
LineWin = Payouts[symbol][symbolCount] * wager
TotalWin += Payouts[symbol][symbolCount] * wager
Message += "Line " + str(i) +" wins " + str(LineWin) + " credits with " + str(symbolCount) + " " + SymbolMap[symbol] + "!" + "\n"
I'm getting the error that both TotalWin and Message are undefined. I thought that I could defined them globally up top?
You need to use the global keyword in each function to access variables defined in a parent.
For example:
def PayTable(i,wager,symbolCount,symbol):
global TotalWin
global Message
(This is in in Python, and code would be great, but I'm primarily interested in the algorithm.)
I'm monitoring an audio stream (PyAudio) and looking for a series of 5 pops (see the bottom for a visualization). I'm read()ing the stream and getting the RMS value for the block that I've just read (similar to this question). My problem is that I'm not looking for a single event, but instead a series of events (pops) that have some characteristics but aren't nearly as boolean as I'd like. What's the most straightforward (and performant) way to detect these five pops?
The RMS function gives me a stream like this:
0.000580998485254, 0.00045098391298, 0.00751436443973, 0.002733730043, 0.00160775708652, 0.000847808804511
It looks a bit more useful if I round (a similar stream) for you:
0.001, 0.001, 0.018, 0.007, 0.003, 0.001, 0.001
You can see the pop in item 3, and presumably as it quiets down in item 4, and maybe the tail end was during a fraction of item 5.
I want to detect 5 of those in a row.
My naive approach is to:
a) define what a pop is: Block's RMS is over .002. For at least 2 blocks but no more than 4 blocks. Started with silence and ends with silence.
Additionally, I'm tempted to define what silence is (to ignore the not quite loud but not quite silent blocks, but I'm not sure this makes more sense then considering 'pop' to be boolean).
b) Then have a state machine that keeps track of a bunch of variables and has a bunch of if statements. Like:
while True:
is_pop = isRMSAmplitudeLoudEnoughToBeAPop(stream.read())
if is_pop:
if state == 'pop':
#continuation of a pop (or maybe this continuation means
#that it's too long to be a pop
if num_pop_blocks <= MAX_POP_RECORDS:
num_pop_blocks += 1
else:
# too long to be a pop
state = 'waiting'
num_sequential_pops = 0
else if state == 'silence':
#possible beginning of a pop
state = 'pop'
num_pop_blocks += 1
num_silence_blocks = 0
else:
#silence
if state = 'pop':
#we just transitioned from pop to silence
num_sequential_pops += 1
if num_sequential_pops == 5:
# we did it
state = 'waiting'
num_sequential_pops = 0
num_silence_blocks = 0
fivePopsCallback()
else if state = 'silence':
if num_silence_blocks >= MAX_SILENCE_BLOCKS:
#now we're just waiting
state = 'waiting'
num_silence_blocks = 0
num_sequential_pops = 0
That code is not at all complete (and might have a bug or two), but illustrates my line of thinking. It's certainly more complex than I'd like it to be, which is why I'm asking for suggestions.
You might want to compute the simple moving average of the last P points, where P ~= 4 and plot the result together with your raw input data.
You could then use the maxima of the smoothed average as a pop. Define a maximum interval in which to see five pops and that could be what your after.
Adjust P for best fit.
I wouldn't be surprised if there wasn't already a Python module for this, but I haven't looked.
I ended up with what, to me, feels like a naive approach with an ongoing loop and a few variables to maintain and transition to new states. It occurred to me after finishing, though, that I should have explored hotword detection because 5 consecutive clicks are basically a hotword. And they have a pattern that I have to look for.
Anyways, here's my code:
POP_MIN_MS = 50
POP_MAX_MS = 150
POP_GAP_MIN_MS = 50
POP_GAP_MAX_MS = 200
POP_BORDER_MIN_MS = 500
assert POP_BORDER_MIN_MS > POP_GAP_MAX_MS
POP_RMS_THRESHOLD_MIN = 100
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100 # Sampling Rate -- frames per second
INPUT_BLOCK_TIME_MS = 50
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME_MS/1000)
POP_MIN_BLOCKS = POP_MIN_MS / INPUT_BLOCK_TIME_MS
POP_MAX_BLOCKS = POP_MAX_MS / INPUT_BLOCK_TIME_MS
POP_GAP_MIN_BLOCKS = POP_GAP_MIN_MS / INPUT_BLOCK_TIME_MS
POP_GAP_MAX_BLOCKS = POP_GAP_MAX_MS / INPUT_BLOCK_TIME_MS
POP_BORDER_MIN_BLOCKS = POP_BORDER_MIN_MS / INPUT_BLOCK_TIME_MS
def listen(self):
pops = 0
sequential_loud_blocks = 0
sequential_notloud_blocks = 0
stream = self.pa.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=INPUT_FRAMES_PER_BLOCK
)
states = {
'PENDING': 1,
'POPPING': 2,
'ENDING': 3,
}
state = states['PENDING']
while True:
amp = audioop.rms(stream.read(INPUT_FRAMES_PER_BLOCK), 2)
is_loud = (amp >= POP_RMS_THRESHOLD_MIN)
if state == states['PENDING']:
if is_loud:
# Only switch to POPPING if it's been quiet for at least the border
# period. Otherwise stay in PENDING.
if sequential_notloud_blocks >= POP_BORDER_MIN_BLOCKS:
state = states['POPPING']
sequential_loud_blocks = 1
# If it's now loud then reset the # of notloud blocks
sequential_notloud_blocks = 0
else:
sequential_notloud_blocks += 1
elif state == states['POPPING']:
if is_loud:
sequential_loud_blocks += 1
# TODO: Is this necessary?
sequential_notloud_blocks = 0
if sequential_loud_blocks > POP_MAX_BLOCKS:
# it's been loud for too long; this isn't a pop
state = states['PENDING']
pops = 0
#print "loud too long"
# since it has been loud and remains loud then no reason to reset
# the notloud_blocks count
else:
# not loud
if sequential_loud_blocks:
# just transitioned from loud. was that a pop?
# we know it wasn't too long, or we would have transitioned to
# PENDING during the pop
if sequential_loud_blocks < POP_MIN_BLOCKS:
# wasn't long enough
# go to PENDING
state = states['PENDING']
pops = 0
#print "not loud long enough"
else:
# just right
pops += 1
logging.debug("POP #%s", pops)
sequential_loud_blocks = 0
sequential_notloud_blocks += 1
else:
# it has been quiet. and it's still quiet
sequential_notloud_blocks += 1
if sequential_notloud_blocks > POP_GAP_MAX_BLOCKS:
# it was quiet for too long
# we're no longer popping, but we don't know if this is the
# border at the end
state = states['ENDING']
elif state == states['ENDING']:
if is_loud:
# a loud block before the required border gap. reset
# since there wasn't a gap, this couldn't be a valid pop anyways
# so just go back to PENDING and let it monitor for the border
sequential_loud_blocks = 1
sequential_notloud_blocks = 0
pops = 0
state = states['PENDING']
else:
sequential_notloud_blocks += 1
# Is the border time (500 ms right now) enough of a delay?
if sequential_notloud_blocks >= POP_BORDER_MIN_BLOCKS:
# that's a bingo!
if pops == 5:
stream.stop_stream()
# assume that starting now the channel is not silent
start_time = time.time()
print ">>>>> 5 POPS"
elapsed = time.time() - start_time
#time.time() may return fractions of a second, which is ideal
stream.start_stream()
# do whateve we need to do
state = states['PENDING']
pops = 0
It needs some formal testing. I found an issue just last night in which it wasn't resetting itself after a pop and then too-long quiet. My plan is to refactor and then feed it a stream of simulated RMS' (e.g., (0, 0, 0, 500, 200, 0, 200, 0, ...)) and ensure it detects (or doesn't detect) appropriately.