Skip to content

Commit 5be99a3

Browse files
authored
Merge pull request #38 from MTG/yapf-formatting
Added yapf code style checking
2 parents 276591e + a41e73b commit 5be99a3

6 files changed

Lines changed: 109 additions & 72 deletions

File tree

.github/workflows/yapf.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: YAPF Formatting Check
2+
on: [push, pull_request]
3+
jobs:
4+
formatting-check:
5+
name: Formatting Check
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
- name: run YAPF to test if python code is correctly formatted
10+
uses: AlexanderMelde/yapf-action@master
11+
with:
12+
args: --verbose

download_bookmarks_example.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
print("\tCategory:", bookmark.name)
3434
print("\tNum sounds:", bookmark.num_sounds)
3535

36-
sounds_results_pager = user.get_bookmark_category_sounds(
37-
bookmark.id,
38-
fields="id,name,type",
39-
page_size=1
40-
)
36+
sounds_results_pager = user.get_bookmark_category_sounds(bookmark.id, fields="id,name,type", page_size=1)
4137

4238
while True:
4339
for sound in sounds_results_pager:

essentia_example.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pyaudio, numpy, scipy,freesound, essentia, wave, wavio
1+
import pyaudio, numpy, scipy, freesound, essentia, wave, wavio
22
from essentia.standard import *
33
from scipy.io.wavfile import *
44

@@ -9,59 +9,59 @@
99
RECORD_SECONDS = 3
1010
SIZE = 4
1111

12+
1213
def record_audio():
1314
p = pyaudio.PyAudio()
14-
stream = p.open(format=FORMAT,
15-
channels=CHANNELS,
16-
rate=RATE,
17-
input=True,
18-
frames_per_buffer=CHUNK)
19-
15+
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
16+
2017
print("* recording (3 seconds)")
2118
frames = []
2219
audio = numpy.array([])
2320
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
2421
data = stream.read(CHUNK)
2522
floats = numpy.fromstring(data, 'Float32')
2623
frames.append(data)
27-
audio = numpy.hstack((audio,floats))
24+
audio = numpy.hstack((audio, floats))
2825
print("* done")
2926
stream.stop_stream()
3027
stream.close()
3128
p.terminate()
32-
write_file(frames,'recording.wav')
29+
write_file(frames, 'recording.wav')
3330
return audio
3431

35-
def write_file(frames,fname):
32+
33+
def write_file(frames, fname):
3634
wf = wave.open(fname, 'wb')
3735
wf.setnchannels(CHANNELS)
3836
wf.setsampwidth(SIZE)
3937
wf.setframerate(RATE)
4038
wf.writeframes(b''.join(frames))
4139
wf.close()
4240

41+
4342
def extract_mfcc(audio):
44-
w = Windowing(type = 'blackmanharris62')
43+
w = Windowing(type='blackmanharris62')
4544
spectrum = Spectrum()
4645
mfcc = essentia.standard.MFCC()
47-
mfccs =[]
46+
mfccs = []
4847
audio = essentia.array(audio)
49-
for frame in FrameGenerator(audio, frameSize = 2048 , hopSize = 1024):
48+
for frame in FrameGenerator(audio, frameSize=2048, hopSize=1024):
5049
mfcc_bands, mfcc_coeffs = mfcc(spectrum(w(frame)))
5150
mfccs.append(mfcc_coeffs)
5251
mfccs = essentia.array(mfccs).T
5352
return mfccs
5453

54+
5555
def query_by_voice():
5656
c = freesound.FreesoundClient()
57-
c.set_token("<YOUR_API_KEY_HERE>","token")
57+
c.set_token("<YOUR_API_KEY_HERE>", "token")
5858
d = record_audio()
5959
mfcc_frames = extract_mfcc(d)
60-
mfcc_mean=numpy.mean(mfcc_frames,1)
61-
mfcc_var=numpy.var(mfcc_frames,1)
62-
m =",".join(["%.3f"%x for x in mfcc_mean])
63-
v =",".join(["%.3f"%x for x in mfcc_var])
64-
60+
mfcc_mean = numpy.mean(mfcc_frames, 1)
61+
mfcc_var = numpy.var(mfcc_frames, 1)
62+
m = ",".join(["%.3f" % x for x in mfcc_mean])
63+
v = ",".join(["%.3f" % x for x in mfcc_var])
64+
6565
results = c.content_based_search(target="lowlevel.mfcc.mean:"+m+" lowlevel.mfcc.var:"+v,\
6666
fields="id,name,url,analysis", descriptors="lowlevel.mfcc.mean,lowlevel.mfcc.var", \
6767
filter="duration:0 TO 3")
@@ -70,6 +70,5 @@ def query_by_voice():
7070
print(sound.name)
7171
print(sound.url)
7272
print("--")
73-
74-
73+
7574
return results

examples.py

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
print("Sound info specifying some request parameters:")
2727
print("-----------")
2828
sound = freesound_client.get_sound(
29-
96541,
30-
fields="id,name,username,duration,analysis",
31-
descriptors="lowlevel.spectral_centroid",
32-
normalized=1
29+
96541, fields="id,name,username,duration,analysis", descriptors="lowlevel.spectral_centroid", normalized=1
3330
)
3431
print("Getting sound:", sound.name)
3532
print("Username:", sound.username)
@@ -52,10 +49,7 @@
5249
# Get sound analysis example specifying some request parameters
5350
print("Get analysis with specific normalized descriptor:")
5451
print("-------------")
55-
analysis = sound.get_analysis(
56-
descriptors="lowlevel.spectral_centroid.mean",
57-
normalized=1
58-
)
52+
analysis = sound.get_analysis(descriptors="lowlevel.spectral_centroid.mean", normalized=1)
5953
spectral_centroid_mean = analysis.lowlevel.spectral_centroid.mean
6054
print("Normalized mean of spectral centroid:", spectral_centroid_mean)
6155
print()
@@ -72,9 +66,7 @@
7266
print("Similar sounds specifying some request parameters:")
7367
print("---------------")
7468
results_pager = sound.get_similar(
75-
page_size=10,
76-
fields="name,username",
77-
descriptors_filter="lowlevel.pitch.mean:[110 TO 180]"
69+
page_size=10, fields="name,username", descriptors_filter="lowlevel.pitch.mean:[110 TO 180]"
7870
)
7971
for similar_sound in results_pager:
8072
print("\t-", similar_sound.name, "by", similar_sound.username)
@@ -128,29 +120,44 @@
128120
print("\t-", sound.name, "by", sound.username)
129121
print()
130122

131-
132123
# Getting sounds from a user example specifying some request parameters
133124
print("User sounds specifying some request parameters:")
134125
print("-----------")
135126
user = freesound_client.get_user("Headphaze")
136127
print("User name:", user.username)
137128
results_pager = user.get_sounds(
138-
page_size=10,
139-
fields="name,username,samplerate,duration,analysis",
140-
descriptors="rhythm.bpm"
129+
page_size=10, fields="name,username,samplerate,duration,analysis", descriptors="rhythm.bpm"
141130
)
142131

143132
print("Num results:", results_pager.count)
144133
print("\t----- PAGE 1 -----")
145134
for sound in results_pager:
146-
print("\t-", sound.name, "by", sound.username,)
147-
print(", with sample rate of", sound.samplerate, "Hz and duration of",)
135+
print(
136+
"\t-",
137+
sound.name,
138+
"by",
139+
sound.username,
140+
)
141+
print(
142+
", with sample rate of",
143+
sound.samplerate,
144+
"Hz and duration of",
145+
)
148146
print(sound.duration, "s")
149147
print("\t----- PAGE 2 -----")
150148
results_pager = results_pager.next_page()
151149
for sound in results_pager:
152-
print("\t-", sound.name, "by", sound.username,)
153-
print(", with sample rate of", sound.samplerate, "Hz and duration of",)
150+
print(
151+
"\t-",
152+
sound.name,
153+
"by",
154+
sound.username,
155+
)
156+
print(
157+
", with sample rate of",
158+
sound.samplerate,
159+
"Hz and duration of",
160+
)
154161
print(sound.duration, "s")
155162
print()
156163

@@ -160,21 +167,37 @@
160167
pack = freesound_client.get_pack(3524)
161168
print("Pack name:", pack.name)
162169
results_pager = pack.get_sounds(
163-
page_size=5,
164-
fields="id,name,username,duration,analysis",
165-
descriptors="lowlevel.spectral_flatness_db"
170+
page_size=5, fields="id,name,username,duration,analysis", descriptors="lowlevel.spectral_flatness_db"
166171
)
167172
print("Num results:", results_pager.count)
168173
print("\t----- PAGE 1 -----")
169174
for sound in results_pager:
170-
print("\t-", sound.name, "by", sound.username, ", with duration of",)
171-
print(sound.duration, "s and a mean spectral flatness of",)
175+
print(
176+
"\t-",
177+
sound.name,
178+
"by",
179+
sound.username,
180+
", with duration of",
181+
)
182+
print(
183+
sound.duration,
184+
"s and a mean spectral flatness of",
185+
)
172186
print(sound.analysis.lowlevel.spectral_flatness_db.mean)
173187
print("\t----- PAGE 2 -----")
174188
results_pager = results_pager.next_page()
175189
for sound in results_pager:
176-
print("\t-", sound.name, "by", sound.username, ", with duration of",)
177-
print(sound.duration, "s and a mean spectral flatness of",)
190+
print(
191+
"\t-",
192+
sound.name,
193+
"by",
194+
sound.username,
195+
", with duration of",
196+
)
197+
print(
198+
sound.duration,
199+
"s and a mean spectral flatness of",
200+
)
178201
print(sound.analysis.lowlevel.spectral_flatness_db.mean)
179202
print()
180203

@@ -187,6 +210,11 @@
187210
print("Num results:", results_pager.count)
188211
print("\t----- PAGE 1 -----")
189212
for bookmark_category in results_pager:
190-
print("\t-", bookmark_category.name, "with", bookmark_category.num_sounds,)
213+
print(
214+
"\t-",
215+
bookmark_category.name,
216+
"with",
217+
bookmark_category.num_sounds,
218+
)
191219
print("sounds at", bookmark_category.url)
192220
print()

freesound.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class URIS:
4848
USER_SOUNDS = '/users/<username>/sounds/'
4949
USER_PACKS = '/users/<username>/packs/'
5050
USER_BOOKMARK_CATEGORIES = '/users/<username>/bookmark_categories/'
51-
USER_BOOKMARK_CATEGORY_SOUNDS = '/users/<username>/bookmark_categories/<category_id>/sounds/' # noqa
51+
USER_BOOKMARK_CATEGORY_SOUNDS = '/users/<username>/bookmark_categories/<category_id>/sounds/' # noqa
5252
PACK = '/packs/<pack_id>/'
5353
PACK_SOUNDS = '/packs/<pack_id>/sounds/'
5454
PACK_DOWNLOAD = '/packs/<pack_id>/download/'
@@ -83,7 +83,7 @@ class FreesoundClient:
8383
"""
8484

8585
def __init__(self):
86-
self.auth = None # should be set later
86+
self.auth = None # should be set later
8787
self.session = Session()
8888

8989
def get_sound(self, sound_id, **params):
@@ -235,11 +235,11 @@ class FSRequest:
235235

236236
@staticmethod
237237
def request(
238-
uri,
239-
params,
240-
client,
241-
wrapper=FreesoundObject,
242-
method='GET',
238+
uri,
239+
params,
240+
client,
241+
wrapper=FreesoundObject,
242+
method='GET',
243243
):
244244
req = Request(method, uri, params=params, auth=client.auth)
245245
prepared = client.session.prepare_request(req)
@@ -326,9 +326,7 @@ def more(self):
326326
"""
327327
Get more results
328328
"""
329-
return FSRequest.request(
330-
self.more, {}, self.client, CombinedSearchPager
331-
)
329+
return FSRequest.request(self.more, {}, self.client, CombinedSearchPager)
332330

333331

334332
class Sound(FreesoundObject):
@@ -388,13 +386,9 @@ def retrieve_preview(self, directory, name=None, quality='lq', file_format='mp3'
388386
'-',
389387
'Preview uris are not present in your sound object. Please add'
390388
' them using the fields parameter in your request. See '
391-
' https://www.freesound.org/docs/api/resources_apiv2.html#response-sound-list.' # noqa
389+
' https://www.freesound.org/docs/api/resources_apiv2.html#response-sound-list.' # noqa
392390
) from exc
393-
return FSRequest.retrieve(
394-
preview_attr,
395-
self.client,
396-
path
397-
)
391+
return FSRequest.retrieve(preview_attr, self.client, path)
398392

399393
def get_analysis(self, descriptors=None, normalized=0):
400394
"""
@@ -504,9 +498,7 @@ def get_bookmark_category_sounds(self, category_id, **params):
504498
505499
>>> p = u.get_bookmark_category_sounds(0)
506500
"""
507-
uri = URIS.uri(
508-
URIS.USER_BOOKMARK_CATEGORY_SOUNDS, self.username, category_id
509-
)
501+
uri = URIS.uri(URIS.USER_BOOKMARK_CATEGORY_SOUNDS, self.username, category_id)
510502
return FSRequest.request(uri, params, self.client, Pager)
511503

512504
def __repr__(self):

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[tool.yapf]
2+
based_on_style = "google"
3+
spaces_before_comment = 4
4+
split_before_logical_operator = true
5+
split_before_expression_after_opening_paren = true
6+
split_before_first_argument = true
7+
dedent_closing_brackets = true
8+
coalesce_brackets = true
9+
column_limit = 120
10+
align_closing_bracket_with_visual_indent = true

0 commit comments

Comments
 (0)