stringcalc 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stringcalc/__init__.py +8 -0
- stringcalc/_version.py +34 -0
- stringcalc/cli.py +503 -0
- stringcalc/data/__init__.py +3 -0
- stringcalc/data/aquila-nng.csv +32 -0
- stringcalc/data/daddario-stp.csv +622 -0
- stringcalc/data/daddario-tension.csv +654 -0
- stringcalc/data/ghs.csv +560 -0
- stringcalc/data/stringjoy.csv +134 -0
- stringcalc/data/worth.csv +11 -0
- stringcalc/frets.py +181 -0
- stringcalc/py.typed +0 -0
- stringcalc/tension.py +620 -0
- stringcalc-0.1.0.dist-info/METADATA +37 -0
- stringcalc-0.1.0.dist-info/RECORD +18 -0
- stringcalc-0.1.0.dist-info/WHEEL +4 -0
- stringcalc-0.1.0.dist-info/entry_points.txt +2 -0
- stringcalc-0.1.0.dist-info/licenses/LICENSE +21 -0
stringcalc/tension.py
ADDED
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
"""
|
|
2
|
+
String tension calculations.
|
|
3
|
+
|
|
4
|
+
For example:
|
|
5
|
+
|
|
6
|
+
- For specified strings, calculate tensions.
|
|
7
|
+
- For specified total or per-string tension, suggest strings.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import math
|
|
13
|
+
import re
|
|
14
|
+
import warnings
|
|
15
|
+
from functools import lru_cache
|
|
16
|
+
from typing import NamedTuple, Protocol, cast
|
|
17
|
+
|
|
18
|
+
import pandas as pd
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _get_data():
|
|
22
|
+
from importlib.resources import files
|
|
23
|
+
|
|
24
|
+
from . import data
|
|
25
|
+
|
|
26
|
+
return files(data)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
DATA = _get_data()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _assure_string(s: pd.Series) -> pd.Series[str]:
|
|
33
|
+
"""For pandas(-stubs), assure type checker that a Series is of string type.
|
|
34
|
+
Really we know that we have pandas string dtype, but pandas-stubs
|
|
35
|
+
doesn't know/care about this yet(?) in terms of concretizing Series.
|
|
36
|
+
"""
|
|
37
|
+
assert s.dtype == "string"
|
|
38
|
+
return cast("pd.Series[str]", s)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@lru_cache(1)
|
|
42
|
+
def load_data() -> pd.DataFrame:
|
|
43
|
+
"""Load all string data.
|
|
44
|
+
|
|
45
|
+
.. note::
|
|
46
|
+
Important columns:
|
|
47
|
+
|
|
48
|
+
- ``id``: String (product) ID
|
|
49
|
+
(e.g. "DA:PB053" for the string used for the low E in
|
|
50
|
+
D'Addario phosphor bronze acoustic guitar light set
|
|
51
|
+
`EJ16 <https://www.daddario.com/products/guitar/acoustic-guitar/phosphor-bronze/ej16-phosphor-bronze-acoustic-guitar-strings-light-12-53/>`__).
|
|
52
|
+
|
|
53
|
+
- ``group_id``: String group ID (e.g. "DA:PB" for D'Addario phosphor bronze)
|
|
54
|
+
used to identify a group of strings with the same manufacturer, material, etc.
|
|
55
|
+
|
|
56
|
+
- ``uw``: String unit weight (mass per unit length) [lbm/in].
|
|
57
|
+
For DA:PB053 this value is 0.000570.
|
|
58
|
+
|
|
59
|
+
- ``gauge``: String gauge (diameter) [in].
|
|
60
|
+
For DA:PB053 this value is 0.053.
|
|
61
|
+
|
|
62
|
+
The "DA:" prefix in ``id`` and ``group_id``
|
|
63
|
+
indicates the data is from :func:`load_daddario_data`.
|
|
64
|
+
|
|
65
|
+
Notes
|
|
66
|
+
-----
|
|
67
|
+
Combines results from the individual ``load_*_data`` functions
|
|
68
|
+
with ``for_combined=True`` applied.
|
|
69
|
+
|
|
70
|
+
See Also
|
|
71
|
+
--------
|
|
72
|
+
load_aquila_data
|
|
73
|
+
load_daddario_data
|
|
74
|
+
load_daddario_stp_data
|
|
75
|
+
load_ghs_data
|
|
76
|
+
load_stringjoy_data
|
|
77
|
+
load_worth_data
|
|
78
|
+
"""
|
|
79
|
+
df = pd.concat(
|
|
80
|
+
[fn(for_combined=True) for fn in _DATA_LOADERS],
|
|
81
|
+
ignore_index=True,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
for name in ["group", "group_id"]:
|
|
85
|
+
df[name] = df[name].astype("category")
|
|
86
|
+
|
|
87
|
+
return df
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@lru_cache(2)
|
|
91
|
+
def load_daddario_data(*, for_combined: bool = False) -> pd.DataFrame:
|
|
92
|
+
"""Load D'Addario data.
|
|
93
|
+
|
|
94
|
+
Although outdated in some cases, this data set has the most string types.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
for_combined
|
|
99
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
100
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
df = pd.read_csv(DATA.joinpath("daddario-tension.csv"), header=0).convert_dtypes()
|
|
104
|
+
|
|
105
|
+
if for_combined:
|
|
106
|
+
df = df.drop(columns=["notes", "tens"])
|
|
107
|
+
|
|
108
|
+
df["group"] = (
|
|
109
|
+
"D'Addario - " + _assure_string(df["category"]) + " - " + _assure_string(df["group"])
|
|
110
|
+
)
|
|
111
|
+
df["group_id"] = "DA:" + _assure_string(df["group_id"])
|
|
112
|
+
df["id"] = "DA:" + _assure_string(df["id"])
|
|
113
|
+
|
|
114
|
+
df = df.drop(columns=["category", "id_pref", "id_suff"])
|
|
115
|
+
|
|
116
|
+
# Set categorical columns
|
|
117
|
+
for name in ["category", "group", "id_pref", "id_suff", "group_id"]:
|
|
118
|
+
if name in df.columns:
|
|
119
|
+
df[name] = df[name].astype("category")
|
|
120
|
+
|
|
121
|
+
return df
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@lru_cache(1)
|
|
125
|
+
def _get_daddario_group_ids() -> set[str]:
|
|
126
|
+
return set(load_daddario_data(for_combined=False).group_id.cat.categories)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@lru_cache
|
|
130
|
+
def load_aquila_data(*, nng_density: float = 1300, for_combined: bool = False) -> pd.DataFrame:
|
|
131
|
+
"""Load Aquila NNG (New Nylgut) data.
|
|
132
|
+
|
|
133
|
+
Parameters
|
|
134
|
+
----------
|
|
135
|
+
nng_density
|
|
136
|
+
Assumed density of Aquila NNG strings, in [kg m-3].
|
|
137
|
+
They are supposed to be the same density as gut.
|
|
138
|
+
https://www.cs.helsinki.fi/u/wikla/mus/Calcs/wwwscalc.html says gut is 1276 kg m-3;
|
|
139
|
+
I have seen 1300 elsewhere.
|
|
140
|
+
for_combined
|
|
141
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
142
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
143
|
+
"""
|
|
144
|
+
import numpy as np
|
|
145
|
+
|
|
146
|
+
df = pd.read_csv(DATA.joinpath("aquila-nng.csv"), header=0).convert_dtypes()
|
|
147
|
+
|
|
148
|
+
# Compute UW
|
|
149
|
+
# TODO: use Pint
|
|
150
|
+
df["uw"] = nng_density * 2.205 / 1e6 * (2.54**3) * (np.pi * df.gauge**2 / 4)
|
|
151
|
+
|
|
152
|
+
# Set group ID (used to select string type)
|
|
153
|
+
df["group"] = "New Nylgut"
|
|
154
|
+
df["group_id"] = "NNG"
|
|
155
|
+
df[["group", "group_id"]] = df[["group", "group_id"]].astype("string")
|
|
156
|
+
|
|
157
|
+
gauge_eqv_cols = [col for col in df.columns if col.startswith("gauge_")]
|
|
158
|
+
if for_combined:
|
|
159
|
+
df["group"] = "Aquila " + _assure_string(df["group"])
|
|
160
|
+
df["group_id"] = "A:" + _assure_string(df["group_id"])
|
|
161
|
+
df["id"] = "A:" + _assure_string(df["id"])
|
|
162
|
+
|
|
163
|
+
df = df.drop(columns=gauge_eqv_cols)
|
|
164
|
+
else:
|
|
165
|
+
df = df.rename(columns={col: col.replace("gauge_", "gauge_eqv_") for col in gauge_eqv_cols})
|
|
166
|
+
|
|
167
|
+
for name in ["group", "group_id"]:
|
|
168
|
+
df[name] = df[name].astype("category")
|
|
169
|
+
|
|
170
|
+
return df
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@lru_cache(2)
|
|
174
|
+
def load_worth_data(*, for_combined: bool = False) -> pd.DataFrame:
|
|
175
|
+
"""Load Worth fluorocarbon data.
|
|
176
|
+
|
|
177
|
+
Parameters
|
|
178
|
+
----------
|
|
179
|
+
for_combined
|
|
180
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
181
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
df = pd.read_csv(DATA.joinpath("worth.csv"), header=0).convert_dtypes()
|
|
185
|
+
|
|
186
|
+
# Set group ID (used to select string type)
|
|
187
|
+
df["group"] = "Fluorocarbon"
|
|
188
|
+
df["group_id"] = "FC"
|
|
189
|
+
df[["group", "group_id"]] = df[["group", "group_id"]].astype("string")
|
|
190
|
+
|
|
191
|
+
if for_combined:
|
|
192
|
+
df["group"] = "Worth " + _assure_string(df["group"])
|
|
193
|
+
df["group_id"] = "W" + _assure_string(df["group_id"])
|
|
194
|
+
df["id"] = "WFC:" + _assure_string(df["id"])
|
|
195
|
+
|
|
196
|
+
df = df.drop(columns="rho")
|
|
197
|
+
|
|
198
|
+
for name in ["group", "group_id"]:
|
|
199
|
+
df[name] = df[name].astype("category")
|
|
200
|
+
|
|
201
|
+
return df
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@lru_cache(2)
|
|
205
|
+
def load_stringjoy_data(*, for_combined: bool = False) -> pd.DataFrame:
|
|
206
|
+
"""Load Stringjoy data.
|
|
207
|
+
|
|
208
|
+
https://tension.stringjoy.com/
|
|
209
|
+
|
|
210
|
+
Parameters
|
|
211
|
+
----------
|
|
212
|
+
for_combined
|
|
213
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
214
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
df = pd.read_csv(DATA.joinpath("stringjoy.csv"), header=0).convert_dtypes()
|
|
218
|
+
|
|
219
|
+
if for_combined:
|
|
220
|
+
df["id"] = "SJ:" + _assure_string(df["id"])
|
|
221
|
+
df["group"] = "Stringjoy " + _assure_string(df["group"])
|
|
222
|
+
df["group_id"] = "SJ:" + _assure_string(df["group_id"])
|
|
223
|
+
|
|
224
|
+
for name in ["group", "group_id"]:
|
|
225
|
+
df[name] = df[name].astype("category")
|
|
226
|
+
|
|
227
|
+
return df
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
@lru_cache(2)
|
|
231
|
+
def load_ghs_data(*, for_combined: bool = False) -> pd.DataFrame:
|
|
232
|
+
"""Load GHS data.
|
|
233
|
+
|
|
234
|
+
https://www.ghsstrings.com/pages/tension-calc
|
|
235
|
+
|
|
236
|
+
Parameters
|
|
237
|
+
----------
|
|
238
|
+
for_combined
|
|
239
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
240
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
241
|
+
"""
|
|
242
|
+
|
|
243
|
+
df = pd.read_csv(DATA.joinpath("ghs.csv"), header=0).convert_dtypes()
|
|
244
|
+
|
|
245
|
+
# Drop ID dupes (e.g. PL which is included for both acoustic and electric)
|
|
246
|
+
df = df.drop_duplicates(subset=["id"], keep="first")
|
|
247
|
+
|
|
248
|
+
# Drop where we don't have gauge (bass strings for different scale lengths)
|
|
249
|
+
df = df.dropna(subset=["gauge"])
|
|
250
|
+
|
|
251
|
+
if for_combined:
|
|
252
|
+
df["id"] = "GHS:" + _assure_string(df["id"])
|
|
253
|
+
df["group"] = "GHS - " + _assure_string(df["group"])
|
|
254
|
+
df["group_id"] = "GHS:" + _assure_string(df["group_id"])
|
|
255
|
+
|
|
256
|
+
for name in ["group", "group_id"]:
|
|
257
|
+
df[name] = df[name].astype("category")
|
|
258
|
+
|
|
259
|
+
return df.reset_index(drop=True)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
@lru_cache(2)
|
|
263
|
+
def load_daddario_stp_data(*, for_combined: bool = False) -> pd.DataFrame:
|
|
264
|
+
"""Load D'Addario data derived from the new String Tension Pro (STP).
|
|
265
|
+
|
|
266
|
+
https://www.daddario.com/string-tension-pro
|
|
267
|
+
|
|
268
|
+
STP is in beta as of June 2024.
|
|
269
|
+
|
|
270
|
+
Parameters
|
|
271
|
+
----------
|
|
272
|
+
for_combined
|
|
273
|
+
Return the frame intended for use in the combined dataset (:func:`load_data`),
|
|
274
|
+
adding appropriate prefixes and dropping extraneous columns.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
df = pd.read_csv(DATA.joinpath("daddario-stp.csv"), header=0).convert_dtypes()
|
|
278
|
+
|
|
279
|
+
if for_combined:
|
|
280
|
+
df = df.drop(columns=["instruments"])
|
|
281
|
+
|
|
282
|
+
df["id"] = "STP:" + df["id"]
|
|
283
|
+
df["group"] = "D'Addario (STP) - " + df["group"]
|
|
284
|
+
df["group_id"] = "STP:" + df["group_id"]
|
|
285
|
+
|
|
286
|
+
for name in ["instruments", "group", "group_id"]:
|
|
287
|
+
if name in df.columns:
|
|
288
|
+
df[name] = df[name].astype("category")
|
|
289
|
+
|
|
290
|
+
return df
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class _DataLoader(Protocol):
|
|
294
|
+
def __call__(self, *, for_combined: bool = False) -> pd.DataFrame: ... # noqa: E704
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
_DATA_LOADERS: list[_DataLoader] = [
|
|
298
|
+
load_daddario_data,
|
|
299
|
+
load_aquila_data,
|
|
300
|
+
load_worth_data,
|
|
301
|
+
load_stringjoy_data,
|
|
302
|
+
load_ghs_data,
|
|
303
|
+
load_daddario_stp_data,
|
|
304
|
+
]
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
_re_string_spec = re.compile(
|
|
308
|
+
r"(?P<L>[\.0-9]+)"
|
|
309
|
+
r" *(?P<Lu>\"|m{2})"
|
|
310
|
+
r" +(?P<type>[A-Z]+)"
|
|
311
|
+
r" +(?P<gauge>[\.[0-9]+)"
|
|
312
|
+
r" *(?P<pw>[pPwW])?"
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
class String(NamedTuple):
|
|
317
|
+
L: float
|
|
318
|
+
"""Scale length."""
|
|
319
|
+
type: str
|
|
320
|
+
"""String type abbreviation, such as D'Addario "PB", "PL", or "NYL"."""
|
|
321
|
+
gauge: float
|
|
322
|
+
"""String gauge."""
|
|
323
|
+
wound: bool
|
|
324
|
+
"""Whether the string is wound (as opposed to plain)."""
|
|
325
|
+
|
|
326
|
+
# TODO: `wound` isn't really relevant for single string, since `type` tells us whether wound or not
|
|
327
|
+
# i.e., `wound` can be a property (as well as winding and core materials, loop vs ball, ...?)
|
|
328
|
+
# OR: store core and winding material, keeping it more general, instead of using D'Addario IDs
|
|
329
|
+
# TODO: unit support (pint?)
|
|
330
|
+
|
|
331
|
+
@classmethod
|
|
332
|
+
def from_spec(cls, s: str):
|
|
333
|
+
"""Create a string from spec, e.g. ``'22.9" PB .042w'``."""
|
|
334
|
+
m = _re_string_spec.match(s.strip())
|
|
335
|
+
if m is None:
|
|
336
|
+
raise ValueError(
|
|
337
|
+
f"input {s!r} did not match the spec. "
|
|
338
|
+
"Some valid examples are:\n"
|
|
339
|
+
' `22.9" PB .042w`'
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
d = m.groupdict()
|
|
343
|
+
|
|
344
|
+
try:
|
|
345
|
+
L = float(d["L"])
|
|
346
|
+
except Exception as e:
|
|
347
|
+
raise ValueError(
|
|
348
|
+
f"detected string length {d['L']!r} could not be coerced to float"
|
|
349
|
+
) from e
|
|
350
|
+
|
|
351
|
+
type_ = d["type"]
|
|
352
|
+
|
|
353
|
+
sgauge = d["gauge"]
|
|
354
|
+
if "." not in sgauge and sgauge.startswith("0"): # leading 0 implies decimal
|
|
355
|
+
sgauge = f".{sgauge}"
|
|
356
|
+
try:
|
|
357
|
+
gauge = float(sgauge)
|
|
358
|
+
except Exception as e:
|
|
359
|
+
raise ValueError(
|
|
360
|
+
f"detected string gauge {sgauge!r} could not be coerced to float"
|
|
361
|
+
) from e
|
|
362
|
+
|
|
363
|
+
pw = d["pw"]
|
|
364
|
+
if pw is None:
|
|
365
|
+
wound = True # maybe should be configurable
|
|
366
|
+
else:
|
|
367
|
+
pw_ = pw.lower()
|
|
368
|
+
if pw_ == "p":
|
|
369
|
+
wound = False
|
|
370
|
+
elif pw_ == "w":
|
|
371
|
+
wound = True
|
|
372
|
+
else:
|
|
373
|
+
raise ValueError(f"invalid p/w {pw}")
|
|
374
|
+
|
|
375
|
+
return cls(L, type_, gauge, wound)
|
|
376
|
+
|
|
377
|
+
def __str__(self):
|
|
378
|
+
sgauge = str(self.gauge)
|
|
379
|
+
if sgauge.startswith("0."):
|
|
380
|
+
sgauge = sgauge[1:]
|
|
381
|
+
|
|
382
|
+
return f"{self.L}\" {self.type} {sgauge}{'p' if not self.wound else ''}"
|
|
383
|
+
|
|
384
|
+
# TODO: .tune_to() method, returning a TunedString
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
# TODO: TunedString class with Pitch
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def tension(s: String, pitch: str = "A4") -> float:
|
|
391
|
+
"""Compute tension for :class:`String`.
|
|
392
|
+
|
|
393
|
+
Results are for a single type, e.g. plain steel or phosphor bronze.
|
|
394
|
+
|
|
395
|
+
Parameters
|
|
396
|
+
----------
|
|
397
|
+
s
|
|
398
|
+
String of interest.
|
|
399
|
+
pitch
|
|
400
|
+
Pitch name in SPN, e.g. "A4".
|
|
401
|
+
"""
|
|
402
|
+
from pyabc2 import Pitch
|
|
403
|
+
|
|
404
|
+
t = s.type
|
|
405
|
+
g = s.gauge
|
|
406
|
+
L = s.L
|
|
407
|
+
|
|
408
|
+
if t in {"PL", "S", "PS"}: # plain steel
|
|
409
|
+
tda = "DA:PL"
|
|
410
|
+
elif t in {"PB"}: # phosphor bronze
|
|
411
|
+
tda = "DA:PB"
|
|
412
|
+
elif t in {"NYL", "N"}: # plain "rectified" nylon
|
|
413
|
+
tda = "DA:NYL"
|
|
414
|
+
elif t in {"NYLW", "NW"}: # standard silver-wrapped nylon
|
|
415
|
+
tda = "DA:NYLW"
|
|
416
|
+
else:
|
|
417
|
+
raise ValueError(f"string type {t!r} invalid or not supported")
|
|
418
|
+
|
|
419
|
+
data = load_data().query(f"group_id == '{tda}'")
|
|
420
|
+
|
|
421
|
+
rows = data.loc[data.gauge == g]
|
|
422
|
+
if len(rows) == 0:
|
|
423
|
+
raise ValueError(
|
|
424
|
+
f"gauge {g} not found. "
|
|
425
|
+
f"Available {tda} gauges are: {', '.join(data.gauge.astype(str))}"
|
|
426
|
+
)
|
|
427
|
+
# TODO: use closest instead with warning?
|
|
428
|
+
elif len(rows) > 1:
|
|
429
|
+
raise ValueError("multiple matching gauges\n{rows.to_string()}")
|
|
430
|
+
|
|
431
|
+
# Starting from
|
|
432
|
+
# 1 / v^2 = mu / T
|
|
433
|
+
# (derivation at https://phys.libretexts.org/Bookshelves/University_Physics/Book%3A_University_Physics_(OpenStax)/Book%3A_University_Physics_I_-_Mechanics_Sound_Oscillations_and_Waves_(OpenStax)/16%3A_Waves/16.04%3A_Wave_Speed_on_a_Stretched_String)
|
|
434
|
+
# => T = mu v^2 [mass / length] * [length time-1]^2 = [mass length time-2]]
|
|
435
|
+
# => T = mu (f wl)^2 since v = f wl
|
|
436
|
+
# => T = mu (2 f L)^2 since the primary mode has wl = 2 L
|
|
437
|
+
#
|
|
438
|
+
# In the D'Addario formula:
|
|
439
|
+
# - UW: "unit weight", really linear density (mu; mass / length) [lbm/in]
|
|
440
|
+
# - L: scale length [in]
|
|
441
|
+
# - F: frequency [Hz := s-1]
|
|
442
|
+
# - T: "tension" [lb] (technically lbf)
|
|
443
|
+
#
|
|
444
|
+
# The scaling factor comes from the conversion from lb (lbm) to lbf
|
|
445
|
+
# i.e., it is gravity in the units of the RHS (seconds and inches)
|
|
446
|
+
# gc = (9.80665 m s-2) * (3.28084 ft m-1) * (12 in ft-1)
|
|
447
|
+
# = 386.09 in s-2 (technically [in lbm lbf-1 s-2])
|
|
448
|
+
#
|
|
449
|
+
# At g0, 1 lbm exerts a force of 1 lbf => lbf = g0 lbm = 32.174 lbm ft s-2
|
|
450
|
+
# https://en.wikipedia.org/wiki/Gc_(engineering)
|
|
451
|
+
|
|
452
|
+
UW = float(rows.uw.iloc[0])
|
|
453
|
+
F = Pitch.from_name(pitch).etf
|
|
454
|
+
|
|
455
|
+
T = UW * (2 * L * F) ** 2 / 386.09
|
|
456
|
+
|
|
457
|
+
return T
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def unit_weight(T: float, L: float, pitch: str) -> float:
|
|
461
|
+
"""From scale length, pitch, and desired tension, compute unit weight
|
|
462
|
+
(mass per unit length) [lbm in-1].
|
|
463
|
+
|
|
464
|
+
Parameters
|
|
465
|
+
----------
|
|
466
|
+
T
|
|
467
|
+
Tension [lbf].
|
|
468
|
+
L
|
|
469
|
+
Scale length [in].
|
|
470
|
+
pitch
|
|
471
|
+
Pitch name in SPN, e.g. "A4".
|
|
472
|
+
"""
|
|
473
|
+
from pyabc2 import Pitch
|
|
474
|
+
|
|
475
|
+
F = Pitch.from_name(pitch).etf
|
|
476
|
+
|
|
477
|
+
UW = (T * 386.09) / (2 * L * F) ** 2
|
|
478
|
+
|
|
479
|
+
return UW
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
DENSITY_LB_IN = {
|
|
483
|
+
"plain steel": 0.285,
|
|
484
|
+
"plain nylon": 0.0412, # 1.14 g/cm3
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
_STRING_TYPE_ALIASES = {
|
|
488
|
+
"plain steel": {"PL", "PS", "S"},
|
|
489
|
+
"plain nylon": {"N", "NYL"},
|
|
490
|
+
# "silver-wound nylon": {"NW", "NYLW"},
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
_STRING_TYPE_ALIAS_TO_VERBOSE = {
|
|
494
|
+
alias: k for k, aliases in _STRING_TYPE_ALIASES.items() for alias in aliases
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def gauge(density: float, T: float, L: float, pitch: str) -> float:
|
|
499
|
+
"""From density, scale length, pitch, and desired tension, compute gauge.
|
|
500
|
+
Not nominal gauge, precise diameter.
|
|
501
|
+
|
|
502
|
+
Parameters
|
|
503
|
+
----------
|
|
504
|
+
density
|
|
505
|
+
String material density [lbm in-3].
|
|
506
|
+
|
|
507
|
+
.. important::
|
|
508
|
+
Although [kg m-3] and [g cm-3] units are more common for density,
|
|
509
|
+
currently you must convert to [lbm in-3] in order to get a correct answer.
|
|
510
|
+
But Pint support for this is coming soon!
|
|
511
|
+
T
|
|
512
|
+
Tension [lbf].
|
|
513
|
+
L
|
|
514
|
+
Scale length [in].
|
|
515
|
+
pitch
|
|
516
|
+
Pitch name in SPN, e.g. "A4".
|
|
517
|
+
"""
|
|
518
|
+
|
|
519
|
+
UW = unit_weight(T, L, pitch)
|
|
520
|
+
|
|
521
|
+
# mu = rho pi r^2
|
|
522
|
+
# => r = sqrt(mu / (rho pi))
|
|
523
|
+
# => d = 2 sqrt(mu / (rho pi))
|
|
524
|
+
d = 2 * math.sqrt(UW / (density * math.pi))
|
|
525
|
+
|
|
526
|
+
return d
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def suggest_gauge(
|
|
530
|
+
T: float, L: float, pitch: str, *, types: set[str] | None = None, n: int = 3
|
|
531
|
+
) -> pd.DataFrame:
|
|
532
|
+
"""For target tension, given scale length, and pitch, return suggested gauge(s).
|
|
533
|
+
|
|
534
|
+
Two types are commonly used to make string sets, e.g.
|
|
535
|
+
plain steel + phosphor bronze for steel-string acoustic guitar
|
|
536
|
+
or plain nylon + silver-wrapped nylon for classical guitar.
|
|
537
|
+
|
|
538
|
+
Parameters
|
|
539
|
+
----------
|
|
540
|
+
T
|
|
541
|
+
Tension [lbf].
|
|
542
|
+
L
|
|
543
|
+
Scale length [in].
|
|
544
|
+
pitch
|
|
545
|
+
Pitch name in ASCII `SPN <https://en.wikipedia.org/wiki/Scientific_pitch_notation>`__,
|
|
546
|
+
e.g. "A4" (A440), which is a fourth above the guitar's high E string.
|
|
547
|
+
Guitar standard tuning: E2 A2 D3 G3 B3 E4.
|
|
548
|
+
types
|
|
549
|
+
String type IDs
|
|
550
|
+
(column ``group_id`` in the :func:`data table <load_data>`)
|
|
551
|
+
to consider.
|
|
552
|
+
If unset, defaults to D'Addario phosphor bronze and plain steel
|
|
553
|
+
(``{'DA:PB', 'DA:PL'}``).
|
|
554
|
+
n
|
|
555
|
+
Number of suggestions to include in the returned frame.
|
|
556
|
+
"""
|
|
557
|
+
from pyabc2 import Pitch
|
|
558
|
+
|
|
559
|
+
if types is None:
|
|
560
|
+
types = {"DA:PB", "DA:PL"}
|
|
561
|
+
|
|
562
|
+
# Assume D'Addario if match to an original D'Addario group ID (with no `DA:` prefix)
|
|
563
|
+
daddario_group_ids = _get_daddario_group_ids()
|
|
564
|
+
types_in = types
|
|
565
|
+
types = {t if t not in daddario_group_ids else f"DA:{t}" for t in types}
|
|
566
|
+
types_changed = types_in - types
|
|
567
|
+
if types_changed:
|
|
568
|
+
warnings.warn(
|
|
569
|
+
f"string type groups {sorted(types_changed)} assumed to be D'Addario. "
|
|
570
|
+
f"Use group ID prefix 'DA:' to be explicit and avoid this warning.",
|
|
571
|
+
stacklevel=2,
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
data_all = load_data()
|
|
575
|
+
data = data_all.query("group_id in @types")
|
|
576
|
+
|
|
577
|
+
# Validate types
|
|
578
|
+
all_types = set(data_all.group_id.cat.categories)
|
|
579
|
+
invalid_passed_types = set(types) - all_types
|
|
580
|
+
if invalid_passed_types:
|
|
581
|
+
raise ValueError(
|
|
582
|
+
f"string type IDs {sorted(invalid_passed_types)} not found in dataset. "
|
|
583
|
+
f"Use one of {sorted(data_all.group_id.cat.categories)}."
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
UW = data.uw
|
|
587
|
+
F = Pitch.from_name(pitch).etf
|
|
588
|
+
T_all = UW * (2 * L * F) ** 2 / 386.09
|
|
589
|
+
|
|
590
|
+
# Find closest ones
|
|
591
|
+
data_sort = data.iloc[(T_all - T).abs().argsort().iloc[:n]].copy()
|
|
592
|
+
data_sort["T"] = T_all[data_sort.index]
|
|
593
|
+
data_sort["dT"] = data_sort["T"] - T
|
|
594
|
+
|
|
595
|
+
b = 1.7 # TODO: allow to set this?
|
|
596
|
+
if (data_sort["dT"] > b).all() or (data_sort["dT"] < -b).all():
|
|
597
|
+
warnings.warn(
|
|
598
|
+
f"(T={T}, L={L}, P={pitch}) "
|
|
599
|
+
f"is outside the range of what string type group(s) {types} can provide. "
|
|
600
|
+
"Maybe a different string type can give the tension/pitch/length you desire.",
|
|
601
|
+
stacklevel=2,
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
df = data_sort[["id", "T", "dT"]].sort_values(by="dT").reset_index(drop=True)
|
|
605
|
+
df.attrs.update(
|
|
606
|
+
{
|
|
607
|
+
"col_desc": {
|
|
608
|
+
"id": "Product ID",
|
|
609
|
+
"T": "Tension",
|
|
610
|
+
"dT": "Tension difference from target tension",
|
|
611
|
+
},
|
|
612
|
+
"fancy_col": {
|
|
613
|
+
"id": "ID",
|
|
614
|
+
"T": "T",
|
|
615
|
+
"dT": "ΔT",
|
|
616
|
+
},
|
|
617
|
+
}
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
return df
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stringcalc
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Instrument string calculations
|
|
5
|
+
Project-URL: Home, https://github.com/zmoon/stringcalc
|
|
6
|
+
Project-URL: Source, https://github.com/zmoon/stringcalc
|
|
7
|
+
Project-URL: Documentation, https://stringcalc.readthedocs.io
|
|
8
|
+
Author-email: zmoon <zmoon92@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Requires-Python: <4,>=3.10
|
|
13
|
+
Requires-Dist: numpy>=1.21
|
|
14
|
+
Requires-Dist: pandas
|
|
15
|
+
Requires-Dist: pint>=0.22
|
|
16
|
+
Requires-Dist: pyabc2
|
|
17
|
+
Provides-Extra: cli
|
|
18
|
+
Requires-Dist: rich; extra == 'cli'
|
|
19
|
+
Requires-Dist: typer; extra == 'cli'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# stringcalc
|
|
23
|
+
|
|
24
|
+
_Calculations for [string instrument](https://en.wikipedia.org/wiki/String_instrument)s' strings_
|
|
25
|
+
|
|
26
|
+
[](https://pypi.org/project/stringcalc/)
|
|
27
|
+
[](https://github.com/zmoon/stringcalc/actions/workflows/ci.yml)
|
|
28
|
+
[](https://stringcalc.readthedocs.io/en/latest/)
|
|
29
|
+
[](https://www.repostatus.org/#active)
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
stringcalc gauge --suggest -T 17 -L 23 -P G2 -P D3 -P A3 -P E4 -N 6 --type DA:LEW --type DA:LE --no-column-info
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
<img src="https://raw.githubusercontent.com/zmoon/stringcalc/refs/heads/main/docs/cli_gauge-suggest_tb23nw.svg"
|
|
36
|
+
alt="CLI gauge suggestion output for 23" TB for D'Addario loop-end nickel-wound strings"
|
|
37
|
+
width="550">
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
stringcalc/__init__.py,sha256=rd63tkQ4EUMHda-S0hGZfXdvRnST6YOxjM7ff2Rk5bk,137
|
|
2
|
+
stringcalc/_version.py,sha256=bauHj-EPGxrNjRoP5OYKhJo32mQBOMvEa2cW00W0jHk,738
|
|
3
|
+
stringcalc/cli.py,sha256=bor_jwkL9V-1fK8XcQ_9CSwGRzm3qONK9vFym9XxibQ,15927
|
|
4
|
+
stringcalc/frets.py,sha256=ySu8_WjdIlCRpFzYxJaMkchB4CGikf8DdX9HIhJFK9A,5006
|
|
5
|
+
stringcalc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
stringcalc/tension.py,sha256=UMaYybdHxqBlSu7UdIFQ4N3VyzOrjMUV8ol5CfxZwwY,19196
|
|
7
|
+
stringcalc/data/__init__.py,sha256=trvP9fq47dvMJJQEE4tpZUqE00Y7Tq9uaPC8m-5G-p0,30
|
|
8
|
+
stringcalc/data/aquila-nng.csv,sha256=nV-kXoRPEXr59NvzMyYob5dg5E3yEw3SW7x1obBeK80,1762
|
|
9
|
+
stringcalc/data/daddario-stp.csv,sha256=YORnKZl5sO3r6mQp6YzkstmjKOXUlwY9ehGKurK_hlo,42672
|
|
10
|
+
stringcalc/data/daddario-tension.csv,sha256=gX2waKA9Vt9Bg-jkIC62fugTAZ38EHTegOEhi5fFcsk,124808
|
|
11
|
+
stringcalc/data/ghs.csv,sha256=78KVEkXCbxvuMOE0Kjeh3sz750AqpR7uCHNguxXScWY,26733
|
|
12
|
+
stringcalc/data/stringjoy.csv,sha256=qGKvHcLKMpGMo8tN10qkyocbp8OT0WtzTqQNge_RFqU,5004
|
|
13
|
+
stringcalc/data/worth.csv,sha256=S0HVB7sww4gTR4DunkHcqR1YHgVHG8b_u_63B6vkE7s,288
|
|
14
|
+
stringcalc-0.1.0.dist-info/METADATA,sha256=MJitRQIsx-gyl7KmsMokCmp8FAmPeCbbRhZ9WdPpeBM,1732
|
|
15
|
+
stringcalc-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
16
|
+
stringcalc-0.1.0.dist-info/entry_points.txt,sha256=YA_JLHynphowPGhhA7fqsEyRIb5EYv2F7-eHbCUKWHA,50
|
|
17
|
+
stringcalc-0.1.0.dist-info/licenses/LICENSE,sha256=QJZDkt5nq7Pto8WzgmPHKnpeTyEyyqscYUFlKoyXgZg,1090
|
|
18
|
+
stringcalc-0.1.0.dist-info/RECORD,,
|