lundi 29 décembre 2014

How to save SpeechSynthesisStream as audio in local storage of Windows Phone 8.1

I am using a SpeechSynthesizer class in my Windows Phone 8.1 Store app (XAML app/ windows runtime app/ universal app). It outputs a SpeechSynthesisStream which i need to save as a mp3/audio file in phone's local storage. Here's the code that i am using to generate the stream. How to save it as an audio file?



using (var speech = new SpeechSynthesizer())
{
var voiceStream = await speech.SynthesizeTextToStreamAsync(text);
}

Android: How merge(join) two OGG music files to one?

How can I merge two OGG files to one in Android (4.0+)? So far I searched a lot before posting here but I didn´t find anything useful about this. I tried to merge two OGG file with the code below:



File newFile = new File(Environment.getExternalStorageDirectory(), "test.ogg");

Vector<InputStream> inputStreams = new Vector<InputStream>();
for (String path : Globals.mCurrentProject.getSamples()) {

inputStreams.add(new FileInputStream(path));
}

Enumeration<InputStream> enu = inputStreams.elements();
SequenceInputStream sis = new SequenceInputStream(enu);

FileOutputStream fos = new FileOutputStream(newFile);

byte[] buf = new byte[8 * 1024]; // or 4 * 1024

while (true) {
int r = sis.read(buf);
if (r == -1) {
break;
}
fos.write(buf, 0, r);
}

sis.close();
fos.close();


But it´s not ideal code. This example merges the files but it´s not set the right track duration and there is a problem with song playing etc. (it should set song metadata: duration,...)


Is it possible somehow (in simple way) join two (or more) OGG files in Android?


In Java is audioInputStream but it´s not in Android. What solution do you recommend for this problem? Maybe join files on the server (send some request and in answer get file)? I prefer do this in my app but if it will be a problem, maybe use server or something else.


How to handle clock skew in audio streaming

Problem : 1. Its live audio-video streaming over Wiffi + udp network. 2. Stream : Mpeg2Ts 3. Player Framework : gstreamer. 4. Pipeline Appsrc ----> tsdemuxer -----> audio-queue---->faad decoder--->alsasink - ----->video-queue->vpudecoder ---->videosink 5. Audio device is configured for processing 48000 samples per second. 6. Senders clock is faster than receiver clock and i get this info by tracking the pcr value coming in the stream and receiver system clock. After 1 hour there is 8 second difference between sender and receiver clocks. 7. So problem is sender is sending more samples in one seconds with respect to receiver clock due to this latency between sender and receiver is keep increasing with the time.


ffmpeg avcodec_get_frame_defaults was declared deprecated

to make ffmpeg simple player


i refer to ffmpeg sample code (https://github.com/phamquy/FFmpeg-tutorial-samples/blob/master/tutorial03.c )


in window 7 with visual studio 12


first i made cmd-project, all link and complie is ok


but when i press F5 in vs12


1>ConsoleApplication1.cpp(141): error C4996: 'avcodec_get_frame_defaults': was declared deprecated Failed | ConsoleApplication1\ConsoleApplication1.vcxproj [Debug|x64]


what happen with me ?


i download latest ffmpeg dll again, but noting is changed


and google doesn't tell me what is wrong, what shold i replace with


i have no idead, if you know what function shold i use, please please warm answer


How to convert pcm data to wav file in Objective-c?

I'm now developing an iphone app and have been stuck by the audio stuff. In the app I will get raw data (NSData in PCM) every 256ms. All the raw pcm data should be added together and saved to a .wav file in Document folder. After all the data received, I could use MediaPlayer to play the wav file accordingly.


I made some research and found that a wav header should be added. But how can I do that? Any example code for adding wav file header in objective-c? or any tutorial?


how to extract part of audio in android

I'm playing an audio in my Android app, and I'd like to ask the user to set the starting and ending point and then copy the audio in-between to another file. How could I do this? So basically, if I have an audio file (in mp3 format currently, but maybe I'll need to convert it?), and two points of time, how can I copy that part of the audio in between to generate a new file in Android?


I'm kind of new to Android programming, so I'd really appreciate a relatively straightforward solution without using too many tools. Isn't there any classes or methods in Android itself to achieve something like this?


call in android using UDP

i am trying to establish call but it is not working. the project is similar to Streaming voice between Android Phones over WiFi i will post the code and i will be greatful for any help


the sender



public class MainActivity extends ActionBarActivity {


private String outputFile = null;
private Button startBtn;
private Button stopBtn;
private Button playBtn;
private Button stopPlayBtn;
private TextView text;
AudioRecord recorder;
public byte[] buffer;
private boolean status = true;
//Audio Configuration.
private int sampleRate = 8000; //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
public int minBufSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text1);
// store it to sd card
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/javacodegeeksRecording.mp3";

startBtn = (Button)findViewById(R.id.start);

startBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start(v);
}
});

stopBtn = (Button)findViewById(R.id.stop);
stopBtn.setEnabled(true);
stopBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stop(v);
}
});

playBtn = (Button)findViewById(R.id.play);
playBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
play(v);
}
});

stopPlayBtn = (Button)findViewById(R.id.stopPlay);
stopPlayBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopPlay(v);
}
});
}

public void start(View view){
try {stopBtn.setEnabled(true);

minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
byte[] buffer = new byte[4096];

Log.d("VS","Buffer created of size " + minBufSize);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS","Recorder starts");
OutputStream os = new FileOutputStream(outputFile);
Log.d("VS","Outputstream starts");
BufferedOutputStream bos = new BufferedOutputStream(os);
Log.d("VS","Buffer starts");
DataOutputStream dos = new DataOutputStream(bos);
Log.d("VS","DataOutputstream starts");
recorder.startRecording();




//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
dos.write(buffer);


} catch (IllegalStateException e) {
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
} catch (IOException e) {
// prepare() fails
e.printStackTrace();
}

text.setText("Recording Point: Recording");
startBtn.setEnabled(false);
stopBtn.setEnabled(true);

Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
}

public void stop(View view){
try {
recorder.stop();
status=false;

stopBtn.setEnabled(false);
playBtn.setEnabled(true);
text.setText("Recording Point: Stop recording");

Toast.makeText(getApplicationContext(), "Stop recording...",
Toast.LENGTH_SHORT).show();
} catch (IllegalStateException e) {
// it is called before start()
e.printStackTrace();
} catch (RuntimeException e) {
// no valid audio/video data has been received
e.printStackTrace();
}
}

public void play(View view) {
try{
minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
Log.d("VS","MinBufSize starts");
FileInputStream in=new FileInputStream( outputFile );
Log.d("VS","fileinputstream starts");
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, 4096, AudioTrack.MODE_STREAM);
byte[] byteData = null;
int bytesread = 0, ret = 0;
int size = (int) outputFile.length();
audioTrack.play();
Log.d("VS","Audio track playing starts");

int count = 512 * 1024;
while (bytesread < size) {
// Write the byte array to the track
Log.d("VS","Reading starts");
ret = in.read( byteData,0, count);
//ret =size in bytes
Log.d("VS","ret="+ ret);
Log.d("VS","continue starts");
if (ret != -1) {
audioTrack.write(byteData,0, ret);
Log.d("VS","write to audio track starts");
bytesread += ret; } //ret
else break; } //while

in.close(); audioTrack.stop(); audioTrack.release();

playBtn.setEnabled(false);
stopPlayBtn.setEnabled(true);
text.setText("Recording Point: Playing");

Toast.makeText(getApplicationContext(), "Start play the recording...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void stopPlay(View view) {
try {

playBtn.setEnabled(true);
stopPlayBtn.setEnabled(false);
text.setText("Recording Point: Stop playing");

Toast.makeText(getApplicationContext(), "Stop playing the recording...",
Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
// TODO Auto-generated catch block
Log.d("VS",e.getMessage());
e.printStackTrace();
}
}

}


the receiver :



public class MainActivity extends ActionBarActivity {
private EditText target;
private TextView streamingLabel;
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket socket;
private int port=50005; //which port??
AudioRecord recorder;
//Audio Configuration.
private int sampleRate = 8000; //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufsize=4096;
private boolean status = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
target = (EditText) findViewById (R.id.target_IP);
streamingLabel = (TextView) findViewById(R.id.streaming_label);
startButton = (Button) findViewById (R.id.start);
stopButton = (Button) findViewById (R.id.stop);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status = false;
recorder.release();
Log.d("VS","Recorder released");

}
});
startButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
status = true;
startStreaming();

}
});

}
public void startStreaming() {


Thread streamThread = new Thread(new Runnable() {

@Override
public void run() {
try {

final InetAddress destination = InetAddress.getByName(target.getText().toString());
Log.d("VS", "Address retrieved");

int minBufSize = 4096;//AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
DatagramSocket socket = new DatagramSocket(port,destination);
Log.d("VS", "Socket Created");

byte[] buffer = new byte[minBufSize];

Log.d("VS","Buffer created of size " + minBufSize);
DatagramPacket packet;



recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
Log.d("VS", "Recorder initialized");

recorder.startRecording();
Log.d("VS", "Start recording");

while(status == true) {
Log.d("VS", "entering loop");

//reading data from MIC into buffer
minBufSize = recorder.read(buffer, 0, buffer.length);
Log.d("VS", "putting data in buffer");

//putting buffer in the packet
packet = new DatagramPacket (buffer,buffer.length,destination,port);
Log.d("VS", "putting data in packet");

socket.send(packet);

Log.d("VS", "sending data");


}



} catch(UnknownHostException e) {
Log.e("VS", "UnknownHostException");
} catch (IOException e) {
Log.e("VS", "IOException");
}


}

});
streamThread.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}