Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as too broad by gnat, MichaelT, jwenting, ChrisF May 5 '14 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.

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.