Last active
August 4, 2024 05:02
-
-
Save ZoomTen/854ec664997ab7aef9a9f6edca3ca10c to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "29295a5e-e86a-437a-89d8-6191a5c41fa4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"; speed 0\n", | |
"\tdb $00, $01, $02, $03, $03, $04, $05, $06, $06, $07, $08, $09, $09, $0a, $0b, $0c\n", | |
"; speed 1\n", | |
"\tdb $00, $01, $02, $03, $04, $05, $05, $06, $07, $08, $07, $08, $08, $0b, $0c, $0d\n", | |
"; speed 2\n", | |
"\tdb $00, $01, $02, $03, $04, $04, $04, $06, $07, $08, $08, $0b, $0b, $0b, $0b, $0c\n", | |
"; speed 3\n", | |
"\tdb $00, $01, $02, $02, $04, $05, $05, $07, $07, $07, $07, $09, $09, $0b, $0b, $0c\n", | |
"; speed 4\n", | |
"\tdb $00, $01, $02, $03, $04, $05, $05, $05, $07, $07, $08, $09, $0a, $0a, $0b, $0d\n", | |
"; speed 5\n", | |
"\tdb $00, $01, $02, $03, $04, $05, $05, $05, $06, $08, $08, $09, $0a, $0b, $0c, $0c\n", | |
"; speed 6\n", | |
"\tdb $00, $01, $02, $03, $03, $04, $06, $06, $06, $07, $08, $09, $0a, $0b, $0b, $0c\n", | |
"; speed 7\n", | |
"\tdb $00, $01, $02, $03, $03, $04, $06, $06, $07, $07, $08, $09, $0a, $0b, $0b, $0c\n" | |
] | |
} | |
], | |
"source": [ | |
"import pyloudnorm\n", | |
"import numpy as np\n", | |
"\n", | |
"sample_rate = 44_100\n", | |
"attenuation = 2.5\n", | |
"\n", | |
"def compare(peak: float, compare_function, envelope_value, raw_list, meter):\n", | |
" # Setup\n", | |
" global sample_rate\n", | |
" frequency = 1000\n", | |
" period = 1/4\n", | |
" seconds = 10\n", | |
" going_up = False\n", | |
" # ---\n", | |
" one_cycle = sample_rate // frequency\n", | |
" no_of_up_samples = int(one_cycle * period)\n", | |
" for env_value in range(15, 0-1, -1):\n", | |
" starting_volume = env_value/15\n", | |
" #######################################################\n", | |
" if envelope_value == 0:\n", | |
" change_every = 0\n", | |
" else:\n", | |
" change_every = (sample_rate // 64) * envelope_value\n", | |
" samples = []\n", | |
" volume = starting_volume\n", | |
" if envelope_value > 7:\n", | |
" envelope_value = 7\n", | |
" elif envelope_value < 0:\n", | |
" envelope_value = 0\n", | |
" has_hit_zero_yet = False\n", | |
" first_sample_to_hit_zero = 0\n", | |
" for i in range(int(sample_rate * seconds)):\n", | |
" if change_every != 0:\n", | |
" if going_up:\n", | |
" if (i % change_every == 0) and (i != 0) and (volume < 1):\n", | |
" volume += 1/15\n", | |
" else:\n", | |
" if (i % change_every == 0) and (i != 0) and (volume > 0):\n", | |
" volume -= 1/15\n", | |
" if volume < 0:\n", | |
" volume = 0\n", | |
" elif volume > 1:\n", | |
" volume = 1\n", | |
" p = i % one_cycle\n", | |
" if p < no_of_up_samples:\n", | |
" samples += [peak * volume]\n", | |
" else:\n", | |
" samples += [-peak * volume]\n", | |
" if going_up:\n", | |
" if (volume > 14/15) and not has_hit_zero_yet:\n", | |
" has_hit_zero_yet = True\n", | |
" first_sample_to_hit_zero = i\n", | |
" else:\n", | |
" if (volume == 0) and not has_hit_zero_yet:\n", | |
" has_hit_zero_yet = True\n", | |
" first_sample_to_hit_zero = i\n", | |
" first_sample_to_hit_zero += change_every\n", | |
" compare_function(samples, first_sample_to_hit_zero, env_value, raw_list, meter)\n", | |
"\n", | |
"for eeee in range(7+1):\n", | |
" meter = pyloudnorm.Meter(sample_rate)\n", | |
" # list of volume maps\n", | |
" raw_list = []\n", | |
" def something(samples, first_sample_to_hit_zero, env_value, raw_list, meter):\n", | |
" global attenuation\n", | |
" # Default block size for pyloudnorm's integrated loudness function is .4 seconds\n", | |
" # so I need to adjust, otherwise the calculation will throw an error\n", | |
" if first_sample_to_hit_zero < (sample_rate * .4):\n", | |
" sample_arr = np.array(samples)\n", | |
" else:\n", | |
" # use the entire sample if the time to 0 or max is less than .4 seconds\n", | |
" sample_arr = np.array(samples[:first_sample_to_hit_zero])\n", | |
" il = meter.integrated_loudness(sample_arr)\n", | |
" effective_sound_length = first_sample_to_hit_zero/sample_rate*1000\n", | |
" x = []\n", | |
" x += [hex(env_value)[2:].zfill(2)]\n", | |
" x += [il]\n", | |
" x += [0.0]\n", | |
" x += [effective_sound_length]\n", | |
" x += [il-attenuation]\n", | |
" x += [None]\n", | |
" raw_list += [x]\n", | |
" compare(1, something, eeee, raw_list, meter)\n", | |
" # Find the correct value for column 5\n", | |
" for i in range(len(raw_list)):\n", | |
" col4_value = raw_list[i][4]\n", | |
" for j in range(len(raw_list) - 1):\n", | |
" if raw_list[j][1] >= col4_value > raw_list[j + 1][1]:\n", | |
" raw_list[i][5] = raw_list[j][0]\n", | |
" break\n", | |
" else:\n", | |
" raw_list[i][5] = None\n", | |
" print(\"; speed %d\" % eeee)\n", | |
" print(\"\\tdb \", end=\"\")\n", | |
" counter = 0\n", | |
" for i in reversed(raw_list):\n", | |
" print((\"$00%s\" if i[5] is None else \"$%s%%s\" % i[5]) % (\"\" if counter == len(raw_list)-1 else \", \"), end=\"\")\n", | |
" counter += 1\n", | |
" print(\"\\n\", end=\"\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d490700f-95d4-49a0-871a-0341328ac2db", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment