Magic Lantern Forum

Developing Magic Lantern => Reverse Engineering => Topic started by: a1ex on July 30, 2012, 09:29:35 PM

Title: How to play custom WAV data
Post by: a1ex on July 30, 2012, 09:29:35 PM
StartPlayWaveData - plays a test WAV file stored in ROM.

550D: WAV data is at ff45715c - contains RIFF and WAVE strings.

If memory resource is already allocated, the code does this:

if audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/ != 0 /*NE*/:
    AJ_guess_sortof_a_copy__FROM.R1__TO.R0__LEN.R2(audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/, 0xff45715c: pointer to 0x46464952, 0x2ee24)
    SamplingRate_bit_channel(0xbb80, 0x10, HALFWORD(*0xFF457172), 0x2ee00)


0xff064eb8 SamplingRate_bit_channel:


con_puts('\nSamplingRate = %d, bit = %d, channel = %d\n', arg0, arg1, arg2) => ret_con_puts_FF064ED8
audio_struct.off_0x4 /*0x26D0*/ = BYTE(arg2)
audio_struct.off_0x60 /*0x272C*/ = arg3
audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/ = 44 + audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/
SetSamplingRate_FSamplingRate(arg0, 0x1)
src:ASIF\ASIF.c()
AJ_PowerAudioOutput()
Audio\AudioIC.c_hHPTimer(0x3)
*(-32 + sp0) = @called_by:SamplingRate_bit_channel
StartASIFDMADAC(44 + audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/, arg3, 44 + audio_struct.continuous_mem_buf /*off_0x2C, 0x26F8*/, arg3)


StartASIFDMADAC uses the following DIGIC registers:
0xC092020C, 0xC0920220, 0xC05000xx.
Title: Re: How to play custom WAV data
Post by: a1ex on July 30, 2012, 09:52:12 PM
This code seems to play a 480 Hz test tone (550D 109), and it's pretty loud :)


int N = 48000;
int16_t* buf = AllocateMemory(N*2);
for (int i = 0; i < N; i++) buf[i] = ((i/100) % 2) ? 32767 : -32767;
MEM(0x26F8) = buf;
PlayWaveData(48000, 0x10, 2, 48000);

NSTUB(0xff064eb8, PlayWaveData) // args: sampling rate, bit, channel, size maybe; wav data ptr is at 0x26F8


Enjoy!
Title: Re: How to play custom WAV data
Post by: cgapeart on July 30, 2012, 10:12:28 PM
Now that is brilliant!

Title: Re: How to play custom WAV data
Post by: a1ex on July 30, 2012, 10:19:19 PM
And with a bit of luck, I hope it will work on 5D2/50D/500D, which don't have any StartPlayWaveData function, and will probably fix the limited number of beeps in 60D.
Title: Re: How to play custom WAV data
Post by: Michael Zöller on July 30, 2012, 10:22:26 PM
Veeery interesting. Great find, a1ex! Maybe this is a possible direction: http://ltcsmpte.sourceforge.net/ .. :)
Title: Re: How to play custom WAV data
Post by: a1ex on July 30, 2012, 10:40:48 PM
Code for 5D2 2.1.2 - but only works after playing a movie with audio.


    int N = 48000;
    int16_t* buf = AllocateMemory(N*2);
    for (int i = 0; i < N; i++) buf[i] = ((i/100) % 2) ? 32767 : -32767;
    PowerAudioOutput();
    MEM(0xC092011C) = 4; // SetASIFADCModeSingleINT16
    StartASIFDMADAC(buf, 48000, buf, 48000, asif_cbr, 48000);

    NSTUB(0xff85a890, PowerAudioOutput)
    NSTUB(0xff85828c, StartASIFDMADAC)
Title: Re: How to play custom WAV data
Post by: ilguercio on July 30, 2012, 10:55:06 PM
Give me good news :P
That would mean we can sync audio a bit easier and that some audio chip is somewhere inside our 50D.
Keep on going.
Title: Re: How to play custom WAV data
Post by: a1ex on July 30, 2012, 11:26:38 PM
Yes, I'm almost there with playback on 5D2. As soon as I'll be able to play a sound without having the movie player configure the audio chip, we'll try it on 50D.

The beep is MUCH louder than autofocus beep (and volume is configurable, of course).
Title: Re: How to play custom WAV data
Post by: a1ex on July 31, 2012, 12:06:27 AM
Okay, working code for 5D2 and probably portable to 50D/500D, no idea about 600D:


    int N = 48000;
    int16_t* buf = AllocateMemory(N*2);
    for (int i = 0; i < N; i++) buf[i] = i > 4800 ? 0 : ((i/50) % 2) ? 32767 : -32767;
   
    SetSamplingRate(48000, 1);
    MEM(0xC0920210) = 6; // SetASIFDACModeInterleaveINT16
    PowerAudioOutput();
    SetAudioVolumeOut(2); // 0...5
    StartASIFDMADAC(buf, 48000, buf, 48000, asif_cbr, 48000);


TODO:
* How to specify playback duration? (the above code beeps twice)
* How to switch to a new buffer when playback stops? (maybe with asif_cbr)
Title: Re: How to play custom WAV data
Post by: ilguercio on July 31, 2012, 12:19:19 AM
 ;D
Title: Re: How to play custom WAV data
Post by: a1ex on July 31, 2012, 12:27:48 PM
5D2 code: https://bitbucket.org/hudson/magic-lantern/changeset/e09ecabaf029

No luck with 50D.
Title: Re: How to play custom WAV data
Post by: ilguercio on July 31, 2012, 05:17:56 PM
(http://i2.kym-cdn.com/entries/icons/original/000/000/063/Picture_2_c.jpg)
Title: Re: How to play custom WAV data
Post by: 3pointedit on August 06, 2012, 12:11:15 PM
Can it make a ringtone? You could sell ringtones for MagicLantern  :P fund the program or play Star Wars theme...
Title: Re: How to play custom WAV data
Post by: Greg on August 06, 2012, 01:13:29 PM
Work on 500D
Title: Re: How to play custom WAV data
Post by: a1ex on August 06, 2012, 01:21:28 PM
You can play any WAV file in theory.

So if you don't want to carry a big and clunky music player, you could just plug the headphones in the A/V jack and listen :)

On-topic: this can open the doors to voice recording feature, which was requested a few times, but I had no clue where to start to implement it.
Title: Re: How to play custom WAV data
Post by: scrax on August 06, 2012, 02:05:00 PM
Quote from: a1ex on August 06, 2012, 01:21:28 PM

On-topic: this can open the doors to voice recording feature, which was requested a few times, but I had no clue where to start to implement it.

This is great news, I'll use it for saving info with the pics like names of people shooted, notes on the pic, places etc...
Title: Re: How to play custom WAV data
Post by: 3pointedit on August 06, 2012, 02:40:56 PM
a1ex I was wondering that (audio notes) when I wrote the OT comment (sorry), but doesnt that overlap with the picture event? I guess it would be available between shots. That could be a real killer app for news photographers!
Title: Re: How to play custom WAV data
Post by: 1% on August 06, 2012, 03:48:10 PM
Quote
On-topic: this can open the doors to voice recording feature, which was requested a few times, but I had no clue where to start to implement it


600D firmware mentions a voice recorder. Maybe if you figure out how to record sound only,  600D will have audio free of canon meddling.
Title: Re: How to play custom WAV data
Post by: scrax on August 06, 2012, 06:15:49 PM
Quote from: 3pointedit on August 06, 2012, 02:40:56 PM
a1ex I was wondering that (audio notes) when I wrote the OT comment (sorry), but doesnt that overlap with the picture event? I guess it would be available between shots. That could be a real killer app for news photographers!
Yes between shot I was expecting it, You take a pic, then from PLAY mode pressing LV button (for exmple) ML records an audio file with the same name of the pic selected.
Some sort of that function is already in high end cameras for photojournalist.
Title: Re: How to play custom WAV data
Post by: 3pointedit on August 09, 2012, 10:38:09 AM
If the camera could play back audio files, perhaps small audio tips could be added to the documentation. So that a user could be directed to shortcuts or even ML training/tutorials while using the camera. That is a sequence of audio files could play depending on the feature in use...
Good for kids, possibly handy for non-English readers/speakers too.

But I guess we are only looking at record function here, not playback?
Title: Re: How to play custom WAV data
Post by: scrax on August 11, 2012, 02:31:03 PM
 :o Audio help for blank photographers?  ::)
:D
In what language? I think that is really way more easy for a non English user to read and use google translate (at least) than trying to understand something spoken.
Title: Re: How to play custom WAV data
Post by: a1ex on August 20, 2012, 04:59:43 PM
Voice recording works!!!

I'm able to record 30-second mono clips at 48 KHz, 16-bit (maybe even longer), and play them back.

Code for "don't click me":


int rec_done = 0;
static void asif_stop_cbr()
{
    rec_done = 1;
    info_led_blink(3,50,50);
}

void normalize_audio(int16_t* buf, int N)
{
    int m = 0;
    for (int i = 0; i < N; i++)
        m = MAX(m, ABS(buf[i]));
    for (int i = 0; i < N; i++)
        buf[i] = buf[i] * 32767 / m;
}

void run_test()
{
    int N = 48000*30;
    int16_t* buf = shoot_malloc(N*2);
    if (!buf) return;

    msleep(2000);
   
    info_led_on();
    rec_done = 0;
    SetSamplingRate(48000, 1);
    MEM(0xC092011C) = 4; // SetASIFDACModeSingleINT16
    StartASIFDMAADC(buf, N, 0, 0, asif_stop_cbr, N);
    while (!rec_done) msleep(100);
    info_led_off();
    msleep(1000);
   
    normalize_audio(buf, N);
    play_beep(buf, N);
}



5D2 212:

NSTUB(0xff8580e8, StartASIFDMAADC)

Title: Re: How to play custom WAV data
Post by: Jason_Jung on August 20, 2012, 05:39:12 PM
What about the 600D?
Title: Re: How to play custom WAV data
Post by: a1ex on August 20, 2012, 05:56:02 PM
I expect it to work on everything expect 50D, but only tried on 5D2.
Title: Re: How to play custom WAV data
Post by: 1% on August 20, 2012, 11:57:49 PM
Works on 600D for me. Audio has to be turned on and powered up. Started monitoring through the speaker when I had it disabled.
Title: Re: How to play custom WAV data
Post by: 3pointedit on August 21, 2012, 02:51:51 AM
What kind of file does this create? A .WAV? Or is it a peculiar format that needs decoding on the PC?
Title: Re: How to play custom WAV data
Post by: 1% on August 21, 2012, 03:25:38 AM
In theory 48khz wav but I don't think its actually writing them. It just plays back the buffer in the function.

In 600D:

NSTUB(0xFF17F734, str:VOI_FactoryRecord_[VOI]_[VOI_FactoryRecord])

NSTUB(0xFF17F5B4, str:VOI_StopVoiceRecord_[VOI]_StopSemaphore)

But why does it mess with

0x5DC4 (aAJ_0x1F50_SoundDevSet_HDMI_struct_0x00_to_0x50)

Is it related to the crazy "throw music over your files" function.

QuoteYou can now add music to videos in the camera. You can copy the music that Canon provides on the EOS Digital Solution Disk to the memory card using the provided EOS Utility program. Alternately, you can add music from your computer to the memory card, again using the EOS Utility. Then on the camera, you can choose a music selection to play as background music for the video. With the music added, all you need to do is to plug the T3i/600D into a TV, pop some popcorn, and enjoy your HD videos in style.
Title: Re: How to play custom WAV data
Post by: a1ex on August 21, 2012, 08:10:07 PM
With latest changesets it saves WAV files. You can even add voice tags to photos.
Title: Re: How to play custom WAV data
Post by: Greg on August 21, 2012, 10:50:39 PM
Work on 500D  ;D
Title: Re: How to play custom WAV data
Post by: 1% on August 22, 2012, 12:14:25 AM
I'm running out of memory with the latest:

ASSERT: FALSE
at Memory\Memory.c:188, task beep_task
lv:1 mode:20


Magic Lantern version : v2.3.NEXT.2012Aug21.600D102
Mercurial changeset   : e5ac61e7da2b+16278f5df476+ (unified) tip
Built on 2012-08-21 21:59:59 by user@D610.
Free Memory  : 334K + 1019K

157:  5407.154 [STARTUP] too heavy... free : 1018KB
    158:  8937.274 [MEM] NG AllocateMemory 480172
    159:  8937.452 [MEM] Total = 0x8b0000, Free = 0xfe934, MaxReg = 0x54ab8
    160:  8938.398 [MEM] Num Alloc = 3543, Num Free = 335
    161:  8953.146 [STARTUP] ERROR ASSERT : Memory\Memory.c, Task = beep_task
    162:  8953.237 [STARTUP] ERROR ASSERT : Line 188
    163:  8953.276 [STARTUP] ERROR ASSERT : FALSE
    164:  8953.423 [STARTUP] startupErrorRequestChangeCBR (0x1d)
    165:  8953.476 [STARTUP] startupErrorRequestChangeCBR : ErrorSend (101, ABORT)
    166:  8953.745 [STARTUP] ASSERT : Time 2012/8/21 17:10:5




Ok, I've  fixed recording using shoot_malloc. I still get error 70 on playback.

Will this work to free the shooting buffer?
FreeMemory(wav_buf);


Also changed:

FIO_WriteFile(f, shoot_malloc(wav_buf), sizeof(wav_header) + N);

int16_t* long_buf = shoot_malloc(N*2);
if (!long_buf) { N = 48000; long_buf = shoot_malloc(N*2); }
[/quote]

Did I miss a location? I can record a whole 60 second clip but can't even play back 5s... regular beeps work.


Also get a dir err at start but pressing left/right shows the file names and lets them be selected.


I put zebras into exmem space and now I can play back a 5s file. I have 1500k free... I don't think there is any more I can free....

Tried changing file reading to exmem and then I get no ML menu. Exmem is not big enough, I guess. Is there a way to find max size or make it allocate above what canon uses? I also put back allocate memory for short tones and uncachable for file writing (made no difference either way)....

Now recording is great but for some reason playback isn't working. At least the memory problems are gone. Thanks!
Title: Re: How to play custom WAV data
Post by: miyake on August 28, 2012, 06:39:22 PM
600D recording is working current codes. But play is not working yet.  I will discover how to solve it on 600D.

Anyway, 600D audio forum user testing combination with Sound recording and video recording
http://www.magiclantern.fm/forum/index.php?topic=2032.msg9437#msg9437
It will crash camera, and need battery pull.
Do you know this? And another cameras don't have this issue?

Title: Re: How to play custom WAV data
Post by: a1ex on August 28, 2012, 06:44:14 PM
As far as I know, 600D is able to beep, so it should playback sound too (maybe you redirect the sound to headphones?)

I didn't try sound recording and video recording at the same time, so it's likely to run into trouble with this.
Title: Re: How to play custom WAV data
Post by: scrax on September 03, 2012, 12:34:15 AM
I've just tried on last code from miyake and it's working ok now, but since i use Hold->Play feat for image review voice tags can't be used.
Is it possible to have an option to enable it in PLAY mode too?

If there is no audio file associated with the shown pic SET will record Voice Tag, if present it will play it (maybe an icon should be perfect to see the audio file already present) and MENU button can be used to delete them from PLAY mode.
This will also permit to take photos at an events and then tag them after the day maybe with someone who knows the subjects better.
Title: Re: How to play custom WAV data
Post by: a1ex on September 03, 2012, 09:10:19 AM
This would require a way to find out which file is selected in playback mode. If this can be done, the icon and playback are easy to implement.