Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Using pseudocode, what would the code for an echo using recursion be?

UPDATE: Appears I should have been more clear. By echo I mean the pattern sound exhibits when it echos, not echo as in repeatly add one to self where self equals zero. Any thought on what the code would look like for a "real" echo? For example, I'd thought about "echo(reduce-volume();exit-if-silent;echo())" but was sure if the logic made sense.

UPDATE-2: Working through my own logic above an noticed it lacks a way to account for "positive feedback", which I'm guessing is required.

http://en.wikipedia.org/wiki/Positive_feedback

share|improve this question

closed as too broad by gnat, MichaelT, jwenting, ChrisF May 5 at 10:37

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 3 down vote accepted
echo sound clip S at volume V:
    if V is zero, done. (Exit recursion.)
    start playing S.
    V = V * F.
    Wait for time T.
    echo sound clip S at volume V.

F is the attenuation factor, and I suppose a good value for it would be 0.5

T is the delay between repetitions of the echo, it simulates the distance of the echoing wall.

Note: of course, this is just tail recursion, so it can be trivially converted into a loop. However, it is possible to extend it to simulate multiple echoing walls at different distances, in which case the recursion might be the best way to do it.

Positive feedback cannot be accounted for without working with the individual samples that the waveform is composed of, which calls for an altogether different, and far more complicated solution.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.