Rockbox Technical Forums

Rockbox Development => Starting Development and Compiling => Topic started by: s3t on March 14, 2010, 11:25:30 AM

Title: Adding time delay to left channel on sound output
Post by: s3t on March 14, 2010, 11:25:30 AM
Hi all,

Could you please suggest me where do i put a piece of code to add a delay to left channel?
It should be just a few ms delay - i need it in car audio installation where i use iRiver ihp-120 as main audio source.
The delay is needed to time align left and right channels in car environment.
The distance to left and right speakers in car is not symmetrical, therefore the sound produced by speakers comes with different delay to the listener, so the delays has to be "aligned" to make listener (me :) ) happy.

The way i think it should be implemented is using a FIFO buffer (array) with pointers (indexes) for reading and writing, which will store the audio data.

Another option is to mess with existing audio buffer, and adjusting it's reading addresses for different channels:
Say we are at #900 position of buffer, then the right channel reads #900, and the left -  #800 (#900-100) (100 samples difference).


I'm unfamiliar with the internals of RockBox firmware, so i ask Your advice which is the easiest way to do this?

Thank you for the best firmware ever for the mp3! It really makes the mp3 players shine! :) I love it so much i decided to put it in car instead of head unit :)
Title: Re: Adding time delay to left channel on sound output
Post by: Recording Guy on March 23, 2010, 08:04:22 PM
I would have thought a Digital Delay Pedal might be the best way to go about your task,
you could split the lead and run the left channel through the delay, then you could "fine tune"
the ms delay that you are after.

Something like this maybe :

http://www.gear4music.com/Guitar-and-Bass/Behringer-DD400-Digital-Delay-Pedal/4I1

Have you tried panning your amp to get a stronger left signal towards the drivers side?

Just a couple of thoughts to an unusual conundrum  ;)
Title: Re: Adding time delay to left channel on sound output
Post by: perfectdrug on March 23, 2010, 09:04:35 PM
I thought that is what Balance is for.
Title: Re: Adding time delay to left channel on sound output
Post by: saratoga on March 23, 2010, 09:20:25 PM
I think balance only adjusts volume, not phase delay.
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on March 24, 2010, 04:34:04 PM
The pedal is nice as long as it stays in digital domain of signal path which is not the case here... Yet it's nice to know such product exists.

I choose the old iriver player for it's ability to give spdif/toslink output, which i use to feed some diy DAC.

I want to keep away from unnecessary analog-digital-analog conversions, which, probably, will kill the quality of sound.

I'm trying to mimic the sophisticated head units which do "time alignment" feature...

Title: Re: Adding time delay to left channel on sound output
Post by: Llorean on March 24, 2010, 04:59:14 PM
Does your car have 100% isolation from outside noise?

It seems like you're going through an awful lot of effort to "improve" sound in a situation where background noise will overwhelm and of the incredibly minor improvements you're going to get from avoiding one extra D->A conversion (assuming decent things are doing the conversion)  or trying to adjust the audio delay a few milliseconds.
Title: Re: Adding time delay to left channel on sound output
Post by: ew on March 24, 2010, 05:13:29 PM
Can you really tell the difference?  Sound travels at 1125 ft/sec.  Assuming that the right speaker is 4 ft more than the left one, you are trying to insert a delay of a whopping .0036 seconds!

Do you have front and rear speakers in your car?  Does the back sound out of phase with the front?

Perhaps I am just not getting something here.
Title: Re: Adding time delay to left channel on sound output
Post by: saratoga on March 24, 2010, 05:28:58 PM
Can you really tell the difference?  Sound travels at 1125 ft/sec.  Assuming that the right speaker is 4 ft more than the left one, you are trying to insert a delay of a whopping .0036 seconds!

Thats actually a pretty large difference, and you can pretty easily hear it.  Thats a large part of how your ears directional hearing works.


http://en.wikipedia.org/wiki/Interaural_time_difference
Title: Re: Adding time delay to left channel on sound output
Post by: yapper on March 24, 2010, 05:32:46 PM
Obviously any adjustment you make to suit the driver will have an opposite effect on the passenger's experience.
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on March 25, 2010, 04:51:59 AM
Llorean, my isolation is quite mediocre, so i really doubt whether its worthwhile to build "hi-end" system in car environment...
In the other hand, the iriver's analog output is not that good, and the idea of player>spdif>dac>amplifier signal path is really great for in-car use - there is no noise coming from long RCA cables that run across the car, nor hum of ground mismatches between head unit and amplifier grounds.

The alteration of RockBox's source code is in my opinion the easiest way to get the feature of time alignment, as it should be simple to implement.
Another way of doing this is to build hardware device that will take i2s, split it into 2 channels, and run one of channels thru FIFO buffer. But it is way too much hassle for the task.

ew,
The difference is whether you feel like you are standing on the left to the singer, or in front of him.

yapper, both driver and passenger can't be happy in the car the same time...
Mostly i drive alone, so there are no passengers. And if there are some - they rarely are audiophiles. And even if they actually are audiophiles - i could be able to turn off the time correction thru the menu.
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on May 23, 2011, 06:30:10 PM
Please please please, guys, help me out with this - just a name of file i should look into.
The last time i tried to investigate the data flow, it turned out there is DMA thing, which prevented me from inserting the delay near the codec driver (the CPU feeds the codec using it's internal routines for DMA managing?)

Well, another option is to go to EQ section, and check out how the IIR filters are applied. All in all there is 100% chance of getting direct access to data stream.



UPD: dsp.c is the file, and int dsp_process(...) is the process.
t2[0..1] have both channels.
and now... i need to create global array for 64 samples, hooray!
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on May 24, 2011, 07:40:25 PM
And, the end of story: mission accomplished, i've got the delay.

It was really easy after i've found the dsp.c.
Made circulating "log" buffer of 128 samples in int32_t array, and some "if" statement for calculating the playback position.

I'm unable to make patch/contribute, as i believe my source already violated a couple of rules, i've bricked the "crossfeed" function, i don't have settings page for the delay (i use crossfeed's instead), and... my current source is already modified for SW volume.
Title: Re: Adding time delay to left channel on sound output
Post by: Recording Guy on May 25, 2011, 05:34:36 PM
Well done!  ;D
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on May 26, 2011, 06:11:38 AM
Looks like my ideas gone wild.
I want to try digital room correction in there, down to some 500-1000hz range (keeping FIR filter sequence short).
n^2 multiplifications, where n=FIR length.
n=Fs/Fo
Seems a lot of multiplifications there...
For Fo=500:
Fs:   44100
Fo:   500
Taps:   88.2
Multiplifications:   7779.24

1000hz
Fs:   44100
Fo:   1000
Taps:   44.1
Multiplifications:   1944.81

Going over >1kHz doesn't makes sense...
Well, probably i should leave the idea out.
Or... maybe... I'll throw a "while" loop in there, that will do some multiplication just to check the performance.


Probably won't work, as i need ~2000 multiplications per 44100 cycle. It translates into minimum of 44100 *2000 multiplications per second = minimum 88mhz as long as single multiplication takes 1 clock cycle.


Another idea... there is both TosLink and analog outputs on the player. I have checked the CODEC datasheet and it doesn't support TosLink = therefore, the TosLink transmitter is connected to the CPU directly = it should have separate I2S bus, and be able to produce sounds different from the CODEC.
What i'm trying to say is i could get 4 separate channels out of iRiver! All i need to squeeze there is additional delay to make the subwoofer and front stage produce time-coherent waves at listening position... Wow!
Title: Re: Adding time delay to left channel on sound output
Post by: fcporto on June 02, 2011, 07:50:06 PM
hi st3 !
Great work!
I am not a developer, just a simple RockBox user. I was eagerly waiting for someone to develop this time delay tool! I too love to hear music in my car and imaging is always misplaced in an asymmetrical listening position like this (unless you own a McLaren F1...). Time delay is a very useful feature to correct this and it is only available in top head units or sound processors. Newer cars do not allow to change the stereo, so we are left with the (usually expensive) option of adding a digital sound processor. The idea of bringing it to RockBox is great, and I just wonder if it will be incorporated in future versions of this wonderful open source software (in my case, I'm using a 5th gen iPod).
Thanks !
Title: Re: Adding time delay to left channel on sound output
Post by: s3t on June 05, 2011, 05:11:13 AM
I'll try to make a patch for this with configuration menu etc.
I'll need to make these patches first and see how do these patches move to the daily builds.

I can compile it for you, but i use an old sources (i've dovnloaded them a year ago). Just let me know the difference you have between left and right channels (in distance or time).
Title: Re: Adding time delay to left channel on sound output
Post by: fcporto on June 08, 2011, 07:37:58 AM
Hi,

I've just seen your reply. Thanks a lot!
I'll measure the distances and post them here. Since I have separated woofers and tweeters (woofers in doors and tweeters in sail panels, behind the side mirrors), the distances are different. From what I read, human hear is more sensitive to time differences in the lower frequencies and to volume level in the higher frequencies, so the distance between the woofers seems more relevant in this case.
Anyway, my previous experience with time delay, two cars ago using an Alpine headunit, tells me that the differences are only a starting point and that ultimately, the delay is set by hearing.
I think most people that hear music in their cars do not realize what a difference time delay can make to imaging and soundstage, so this would really be a great feature for future versions of rockbox.
A good additional feature would be to apply the delay to the right channel, just for those Brits who drive on the wrong side of the car ;) (not my case, anyway)

Thanks again!
Title: Re: Adding time delay to left channel on sound output
Post by: fcporto on June 12, 2011, 06:45:53 PM
So, the distances are 93 cm to the left and 133 cm to the right.
Hope you can help!
Thanks again!