Speech to Text via URL in Visual Studio - winforms

I did a lot of research and came up with the code below. It successfully translates speech to text using the microphone.
I have a file on my webserver that streams audio via mp3. It is just a link to an mp3 file. I need to have that translated to text.
I am trying to figure out the best way to do this. So, can you select the audio input as the computers audio (ie play the audio in the web browser)? Or can you stream the audio directly to the translator? I think I need to use SetInputToWaveStream method, but do not understand how to use it.
Private Sub InitializeRecognizerSynthesizer()
Dim selectedRecognizer = ( _
Where e.Culture.Equals(Thread.CurrentThread.CurrentCulture)).FirstOrDefault()
recognizer = New SpeechRecognitionEngine(selectedRecognizer)
recognizer.AudioStateChanged += New EventHandler(Of AudioStateChangedEventArgs)(recognizer_AudioStateChanged)
recognizer.SpeechHypothesized += New EventHandler(Of SpeechHypothesizedEventArgs)(recognizer_SpeechHypothesized)
recognizer.SpeechRecognized += New EventHandler(Of SpeechRecognizedEventArgs)(recognizer_SpeechRecognized)
synthesizer = New SpeechSynthesizer()
End Sub
Private Function SelectInputDevice() As Boolean
Dim proceedLoading As Boolean = True
If IsOscompatible() Then
Try
recognizer.SetInputToDefaultAudioDevice()
Catch
'no audio input device
proceedLoading = False
End Try
Else
ThreadPool.QueueUserWorkItem(InitSpeechRecogniser)
End If
Return proceedLoading
End Function

recognizer.SetInputToWaveFile(file) - will read the audio input from a file in the file system.
recognizer.SetInputToAudioStream - will read the audio input from a stream. A short example:
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
SpeechAudioFormatInfo format = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
recognizer.SetInputToAudioStream(fs, format);
When reading from a stream or a file you must use care to make sure that the audio data is in a supported format. For example, one format I know works on my machine is:
8 bits per sample
single channel mono
22,050 samples per second
PCM encoding
See Help with SAPI v5.1 SpeechRecognitionEngine always gives same wrong result with C# for more info about audio formats.
If your question is how to fetch a resource from a web server and handle it as a stream, see HttpWebResponse.GetResponseStream - http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream(v=vs.100).aspx

Related

Is it possible to convert a Windows bitmap with an Alpha channel to a MagickImage?

Using Magick.Net, Is it possible to convert (in memory) a Windows bitmap with an Alpha channel to a MagickImage? When I try the following, it fails with .net formats Format32bppPARgb and Format32bppARgb, but works fine with Format24bppRgb. The error message is "no decode delegate for this image format `XWD'".
bmp = New System.Drawing.Bitmap(400, 300, PixelFormat.Format32bppPARgb)
img = New MagickImage(bmp)
You are getting this exception due to a bug in ImageMagick. It reads the stream and tries to determine the format. It incorrectly decides that the format is XWD instead of BMP. I submitted a patch to the GIT repository of ImageMagick to fix this. Your code will work in Magick.NET 7.0.0.0018 that has not been released at the time of writing.

Initiate file download from byte stream in Flex

I have a situation in a Flex web app where we use a PHP webservice to lookup, read the contents and then send the Base64 encoded data back to the Flex app as part of an XML document.The Flex app must then extract, Base64 decode the byte stream and then send it DIRECTLY to the browser user (for download) without writing the file to disk.
This all must occur on the click of a "DOWNLOAD NOW" button for example.
I have been searching but all I have been finding are examples of either downloading from a URL or actually writing the byte stream to disk, which i don't want.
I need to get the stream pushed directly to the browser. This way i can keep the files on the server protected by being outside the webspace as opposed to publicly accessible.
Can anyone help?
You can pop a ByteArray into a Loader then use FileReference to initiate the download dialog.
var file:FileReference = new FileReference();
var loader:Loader = new Loader();
//put loader in binary mode
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
//I will assume you are getting an appropriate ByteArray from your stream
var data:ByteArray = new ByteArray();
loader.loadBytes(data);
private function loader_completeHandler(evt:Event):void {
//now pop a save window
file.save(loader.data,"default_file_name.png")
}
edit
I will also assume you are doing this to stream a file through a non standard protocol, such as AMF, otherwise you would simply output the file over HTTP and let the browser handle the rest.

How do I asynchronously display and save bitonal Tiff images in Silverlight?

Our company has an application that receives bitonal TIFF images via fax or scanner and saves them to a database; currently, we display the images from the database using an ActiveX control, but now we want to use a Silverlight control to display those images instead. However, we are not able to display or save the images as bitonal TIFF images; using the following code in our ashx handler, we get a blank image:
pageno = Convert.ToInt32(context.Request.QueryString("page"))
'Get image data from database
Dim bmp() As Byte = ShowDocumentImage(documentID)
'Get current page of TIFF image as bitmap
Dim newBmp As Bitmap = GetTifPage(bmp, pageno)
Dim info As Imaging.ImageCodecInfo = Nothing
Dim ice As Imaging.ImageCodecInfo
For Each ice In Imaging.ImageCodecInfo.GetImageEncoders()
If ice.MimeType = "image/tiff" Then
info = ice
End If
Next
Dim enc As Imaging.Encoder = Imaging.Encoder.SaveFlag
Dim ep As New Imaging.EncoderParameters(2)
ep.Param(0) = New Imaging.EncoderParameter(enc, CLng(Imaging.EncoderValue.MultiFrame))
ep.Param(1) = New Imaging.EncoderParameter(enc, CLng(Imaging.EncoderValue.CompressionCCITT4))
If newBmp IsNot Nothing Then
newBmp.Save(context.Response.OutputStream, info, ep)
newBmp.Dispose()
End If
Instead, we have to save them as JPEG, which does display to the user but which also increases the bit depth and the size of the files; it also increases the time it takes to save the image. It takes about 6 seconds to convert a 1728x2079 JPEG to a byte array so that it can be uploaded to the database.
Is there a way to display and save the image as a bitonal TIFF, and/or a faster way to convert the image to a byte array?
As far as I know, support for TIFF images in Silverlight is very limited.
There are 3-rd party libraries that can help you with your task. I am personally recommend you to try LibTiff.Net library. The library is completely free (even for commercial use) and open source.
Source code package of LibTiff.Net contains Silverlight Test Application that shows how to asynchronously load and display TIFF images in a Silverlight app. It can display various TIFF flavors including bitonal ones.
You might find this answer to a similar question useful too.

Write / Read Byte array from/to file java

I have a java serializable object and I want to write/read that object to/from a file.
Yet Google app engine does not support fileoutputstream and objectoutputstream so I convert the serializable object to a byte array and write it to file
$FileWriteChannel writeChannel = fileService.openWriteChannel(
writableFile, lockForWrite);
$writeChannel.write(ByteBuffer.wrap(bytearray));
$writeChannel.closeFinally();
Then I read the file:
AppEngineFile readableFile = new AppEngineFile(filename);
FileReadChannel readChannel = fileService.openReadChannel(readableFile, lockForRead);
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF-8"));
String line = reader.toString();
I see the log and I got error with UTF-8
Can anybody show me what I did incorrectly ?
Error log:
cloud.spam.filter.server.SerializationObject file_io: An error message: invalid stream header: 5B4240312012/07/06 08:00:10
Thank you
Consider using protobuf for this purpose. Beside the fact that it's more compact format than standard java serialization, it will also give you ability to change (by adding new fields) class to be written into file.

Converting byte[] of a PDF file original source to MemoryStream for loading into PDF viewer? (component one)

I'm working with a ComponentOne (C1) silverlight PDF viewer control.
It has a "LoadDocument" method that accepts a "Stream".
I'm making an HTTP get call from my client app to get a PDF document.
This document, on the server side, has been streamed in through File.ReadAllBytes(), then converted to a base64 string using Convert.ToBase64String().
This string is sent across the wire back to my silverlight app where it's then reversely converted back into a byte array with Convert.FromBase64String(val).
Then I'm creating a MemoryStream with that byte array and passing "LoadDocument()" that memory stream.
The viewer is rendering nothing. It shows the toolbar and scrollbars, but the contents are blank and the save button is grayed out, suggesting that no document loaded.
I know for certain the file made it across because the byte array size on the client matches teh byte array pre-conversion on the server side.
Here's my code: (in the interest of time/space, i've truncated, removing validation, etc.)
SERVERSIDE
string sendingToClient = Convert.ToBase64String(File.ReadAllBytes(filePath))
CLIENTSIDE
byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
docViewer.LoadDocument(stream);
edit As a potential workaround, I attempted to save the file into isolated storage with a ".pdf" extension. Then I use the IsolatedStorageFileStream to send to LoadDocument().
I've come to an actual error, it now says "PdfParserException was unhandled by user code: invalid file format (missing pdf header)"
Can anyone shed some light on this PDF header?
Here is an experiment I would conduct.
Add a button to your Xaml and on click use OpenFileDialog to get a FileInfo. From that FileInfo use its Open method to get a stream and pass that to docViewer.LoadDocument.
Now run it, click the button and select the same PDF document you are trying to send from the server.
If that succeeds you need to continue investigating your server streaming strategy. On the other hand if you still have the same problem, well it doesn't get more raw than that. Try other PDF files and start investigating the PDF component. Have you ever actually used it successfully, if so how does this current usage differ.
you should get the stream pointer back to 0 ,so this should do the trick
byte[] image = null;
image = Convert.FromBase64String(stringFromServerCall);
MemoryStream stream = new MemoryStream(image);
stream.Position = 0;
docViewer.LoadDocument(stream);

Resources