How to mix a lot of audio files with NAudio? - c#

For example, I have 5 audio files. And, I want to combine these files with C# NAudio.
<------- A.wav -------> <---- B.wav ---->
<---- C.wav ----> <-- D.wav --> <- E.wav ->
I goggled but I can find a solution which apply this situation.
<----- A.wav ----->
<-- B.wav -->
<------ C.wav ------>
How can I combine a lot of audio files with each length and position?

You can do this with the MixingSampleProvider
Use an AudioFileReader for each file. Then use the DelayBy extension on ISampleProvider to insert silence at the start of each file so they begin at the correct position.

Related

How do I know where my WAV data starts in a WAV file so that I can read the XML before it?

I have some WAV audio files from a dictaphone that have a little special format. The files have a header containing XML data indicating the start and stop time of the file. I have tried to use XmlReader but its one line of data in the beginning of the file and then I get an error.
RIFF .. one line of binary data ..
<?xml version="1.0" encoding="utf-16"?>
<FileData>
<StartTime>2019-09-16 15:27:35</StartTime>
<StopTime>2019-09-16 15:28:32</StopTime>
</FileData>
...audio data ...
I am building a small program that will be able to read the XML data from the file header and then sort out the start time and make it a string that I can use as a value in the program. I will, for example, later in the program rename the file to the start time. The files are, as I said, WAV but with this special XML data added first in the file.
Your WAV data should start with the bytes 0x52 0x49 0x46 0x46 ("RIFF"). So start by reading the file and looking for a 0xA OR 0xD followed by the 4 bytes above. That should be the start of your WAV data. According to your description, everything preceeding the start of the WAV data should be an XML string. You can read that and load it into an XmlDocument or XmlReader - whichever API works best for you.

Saving Bigger Files with TTS Android

Recently i used android TTS - I save the file as MP3 and play it using MediaPlayer so users can pause/resume etc.
It all works fine other than when i have a large text it just does not work.
I read that the android TTS has the limit of 4000 CHs? What should i do to tackle large amount of text?
The following is the code i am using to save MP3
Android.Speech.Tts.TextToSpeech textToSpeech;
...
textToSpeech = new Android.Speech.Tts.TextToSpeech(this, this, "com.google.android.tts");
...
textToSpeech.SynthesizeToFile(ReadableText, null, new Java.IO.File(System.IO.Path.Combine(documentsPath, ID + "_audio.mp3")), ID);
The following is the code i am using to playback the audio
MediaPlayer MP = new MediaPlayer();
...
MP.SetDataSource(System.IO.Path.Combine(documentsPath, ID + "_audio.mp3"));
MP.Prepare();
MP.Start();
It works for small amount of text but not for large text.
File gets saved (most likely just a corrupt file) because when i play it i get the following error
setDataSoruceFD failed: status=0x80000000
Java Solution is also acceptable
FYI - The question is about the max text size as I can generate the file for smaller text
Cheers
In Android ASOP (at least since API-18), TextToSpeech.MaxSpeechInputLength is set to 4000.
Note: OEMs could change this value in their OS image, so it would be wise to check the value and not make any assumptions.
Note: You are naming the output with an .mp3 extension, but by default the files created will be .wav formatted, some speech engines do support other formats/bitrate/etc. but you are passing null for the parameters.
Unless you want to deal with properly joining multiple wave files, I would recommend that you break your text into smaller parts and synthesize multiple files.
You can then play these back in sequence (using the MediaPlayer Completion event|listener).

GDCM library cannot read dicom file in C#

I tried to read a DICOM file using GDCM library using this code :
gdcm.ImageReader imagereader = new gdcm.ImageReader();
imagereader.SetFileName(#"E:\sample_success.dcm");
if (!imagereader.Read()) throw new Exception("Cannot read dicom file!");
For "sample_success.dcm" file, I can read the file just fine (sample_success.png).
But using "sample_failed.dcm" file, GDCM throws exception because it couldn't read it. I tried to open the file using other DICOM viewer such as Radiant and it worked. Is there something wrong with my GDCM build? Why it cannot read it?
I use GDCM 2.6.5. Please find both samples here.
You're file contains garbage (bunch of binary 0) after offset 0x1480aa (somewhere in the Pixel Data attribute). What did you expect from a toolkit if not report properly an error ?
By design GDCM will still load whatever it can until the error. So if you remove your new Exception in your code, you can decide (for example) to pass the imagereader.GetFile() to a gdcm::Writer and rewrite the file as clean DICOM.
As a side note I do not have access to Radiant software but I find it very odd that it does not indicate an error in that case.
I've checked with DCMTK and dicom3tools they all report a parsing issue.
Using the gdcm command line tool you can almost rewrite the file clean using:
$ gdcmconv -I sample_failed.dcm sample_failed_correct.dcm
Because your input dataset is invalid, GDCM (falsely) believe to see an attribute, you can remove it using:
$ gdcmanon --dumb --remove 0,0 sample_failed_correct.dcm sample_failed_correct_clean.dcm
And then:
$ gdcminfo sample_failed_correct.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.3.1 [Ultrasound Multi-frame Image Storage]
TransferSyntax is 1.2.840.10008.1.2.4.50 [JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression]
NumberOfDimensions: 3
Dimensions: (800,600,21)
SamplesPerPixel :3
BitsAllocated :8
BitsStored :8
HighBit :7
PixelRepresentation:0
ScalarType found :UINT8
PhotometricInterpretation: YBR_FULL_422
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2.4.50
Origin: (0,0,0)
Spacing: (0.0106324,0.0106324,1)
DirectionCosines: (1,0,0,0,1,0)
Rescale Intercept/Slope: (0,1)
Orientation Label: AXIAL
Which is valid with the number of Fragments in the Pixel Data:
$ gdcmdump sample_failed_correct.dcm | grep Item | grep "ff.d8" | wc
21 126 2856

Detecting on which frame there is audio presence on a video

I have a few video files that corresponds to a tv zapping (one channel with sound, then a black screen without sound, then sound again with the new channel)
I already detect pretty much everything but I would like to know how long it takes for the audio to appear after the end of the black screen.
Basically I extract the audio from the video and giving the starting frame I would like to know at which frame there is some audio again.
Then using that I can easily calculate the time it took for the audio to appear.
You can use the blackdetect and silencedetect filters and correlate the results.
Example usage:
ffmpeg -i <input> -filter:v blackdetect=d=0.1 -filter:a silencedetect=d=0.1 -f null /dev/null
where 0.1 is the minimum duration (positive floating point value in seconds).
Example output:
[blackdetect # 0x35f65e0] black_start:0.0464444 black_end:0.566444 black_duration:0.52
[blackdetect # 0x35f65e0] black_start:5.16644 black_end:5.32644 black_duration:0.16
[blackdetect # 0x35f65e0] black_start:15.9264 black_end:16.1664 black_duration:0.24
[blackdetect # 0x35f65e0] black_start:33.4464 black_end:33.6064 black_duration:0.16
[blackdetect # 0x35f65e0] black_start:63.9664 black_end:64.7664 black_duration:0.8
[silencedetect # 0x35f8700] silence_start: 67.0521
[silencedetect # 0x35f8700] silence_end: 67.9184 | silence_duration: 0.866236

My code crashes if it tries to play a mp3 file, works fine for wav files

All my sound clips are in mp3, so I do not really feel like translating them tp wav.
If I try to play a mp3 file I get a exception, but the code works fine for .wav files.
I figure there must be a way to play mp3 files.
Stream s = TitleContainer.OpenStream("sounds/bag.mp3");
// throws a exceptio if its a mp3 file
SoundEffect effect = SoundEffect.FromStream(s);
FrameworkDispatcher.Update();
effect.Play();
It is by design - SoundEffect.FromStream only works with wave files. As per MSDN:
The Stream object must point to the head of a valid PCM wave file.
Also, this wave file must be in the RIFF bitstream format.
The audio format has the following restrictions:
Must be a PCM wave file
Can only be mono or stereo
Must be 8 or 16 bit
Sample rate must be between 8,000 Hz and 48,000 Hz
Try to use Media Element..
MediaElement MyMedia = new MediaElement();
MyMedia.Source = new Uri("sounds/bag.mp3", UriKind.RelativeOrAbsolute);
MyMedia.Play();

Categories

Resources