On Mon, Nov 19, 2012 at 5:27 PM, Ron S. [email protected] wrote:
The “processing” more than one file question hits the heart of the
matter. First of all, this creates a file. The processing of files is a
whole separate thing (in my way of viewing this). I want the final thing
to be working on dozens, if not hundreds and thousands of objects. And
that’s where the “object orientation,” “classes,” “methods” and all that
enters in and it’s where I get lost. It’s all so arbitrary. I can define
anything to be anything. And the more I do, the more ideas I get until I
end up with a great big jumble that falls off the rails.
I think you should go step by step, little by little. Adding
functionality and looking for opportunities to reuse code. For
example, if you want to generate a file, and the process it, you can
start by defining a class that represents the file, so that you can
create such object and pass it around. Then you start thinking what
things can be responsibility of that class, things that you would tell
it to do, for example: save yourself in the filesystem. Create a
method for that. If you start this way, you will find yourself with
little reusable things that you can compose to perform more and more
complex logic.
For example:
class MyWaveFile
def initialize frames, frequency, volume, file_name
@frames, @frequency, @volume, @file_name = frames, frequency,
volume, file_name
@w = WaveFile.new(1, 44100, 16) # don’t know what these numbers mean
generate_samples
end
def generate_samples
(0…@frames).each do |frame|
@w.sample_data[frame] = sin(frame * 2.0 * PI / @freq) * @volume *
32767
end
def save
@w.save @file_name
end
end
Now you can:
wave_file = MyWaveFile.new frames, frequency, volume, file
wave_file.save
Later, for processing, you might have several different processing
algorithms (I have absolutely no clue about sound processing). You can
create a class for each processing:
class XProcessing
end
clas YProcessing
end
…
and apply the processings in order or whatever, passing your wave_file
around:
wave_file = MyWaveFile.new frames, frequency, volume, file
XProcessing.new.process wave_file
wave_file.save
If, for example, you see duplicated code in all processing classes,
try to extract it to a common class (for example, a parent class of
all processings). This way, reducing duplication, you end up with a
design for your system.
It’s difficult at the beggining, and depending on the domains can be
quite complex, but keep working at it and you’ll see that it gets
easier and easier.
Good luck !
Jesus.