Posts Tagged ‘api’
24
set

Do you want use google speech api to recognize text from a dictate?

If you want to add in your “test” project, (test project because it’s not a public API), you need to read Chrome Browser source code.

Here chromium url: src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech

Chrome records audio chunks, accepts only a FLAC file, an open source audio codec (free lossless audio codec) file with a sample rate of 16000.0!

After upload to server:

https://www.google.com/speech-api/v1/recognize

and server responds with a json like this:

1
2
3
4
5
6
7
8
9
10
11
12
{
    "status": 0,
    "id": "f3847b5dcu4d657f6667f3pk4sc0a8ca-2",
    "hypotheses": [
    {
        "utterance": "it works",
        "confidence": 0.8012238
    },
    {
        "utterance": "it works"
    }]
}

Now, using ASIFormDataRequest we can send in POST a file to google url waiting for JSON.
Objective-c code:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void) SpeechFromGooglezzz {
  NSURL *url = [NSURL URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"];

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  NSString *filePath = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"tmpAudio.flac"];

  NSData *myData = [NSData dataWithContentsOfFile:filePath];
  [request addPostValue:myData forKey:@"Content"];
  [request addPostValue:@"audio/x-flac; rate=16000" forKey:@"Content-Type"];
  [request startSynchronous];

  NSLog(@"req: %@", [request responseString]);
}

But now is there a big problem… objective-c and iphone don’t recognize FLAC files, you need an intermediate passage to send the correct audio file.

Set up your server with FFMPEG (an audio/video converting/editing tool) and prepare a PHP/JSP/etc. that accept in post an audio file, call a PHP EXEC and launch ffmpeg with flac codec.
Then upload from server to google and return response!

Here code to record and listen audio file from iPhone. Create two buttons and copy/paste code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (IBAction)StartRec:(id)sender {  
  NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithFloat: 16000.0],                 AVSampleRateKey,
                            [NSNumber numberWithInt: kAudioFormatMPEGLayer3],    AVFormatIDKey,
                            [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                            [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                            nil];
 
  NSError *error;
  NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"tmpAudio.mp3"]];
  recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
 
  if (recorder) {
    [recorder prepareToRecord];
    [recorder record];
  }
  else NSLog(@"%@", [error description]);
}
1
2
3
4
5
6
7
8
9
10
11
12
- (IBAction)PlayAudio:(id)sender {
 NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"tmpAudio.mp3"];
 
 SystemSoundID soundID;  
 NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
 AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
 AudioServicesPlaySystemSound(soundID);
}

- (IBAction)StopRec:(id)sender {
  if (recorder) [recorder stop];
}

That’s all, enjoy!

…and remember that speech function of google it’s a private API. You can’t use in a commercial app!

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , , , , ,

06
feb

…but how it works?

Apple rejected app

This

1
kCAFilterPageCurl

was a name of an NSString, not a call to undocumented API, in an unused class…
…after two hours apple responds to this mail!

They uses a grep to check application before submitting in App Store?

:(

Alberto VS Apple – phase 1!

Rif: albertopasca.it





FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , , ,

13
lug

Yes!
It’s double right click!

In this snippet you can use right double click to “go back” on your web browser!
It’s a stupid tricks, but it’s very useful!

Here we go!

As usually, copy and paste this snippet in your windows application.

First of all, you must include this libs:

using System.Runtime.InteropServices;
using System.Diagnostics;





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static MSLLHOOKSTRUCT _posDblClk;

private const int WH_MOUSE_LL = 14;

private enum MouseMessages
{
  WM_LBUTTONDOWN = 0x0201,
  WM_LBUTTONUP = 0x0202,
  WM_MOUSEMOVE = 0x0200,
  WM_MOUSEWHEEL = 0x020A,
  WM_RBUTTONDOWN = 0x0204,
  WM_RBUTTONUP = 0x0205
}

[StructLayout( LayoutKind.Sequential )]
private struct POINT
{
  public int x;
  public int y;
}

[StructLayout( LayoutKind.Sequential )]
private struct MSLLHOOKSTRUCT
{
  public POINT pt;
  public uint mouseData;
  public uint flags;
  public uint time;
  public IntPtr dwExtraInfo;
}

[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr SetWindowsHookEx( int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId );

[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
private static extern bool UnhookWindowsHookEx( IntPtr hhk );

[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr CallNextHookEx( IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam );

[DllImport( "kernel32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr GetModuleHandle( string lpModuleName );

Ok, this is first part of code, used to catch mouse event with windows API like kernel32.dll and user32.dll.

After that, create two simple method that invoke mouse hook:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private static IntPtr SetHook( LowLevelMouseProc proc )
{
  using ( Process curProcess = Process.GetCurrentProcess() )
  using ( ProcessModule curModule = curProcess.MainModule ) {
    return SetWindowsHookEx( WH_MOUSE_LL, proc, GetModuleHandle( curModule.ModuleName ), 0 );
  }
}

private delegate IntPtr LowLevelMouseProc( int nCode, IntPtr wParam, IntPtr lParam );

private static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam )
{
  if ( nCode >= 0 && MouseMessages.WM_RBUTTONDOWN == ( MouseMessages )wParam ) {
    MSLLHOOKSTRUCT hookStruct = ( MSLLHOOKSTRUCT )Marshal.PtrToStructure( lParam, typeof( MSLLHOOKSTRUCT ) );

    if ( ( _posDblClk.pt.x == hookStruct.pt.x ) && ( _posDblClk.pt.y == hookStruct.pt.y ) ) {
      SendKeys.Send( "%{LEFT}" );
    }
    _posDblClk.pt.x = hookStruct.pt.x;
    _posDblClk.pt.y = hookStruct.pt.y;
  }

  return CallNextHookEx( _hookID, nCode, wParam, lParam );
}

…you can view that in HookCallback function there is a method that use “%{LEFT}”, this is equivalent to ALT+LEFTARROW, used to go back in web browser.

SendKeys.Send( “%{LEFT}” );

When is detected a double right click, the program send to the system the ALT+LEFTARROW keys. If you are in a browser, you go back!

Finally,
in you form init, insert this lines

1
2
3
_hookID = SetHook( _proc );
Application.Run();
UnhookWindowsHookEx( _hookID );

that starting the mouse hook.

Perfect! Compile and test it!

If you like this tricks, add it in Windows Autorun!
If you like this post, click iLike!
If you don’t want to compile and want only to use it, you can download from here.




byz

Rif: albertopasca.it

FacebookTwitterDeliciousLinkedInGoogle BookmarksNetlogGoogle GmailMySpaceGoogle ReaderShare

, , , , , , , , , , , ,

Switch to our mobile site