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
add comment

3 Answers

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
add comment
how to echo:
read something
write it
echo everything else
share|improve this answer
    
+1 Thanks, guess 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 was made sense. –  blunders Jan 8 '12 at 6:22
1  
Ok. Sadly I know nothing about audio –  Winston Ewert Jan 8 '12 at 8:14
    
+1 Okay, fair enough - thanks for the response to my comment! –  blunders Jan 8 '12 at 8:25
add comment
Line1

check condition
  if condition ok then 
      take string
      return string

goto Line 1
share|improve this answer
    
+1 Guessing in between you working on your answer you missied the comment I posted to the other answer, which I've now added to the body of the question to avoid future confusion if possible. If it's possible to address the update that be a huge help, thanks, my bad. –  blunders Jan 8 '12 at 6:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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