Conversation
|
I think that it is reasonable to expect the second argument of That said, only reading the first few samples of an audio file is probably not all that useful. I would guess that the most common use cases would be either or one of Although this might be a big contender, too: |
That was exactly what I thought. I think there will always be a range of 2 values You're right that probably all Python-related |
|
How about rearranging it to To avoid confusion between This would enable things like this: I like that! On the other hand, this feels so much like indexing that this is probably more pythonic: |
|
I think that I also think that it's a really good idea to use named arguments in this case. This avoids confusion and makes the code much easier to read. Your examples are quite hard to read (without the comments), let me try to compare them with their keyword counterparts: read('filename', 0, 1024) # first 1024 frames
# vs.
read('filename', frames=1024)
# vs.
read('filename', start=0, frames=1024)The last alternative is probably the one that will be mostly used in real code (with changing read('filename', -1024) # last 1024 frames
# vs.
read('filename', start=-1024)Here the second one is definitely clearer. read('filename', 1024, -1024) # all but the first and last 1024 frames
# vs.
read('filename', start=1024, stop=-1024)Again, I like the second alternative more. I would even be willing to type the argument names in an interactive session, because the 5 characters Surprisingly many Python-Zen guidelines suggest (forced) named arguments:
Having said all that, I think that the order of f = sf.open('myfile.wav', 'w', 44100, 2, 'FLOAT')
data = sf.read('myfile.raw', 44100, 2, 'FLOAT')
sf.write(data, 'another_file.wav', 44100, 'FLOAT')
blocks = sf.blocks('newfile.wav', 'w', 44100, 2, 'FLOAT')I think that's the most consistent it can get, or am I missing something? This would mean that |
|
I think the situation is much more obvious in case of the BTW, I forgot to add the obligatory blocks = sf.blocks('newfile.wav', 'w', 44100, 2, 'FLOAT', blocksize=1024)I think it's best to force |
|
We should definitely strive for consistent function arguments. In my work, I tend to open files more often for reading than for writing, and usually try to have PySoundFile auto-guess the file meta data (fs, channels, data types). I don't think I ever specified more than the sample rate and the number of channels. Thus, realistically, I would expect a more typical use case for me to look something like this: sf.write(data, 'myfile.wav', 44100)
data = sf.read('myfile.flac')
for block in sf.blocks('myfile.flac', 'rw'):
block[:] = block*hann(len(block))I think that these are the use cases we should focus most on. In particular, I would say that the data formats should go last. I have never had any use case for them -- I prefer MAT-files for float data, since WAV/FLAC files that are not playable in my audio players are pretty useless to me. However, if I were to use a special format, I would most likely do something like format = {'format':'RAW', 'subtype':'FLOAT', 'endian':'FILE'}
sf.write(data, 'myfile.raw', **format)
data = sf.read('myfile.raw', dtype='float32', **format)This is largely tangential to the issue at hand though. In the absence of any agreeable order for |
This way the order is more consistent with the open()/write() functions.
It is no longer possible to write this, however:
data, fs = sf.read('myfile.wav', 1024)
This might be a somehow frequent use case, but it might be unclear what
the numeric argument means ('frames' or 'start' or ...?).
It will lead to clearer user code if we force a keyword argument, e.g.:
data, fs = sf.read('myfile.wav', start=31542, frames=1024)
As a side effect, the settings for RAW files can now be specified as
positional arguments:
data, fs = sf.read('myfile.raw', 44100, 2, 'FLOAT')
|
Your examples are indeed more realistic (except the blocks-thing ... and I guess you meant a I like the I changed the order again as discussed. Now the arguments of Is this OK? |
You are right of course, these should have been for-loops. Somehow, I make this error constantly in Github comments, yet never in actual code. Weird. The argument order looks good to me! |
Change order of arguments in read() function
I think this way the use cases for positional arguments are more meaningful (see commit message).
I'm not 100% sure, though ...
Any comments?