NAudio Resampling in 2016? - naudio

http://mark-dot-net.blogspot.com/2014/05/how-to-resample-audio-with-naudio.html?m=1
I followed that link to make some simple code for downmixing an 8KHz WAV audio to 4100KHz.
Some of those samples work and make recognizable output, others make only noise/groaning.
I do not think it is the NAudio code itself, but how the codecs work.
Can someone point me to sample code that shows how to enumerate codecs installed in Windows 10,
or enumerate what comes with NAudio, so the various output types can be compared?
I looked at IResampler but am missing something...
This does not make recognizable sound:
using ( var waveFileReader = new AudioFileReader( FileNameIn ) )
{
var outFormat = WaveFormat.CreateIeeeFloatWaveFormat( 4100 , waveFileReader.WaveFormat.Channels );
using ( var resamplerCurrent = new MediaFoundationResampler( waveFileReader , outFormat ) )
{
resamplerCurrent.ResamplerQuality = 60;
WaveFileWriter.CreateWaveFile( tmpFile , resamplerCurrent );
}
}
These examples both make recognizable sound:
using ( var waveFileReader = new AudioFileReader( FileNameIn ) )
{
var outFormat = new WaveFormat( 4100 , waveFileReader.WaveFormat.Channels );
using ( var resamplerCurrent = new MediaFoundationResampler( waveFileReader , outFormat ) )
{
resamplerCurrent.ResamplerQuality = 60;
WaveFileWriter.CreateWaveFile( tmpFile , resamplerCurrent );
}
}
using ( var waveFileReader = new AudioFileReader( FileNameIn ) )
{
var resamplerCurrent = new WdlResamplingSampleProvider( waveFileReader , 4100 );
WaveFileWriter.CreateWaveFile16( tmpFile , resamplerCurrent );
}
Thanks.

Related

Variable is not modified in this loop - no-unmodified-loop-condition from ESLint

I have a project where ESLint throws this error from while loop. It does not make sence to me. Error says:
497:14 error 'from' is not modified in this loop no-unmodified-loop-condition
497:22 error 'to' is not modified in this loop no-unmodified-loop-condition
This is the code (look at while cycle):
mediaSettings: (state) => {
const timestamps = [];
const events = [];
state.media.forEach( medium => {
if( !medium.settings.dates ) return;
medium.settings.dates.forEach( dateRange => {
if( !dateRange.date_from || !dateRange.date_to ) return;
const from = new Date( dateRange.date_from );
const to = new Date( dateRange.date_to );
while ( from <= to ) {
if( timestamps.includes(from.getTime()) ) {
from.setDate( from.getDate() + 1 );
continue;
}
events.push({
date: new Date( from.getTime() ), // Need Date clone via new Date().
mediumId: medium.id,
});
from.setDate( from.getDate() + 1 );
};
});
});
return events;
}
What is that? Can somebody tell me plase how to fix it? It does not make sence. This is not an error.
I found clever solution on this page which disables ESLint for some block of code and also a specific ESLint rules. It looks like:
mediaSettings: (state) => {
const timestamps = [];
const events = [];
state.media.forEach( medium => {
if( !medium.settings.dates ) return;
medium.settings.dates.forEach( dateRange => {
if( !dateRange.date_from || !dateRange.date_to ) return;
const from = new Date( dateRange.date_from );
const to = new Date( dateRange.date_to );
/* eslint-disable no-unmodified-loop-condition */
while ( from <= to ) {
if( timestamps.includes(from.getTime()) ) {
from.setDate( from.getDate() + 1 );
continue;
}
events.push({
date: new Date( from.getTime() ), // Need Date clone via new Date().
mediumId: medium.id,
});
from.setDate( from.getDate() + 1 );
};
/* eslint-enable no-unmodified-loop-condition */
});
});
return events;
}
You wouldn't get this error if you are using reassignable variables like this:
...
let from = new Date(dateRange.date_from)
let to = new Date(dateRange.date_to)
while (from <= to) {
if (timestamps.includes(from.getTime())) {
from = from.setDate(from.getDate() + 1)
continue
}
events.push({
date: new Date(from.getTime()), // Need Date clone via new Date().
mediumId: medium.id,
})
from = from.setDate(from.getDate() + 1)
}
...

What is the preferred way of working with dates and JavaScript?

When working with a Faunadb record that contains a date value, I struggled with using that date in JavaScript. Eventually I got it working like so:
project.shipDate = new Date(await client.query(q.Format('%t', project.shipDate)));
This seems fine, but I also noticed I could do this:
let test = JSON.parse(JSON.stringify(project.created));
console.log(test);
datetest = new Date(test["#date"]);
Which seems wonky (grin), but may be quicker as it's not using the Fauna client library. Which should I prefer?
The JS driver has several helper classes that let you cast the javascript Time and Date objects to their FQL counterparts.
Here is an example.
From Fauna to JS
You can create a new document that contains a date value.
const project = await client.query(
q.Create(
q.Collection("projects"),
{ data: { shipDate: q.ToDate(q.Now()) } }
)
)
You can retrieve the date value and use as a javascript Date object by using the value property
const shipDate = new Date(project.data.shipDate.value)
From JS to Fauna
The date value can be modified however you want, and you can pass it back to FQL using the values.FaunaDate class.
const { values } = require('faunadb')
/* ... */
let nextDay = new Date(shipDate.getTime() + 86400000)
const faunaDate = new values.FaunaDate(nextDay)
Full Example
const project = await client.query(
q.Create(
q.Collection("projects"),
{ data: { shipDate: q.ToDate(q.Now()) } }
)
)
console.log(project)
const shipDate = new Date(project.data.shipDate.value)
console.log(shipDate)
let nextDay = new Date(shipDate.getTime() + 86400000)
console.log(nextDay)
const projectRef = project.ref
const projectUpdate = await client.query(
q.Update(
projectRef,
{ data: { shipDate: new values.FaunaDate(nextDay) } }
)
)
console.log(projectUpdate)
{
ref: Ref(Collection("projects"), "307924674409398337"),
ts: 1629918703380000,
data: { shipDate: Date("2021-08-25") }
}
2021-08-25T00:00:00.000Z
2021-08-26T00:00:00.000Z
{
ref: Ref(Collection("projects"), "307924674409398337"),
ts: 1629918703470000,
data: { shipDate: Date("2021-08-26") }
}

resize of image not working in codeigniter 3

I've been attempting to resize a image in codeigniter 3 ,but no luck! I have the resize inside the ddoo_upload() function , the resize() will work , when it comes to one image field within that form and if you will add two image field within that form , then the resize() will not work. Not sure what is wrong!
This is what i have tried for image upload,The code below shows my upload function (and resize within it)
if (isset($_FILES['destiimg']) && $_FILES['destiimg']['name'] != '') {
$filename = $this->ddoo_upload('destiimg', '2000' , '336');
} else {
$filename = NULL;
}
if (isset($_FILES['destiimg_thumb']) && $_FILES['destiimg_thumb']['name'] != '') {
$destbannerthumb_imgnew = $this->ddoo_upload('destiimg_thumb', 300 , 225);
} else {
$destbannerthumb_imgnew = NULL;
}
function ddoo_upload($filename, $width, $height)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($filename)) {
echo $this->upload->display_errors();die();
return NULL;
} else {
$data = $this->upload->data();
$filename = $data['file_name'];
$config1['image_library'] = 'gd2';
$config1['source_image'] = $this->upload->upload_path.$this->upload->file_name;
//$config1['create_thumb'] = TRUE;
$config1['maintain_ratio'] = FALSE;
$config1['width'] = $width;
$config1['height'] = $height;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
return $filename;
}
}
The upload works fine , but the resize has some issue.
You are missing some config options when loading image_lib.
Look at the manual, you didn't specified source_image (and other options).
If you want to use something advanced, there is good library called Image Moo. It's older but good working with CI3.

Set layer active photoshop script

So I have 2 open documents with the same layer names.
I want to select a layer in the first document. Then run the script and automaticly select the same layer by name in the other document.
So far i've bin able to store the first layer name and open the 2nd document.
But I can't seem to set the same layer active.
This is my code:
var aDoc = app.activeDocument;
var AllDocs = app.documents;
var actLay = aDoc.activeLayer;
if (AllDocs.length > 1) {
var itemDoc = null;
var win = new Window("dialog","select the same name in other document");
this.windowRef = win;
win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");
win.NewList=win.add ("dropdownlist", undefined, AllDocs);
win.NewList.selection = 0;
itemDoc = win.NewList.selection.index;
win.testBtn4 = win.add('button', [260,140,100,50], 'select the same name in other document', {name:'doding1'});
win.testBtn4.onClick = dothing;
//Get selected document from list
win.NewList.onChange= function () {
itemDoc = win.NewList.selection.index;
return itemDoc;
}
//Show al items
win.show();
function dothing()
{
//Make the selected document the active document.
app.activeDocument = app.documents[itemDoc];
app.refresh();
//This outputs [Artlayer layername]
//alert (actLay);
//Find right layer and set active THIS DOES NOT WORK!!
//app.activeDocument.activeLayer = app.activeDocument.layers.itemByName(actLay);
win.close();
}
}
else
{
alert ("No other documents open");
}
Figured it out! Because the layer was in a certain group it couldn't find the layer.
fixed it with the following code:
activeDocument.activeLayer = activeDocument.layerSets[groupname].artLayers.getByName (actLay);
I got this from the adobe forum.
Someone wrote a function to find the location of the layer more easily.
//usage example:
select_layer(actLay.name);
function select_layer(id, add, viz)
{
try {
var d = new ActionDescriptor();
if (viz == undefined) viz = false;
var r = new ActionReference();
if (typeof(id) == "string") r.putName( charIDToTypeID( "Lyr " ), id);
else r.putIdentifier( charIDToTypeID( "Lyr " ), id);
d.putReference( charIDToTypeID( "null" ), r );
d.putBoolean( charIDToTypeID( "MkVs" ), viz );
if (add == true) d.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
if (add == -1) d.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "removeFromSelection" ) );
var ok = true;
try { executeAction( charIDToTypeID( "slct" ), d, DialogModes.NO ); } catch(e) { ok = false; }
d = null;
return ok;
}
catch (e) { alert(e); return false; }
}

Photoshop CS6 Export Nested Layers to PNG?

I have a PSD that contains 100s of layers. Most are grouped with individual elements within the groups (nested).
How do I Export to PNG certain individual groups (with nested layers inside of them)?
I don't want to run the Export All Layers to PNG Script, just need to export individual nested groups (layers) to PNG. I've found a few other 3rd party scripts, but they export everything. Only need to export the ones I've selected.
Thanks in advance
This script should do what you want. It works only when you have the group that you want selected (not a layer within that group) I've not extensively tested it and I'm assuming you don't have groups within groups (which becomes a headache)
// export png from group
srcDoc = app.activeDocument;
var allLayers = new Array();
var selectedLayer = srcDoc.activeLayer;
var theLayers = collectAllLayers(app.activeDocument, 0);
var groupName = "";
// function collect all layers
function collectAllLayers (theParent, level)
{
for (var m = theParent.layers.length - 1; m >= 0; m--)
{
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "LayerSet")
{
var goCode = false;
allLayers.push(level + theLayer.name);
groupName = theLayer.name
// alert(level + " " + theLayer.name)
collectAllLayers(theLayer, level + 1)
var subDoc = srcDoc.layers[m];
//alert(subDoc)
var numOfSubLayers = subDoc.layers.length;
//alert(numOfSubLayers)
for (var j = numOfSubLayers -1; j >= 0 ; j--)
{
if (subDoc.layers[j].typename == "ArtLayer")
{
// if the selected (active) layer is in the group
if (theLayer == selectedLayer)
{
var mylayerName = subDoc.layers[j].name
srcDoc.activeLayer = subDoc.layers[j];
// alert("Selected: " + mylayerName + " in " + groupName)
processLayer(mylayerName)
//selectAllLayersInGroup(groupName)
}
}
}
}
}
}
function processLayer(alayername)
{
// duplicate image into new document
// =======================================================
var id2784 = charIDToTypeID( "Mk " );
var desc707 = new ActionDescriptor();
var id2785 = charIDToTypeID( "null" );
var ref508 = new ActionReference();
var id2786 = charIDToTypeID( "Dcmn" );
ref508.putClass( id2786 );
desc707.putReference( id2785, ref508 );
var id2787 = charIDToTypeID( "Nm " );
desc707.putString( id2787, alayername );
var id2788 = charIDToTypeID( "Usng" );
var ref509 = new ActionReference();
var id2789 = charIDToTypeID( "Lyr " );
var id2790 = charIDToTypeID( "Ordn" );
var id2791 = charIDToTypeID( "Trgt" );
ref509.putEnumerated( id2789, id2790, id2791 );
desc707.putReference( id2788, ref509 );
executeAction( id2784, desc707, DialogModes.NO );
// create new layer
var layerRef = app.activeDocument.artLayers.add()
layerRef.name = "temp"
layerRef.blendMode = BlendMode.NORMAL
//merge visible
// =======================================================
var id1435 = charIDToTypeID( "MrgV" );
executeAction( id1435, undefined, DialogModes.NO );
// Set filePath and fileName to source path
filePath = srcDoc.path + '/' + app.activeDocument.name + '.png';
// save out the image
var pngFile = new File(filePath);
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.embedColorProfile = true;
pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;
activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);
// alert(filePath)
// close that save png
app.activeDocument.close()
// selects document that's been open the longest
app.activeDocument = srcDoc;
}