Add multiple samples to one instrument in Gervill SF2Soundbank - midi

I am using Gervill to create a soundbank with instruments. I've recorded a sample for each tone pitch and now I'd like to put these samples into one instrument.
The documentation that I used so far are tests from the openjdk6 source code. Apart from that I found an example by Karl Helgason, that helped greatly. The example loads an audio file into a soundbank however it only uses one sample per instrument. I've modified his example file and when I use the sound bank for replay, it seems that only one sample is used and pitched according to the requested tone pitch. In contrast I want to use a specific sample per tone pitch.
I suspect that my for-loop is constructed around the wrong parts of the method and that an additional sample is overwriting the formerly saved one.
My question is which parts should every sample have separately: The layer? Or the region? Both? Unfortunately Gervill terminology seems to be slightly different from another one that I found, so I am a bit confused.
I've used the following source code (I left the copyright note in the altered source code, I am not a lawyer, so I am not sure if this is the right thing to do.):
/*
* Copyright (c) 2007 by Karl Helgason
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package midiplay;
import com.sun.media.sound.*;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.Patch;
import javax.sound.sampled.*;
public class MakeSoundfont {
public SF2Soundbank CreateSoundbank()
throws UnsupportedAudioFileException, IOException {
SF2Soundbank sf2 = new SF2Soundbank();
String[] fnames = new String[]{"C.wav", "Cs.wav", "D.wav", "Ds.wav"};
SF2Layer layer = new SF2Layer(sf2);
layer.setName("fname Layer");
sf2.addResource(layer);
int i = 0;
for (String fname : fnames) {
File audiofile = new File("./" + fname);
AudioInputStream audiostream = AudioSystem
.getAudioInputStream(audiofile);
AudioFormat format = new AudioFormat(audiostream.getFormat()
.getSampleRate(), 16, 2, true, false);
AudioInputStream convaudiostream = AudioSystem.getAudioInputStream(
format, audiostream);
/*
* Read the content of the file into a byte array.
*/
int datalength = (int) convaudiostream.getFrameLength()
* format.getFrameSize();
byte[] data = new byte[datalength];
convaudiostream.read(data, 0, data.length);
audiostream.close();
/*
* Create SoundFont2 sample.
*/
SF2Sample sample = new SF2Sample(sf2);
sample.setName(fname);
sample.setData(data);
sample.setSampleRate((long) format.getSampleRate());
sample.setOriginalPitch(60 + i);
sf2.addResource(sample);
i++;
/*
* Create region for layer.
*/
SF2LayerRegion region = new SF2LayerRegion();
region.putInteger(SF2Region.GENERATOR_RELEASEVOLENV, 12000);
region.setSample(sample);
layer.getRegions().add(region);
}
/*
* Create SoundFont2 instrument.
*/
SF2Instrument ins = new SF2Instrument(sf2);
ins.setName("Back Instrument");
ins.setPatch(new Patch(0, 0));
sf2.addInstrument(ins);
/*
* Create region for instrument.
*/
SF2InstrumentRegion insregion = new SF2InstrumentRegion();
insregion.setLayer(layer);
ins.getRegions().add(insregion);
return sf2;
}
}
EDIT: I seem to listen to all the samples at once. They are played at the same time, so I just realized the following: I am only setting the original pitch of the sample, but nowhere do I set the scope, i.e. I do not assign the samples to certain midi keys. Where can I do that?

I found the solution myself after reading some Gervill source code. Using GENERATOR_KEYRANGE one can define the range of tone pitches for a specific sample:
SF2LayerRegion region = new SF2LayerRegion();
region.putInteger(SF2Region.GENERATOR_RELEASEVOLENV, 12000);
region.putBytes(SF2Region.GENERATOR_KEYRANGE, new byte[]{(byte)(60+i),(byte)(60+i)});
i++;
region.setSample(sample);
layer.getRegions().add(region);

Related

std::lock_guard (mutex) produces deadlock

First: Thanks for reading this question and tryin' to help me out. I'm new to the whole threading topic and I'm facing a serious mutex deadlock bug right now.
Short introduction:
I wrote a game engine a few months ago, which works perfectly and is being used in games already. This engine is based on SDL2. I wanted to improve my code by making it thread safe, which would be very useful to increase performance or to play around with some other theoretical concepts.
The problem:
The game uses internal game stages to display different states of a game, like displaying the menu, or displaying other parts of the game. When entering the "Asteroid Game"-stage I recieve an exception, which is thrown by the std::lock_guard constructor call.
The problem in detail:
When entering the "Asteroid Game"-stage a modelGetDirection() function is being called to recieve a direction vector of a model. This function uses a lock_guard to make this function being thread safe. When debugging this code section this is where the exception is thrown. The program would enter this lock_guard constructor and would throw an exception. The odd thing is, that this function is NEVER being called before. This is the first time this function is being called and every test run would crash right here!
this is where the debugger would stop in threadx:
inline int _Mtx_lockX(_Mtx_t _Mtx)
{ // throw exception on failure
return (_Check_C_return(_Mtx_lock(_Mtx)));
}
And here are the actual code snippets which I think are important:
mutex struct:
struct LEMutexModel
{
// of course there are more mutexes inside here
mutex modelGetDirection;
};
engine class:
typedef class LEMoon
{
private:
LEMutexModel mtxModel;
// other mutexes, attributes, methods and so on
public:
glm::vec2 modelGetDirection(uint32_t, uint32_t);
// other methods
} *LEMoonInstance;
modelGetDirection() (engine)function definition:
glm::vec2 LEMoon::modelGetDirection(uint32_t id, uint32_t idDirection)
{
lock_guard<mutex> lockA(this->mtxModel.modelGetDirection);
glm::vec2 direction = {0.0f, 0.0f};
LEModel * pElem = this->modelGet(id);
if(pElem == nullptr)
{pElem = this->modelGetFromBuffer(id);}
if(pElem != nullptr)
{direction = pElem->pModel->mdlGetDirection(idDirection);}
else
{
#ifdef LE_DEBUG
char * pErrorString = new char[256 + 1];
sprintf(pErrorString, "LEMoon::modelGetDirection(%u)\n\n", id);
this->printErrorDialog(LE_MDL_NOEXIST, pErrorString);
delete [] pErrorString;
#endif
}
return direction;
}
this is the game function that uses the modelGetDirection method! This function would control a space ship:
void Game::level1ControlShip(void * pointer, bool controlAble)
{
Parameter param = (Parameter) pointer;
static glm::vec2 currentSpeedLeft = {0.0f, 0.0f};
glm::vec2 speedLeft = param->engine->modelGetDirection(MODEL_VERA, LEFT);
static const double INCREASE_SPEED_LEFT = (1.0f / VERA_INCREASE_LEFT) * speedLeft.x * (-1.0f);
// ... more code, I think that's not important
}
So as mentioned before: When entering the level1ControlShip() function, the programm will enter the modelGetDirection() function. When entering the modelGetDirection() function an exception will be thrown when tryin' to call:
lock_guard<mutex> lockA(this->mtxModel.modelGetDirection);
And as mentioned, this is the first call of this function in the whole application run!
So why is that? I appreciate any help here! The whole engine (not the game) is an open source project and can be found on gitHub in case I forgot some important code snippets (sorry! in that case):
GitHub: Lynar Moon Engine
Thanks for your help!
Greetings,
Patrick

As to the GPIOTE function(External Interrupt) of nRF52832

I have some trouble to control GPIOTE function with nRF52832 sdk,
when using 14.01 version(SDK), it seems that the GPIOTE function can't be used with BLE function, I used the code below, it made hang-up issue of system, why?
I wonder whether GPIOTE function can't be used with BLE function or not,
and another method to use the function with BLE function,
thankful for your support and kindness in advance,
#define PIN_IN BUTTON_4
//#define PIN_OUT BSP_LED_3
void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
printf("love %d: %d\n", (int)pin, (int)action);
// nrf_drv_gpiote_out_toggle(PIN_OUT);
}
/**
* #brief Function for configuring: PIN_IN pin for input, PIN_OUT pin for output,
* and configures GPIOTE to give an interrupt on pin change.
*/
void gpio_external_int_init(void)//love_1108
{
uint32_t err_code;
err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);
//
err_code = nrf_drv_gpiote_init();
APP_ERROR_CHECK(err_code);
//
// (void)nrf_drv_gpiote_init();
// nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
// err_code = nrf_drv_gpiote_out_init(PIN_OUT, &out_config);
// APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
in_config.pull = NRF_GPIO_PIN_PULLUP;
err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_event_enable(PIN_IN, true);
}
While you don't provide much detail, such as what is meant by "used with BLE function", I have found an issue with the SDK example ble_app_template. In my case the cause was that the file bsp_btn_ble.c demands that there are more buttons than my board_custom.h defines. So the function startup_event_extract() wants to check the state of BTN_ID_WAKEUP_BOND_DELETE, which does not exist on my hardware, causes an assertion. It is disturbing that BTN_ID_WAKEUP_BOND_DELETE and other buttons are defined in the c file, rather than being derived from custom_board.h.
So, trace the board initialization and you may find something like ASSERT(button_idx < BUTTONS_NUMBER), which caused a hang in my case.

Why does the Linux kernel have `struct sock` and `struct socket`?

This questions was asked before on the Internet, but I couldn't find a good answer.
The Linux kernel networking stack features two structures:
struct socket, generally stored in a variable sock
struct sock, generally stored in a variable sk
The two structures are essentially linked, but seem to have slightly different lifetimes. One can find an sk via sock->sk, or find a sock via sk->sk_socket.
Why are there two structures to store information about sockets? Assuming I need to add a new field, when would I add it to struct socket and when to struct sock?
UPDATE: Please note that I refer to struct socket in include/linux/net.h inside the Linux source code, which is meant for kernel code only, and not /usr/include/sys/socket.h which is meant for userland.
struct socket seems to be a higher level interface that is used for system calls (that is why it also has pointer to struct file which represents file descriptor here).
struct sock is a in-kernel implemenation for AF_INET sockets (there is also struct unix_sock for AF_UNIX sockets which is derivative of this) which can be used both by kernel and by userspace (via struct sock).
Both were added to Linux 1.0 back in 1993, I doubt you'll find a doc specifying initial design decision.
“The two structures are essentially linked” - not sure what you meant.
I guess you could find answer if look at source files for these structures:
socket -> linux-src/include/linux/net.h
sock -> linux-src/include/net/sock.h
socket
* NET An implementation of the SOCKET network access protocol.
* This is the master header file for the Linux NET layer,
* or, in plain English: the networking handling part of the
* kernel.
sock
* INET An implementation of the TCP/IP protocol suite for the LINUX
* operating system. INET is implemented using the BSD Socket
* interface as the means of communication with the user level.
These structures are different and has different representation of socket abstraction.
Here answer about different sockets.
Unix vs BSD vs TCP vs Internet sockets?
Where to define additional field depends on your intention. Please describe your task.
Please look at sources.
linux-src/include/linux/net.h
/*
* NET An implementation of the SOCKET network access protocol.
* This is the master header file for the Linux NET layer,
* or, in plain English: the networking handling part of the
* kernel.
*
* Version: #(#)net.h 1.0.3 05/25/93
*
* Authors: Orest Zborowski, <obz#Kodak.COM>
* Ross Biro
* Fred N. van Kempen, <waltje#uWalt.NL.Mugnet.ORG>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
.....
.....
.....
/**
* struct socket - general BSD socket
* #state: socket state (%SS_CONNECTED, etc)
* #type: socket type (%SOCK_STREAM, etc)
* #flags: socket flags (%SOCK_NOSPACE, etc)
* #ops: protocol specific socket operations
* #file: File back pointer for gc
* #sk: internal networking protocol agnostic socket representation
* #wq: wait queue for several uses
*/
struct socket {
socket_state state;
kmemcheck_bitfield_begin(type);
short type;
kmemcheck_bitfield_end(type);
unsigned long flags;
struct socket_wq __rcu *wq;
struct file *file;
struct sock *sk;
const struct proto_ops *ops;
};
linux-src/include/net/sock.h
/*
* INET An implementation of the TCP/IP protocol suite for the LINUX
* operating system. INET is implemented using the BSD Socket
* interface as the means of communication with the user level.
*
* Definitions for the AF_INET socket handler.
*
* Version: #(#)sock.h 1.0.4 05/13/93
*
* Authors: Ross Biro
* Fred N. van Kempen, <waltje#uWalt.NL.Mugnet.ORG>
* Corey Minyard <wf-rch!minyard#relay.EU.net>
* Florian La Roche <flla#stud.uni-sb.de>
*
* Fixes:
* Alan Cox : Volatiles in skbuff pointers. See
* skbuff comments. May be overdone,
* better to prove they can be removed
* than the reverse.
* Alan Cox : Added a zapped field for tcp to note
* a socket is reset and must stay shut up
* Alan Cox : New fields for options
* Pauline Middelink : identd support
* Alan Cox : Eliminate low level recv/recvfrom
* David S. Miller : New socket lookup architecture.
* Steve Whitehouse: Default routines for sock_ops
* Arnaldo C. Melo : removed net_pinfo, tp_pinfo and made
* protinfo be just a void pointer, as the
* protocol specific parts were moved to
* respective headers and ipv4/v6, etc now
* use private slabcaches for its socks
* Pedro Hortas : New flags field for socket options
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
....
....
....
/**
* struct sock - network layer representation of sockets
* #__sk_common: shared layout with inet_timewait_sock
* #sk_shutdown: mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
* #sk_userlocks: %SO_SNDBUF and %SO_RCVBUF settings
* #sk_lock: synchronizer
* #sk_kern_sock: True if sock is using kernel lock classes
* #sk_rcvbuf: size of receive buffer in bytes
* #sk_wq: sock wait queue and async head
* #sk_rx_dst: receive input route used by early demux
* #sk_dst_cache: destination cache
* #sk_dst_pending_confirm: need to confirm neighbour
* #sk_policy: flow policy
* #sk_receive_queue: incoming packets
* #sk_wmem_alloc: transmit queue bytes committed
* #sk_tsq_flags: TCP Small Queues flags
* #sk_write_queue: Packet sending queue
* #sk_omem_alloc: "o" is "option" or "other"
* #sk_wmem_queued: persistent queue size
* #sk_forward_alloc: space allocated forward
* #sk_napi_id: id of the last napi context to receive data for sk
* #sk_ll_usec: usecs to busypoll when there is no data
* #sk_allocation: allocation mode
* #sk_pacing_rate: Pacing rate (if supported by transport/packet scheduler)
* #sk_pacing_status: Pacing status (requested, handled by sch_fq)
* #sk_max_pacing_rate: Maximum pacing rate (%SO_MAX_PACING_RATE)
* #sk_sndbuf: size of send buffer in bytes
* #__sk_flags_offset: empty field used to determine location of bitfield
* #sk_padding: unused element for alignment
* #sk_no_check_tx: %SO_NO_CHECK setting, set checksum in TX packets
* #sk_no_check_rx: allow zero checksum in RX packets
* #sk_route_caps: route capabilities (e.g. %NETIF_F_TSO)
* #sk_route_nocaps: forbidden route capabilities (e.g NETIF_F_GSO_MASK)
* #sk_gso_type: GSO type (e.g. %SKB_GSO_TCPV4)
* #sk_gso_max_size: Maximum GSO segment size to build
* #sk_gso_max_segs: Maximum number of GSO segments
* #sk_lingertime: %SO_LINGER l_linger setting
* #sk_backlog: always used with the per-socket spinlock held
* #sk_callback_lock: used with the callbacks in the end of this struct
* #sk_error_queue: rarely used
* #sk_prot_creator: sk_prot of original sock creator (see ipv6_setsockopt,
* IPV6_ADDRFORM for instance)
* #sk_err: last error
* #sk_err_soft: errors that don't cause failure but are the cause of a
* persistent failure not just 'timed out'
* #sk_drops: raw/udp drops counter
* #sk_ack_backlog: current listen backlog
* #sk_max_ack_backlog: listen backlog set in listen()
* #sk_uid: user id of owner
* #sk_priority: %SO_PRIORITY setting
* #sk_type: socket type (%SOCK_STREAM, etc)
* #sk_protocol: which protocol this socket belongs in this network family
* #sk_peer_pid: &struct pid for this socket's peer
* #sk_peer_cred: %SO_PEERCRED setting
* #sk_rcvlowat: %SO_RCVLOWAT setting
* #sk_rcvtimeo: %SO_RCVTIMEO setting
* #sk_sndtimeo: %SO_SNDTIMEO setting
* #sk_txhash: computed flow hash for use on transmit
* #sk_filter: socket filtering instructions
* #sk_timer: sock cleanup timer
* #sk_stamp: time stamp of last packet received
* #sk_tsflags: SO_TIMESTAMPING socket options
* #sk_tskey: counter to disambiguate concurrent tstamp requests
* #sk_zckey: counter to order MSG_ZEROCOPY notifications
* #sk_socket: Identd and reporting IO signals
* #sk_user_data: RPC layer private data
* #sk_frag: cached page frag
* #sk_peek_off: current peek_offset value
* #sk_send_head: front of stuff to transmit
* #sk_security: used by security modules
* #sk_mark: generic packet mark
* #sk_cgrp_data: cgroup data for this cgroup
* #sk_memcg: this socket's memory cgroup association
* #sk_write_pending: a write to stream socket waits to start
* #sk_state_change: callback to indicate change in the state of the sock
* #sk_data_ready: callback to indicate there is data to be processed
* #sk_write_space: callback to indicate there is bf sending space available
* #sk_error_report: callback to indicate errors (e.g. %MSG_ERRQUEUE)
* #sk_backlog_rcv: callback to process the backlog
* #sk_destruct: called at sock freeing time, i.e. when all refcnt == 0
* #sk_reuseport_cb: reuseport group container
* #sk_rcu: used during RCU grace period
*/
struct sock {
/*
* Now struct inet_timewait_sock also uses sock_common, so please just
* don't add nothing before this first member (__sk_common) --acme
*/
struct sock_common __sk_common;
#define sk_node __sk_common.skc_node
#define sk_nulls_node __sk_common.skc_nulls_node
#define sk_refcnt __sk_common.skc_refcnt
#define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping
#define sk_dontcopy_begin __sk_common.skc_dontcopy_begin
#define sk_dontcopy_end __sk_common.skc_dontcopy_end
#define sk_hash __sk_common.skc_hash
#define sk_portpair __sk_common.skc_portpair
#define sk_num __sk_common.skc_num
#define sk_dport __sk_common.skc_dport
#define sk_addrpair __sk_common.skc_addrpair
#define sk_daddr __sk_common.skc_daddr
#define sk_rcv_saddr __sk_common.skc_rcv_saddr
#define sk_family __sk_common.skc_family
#define sk_state __sk_common.skc_state
#define sk_reuse __sk_common.skc_reuse
#define sk_reuseport __sk_common.skc_reuseport
#define sk_ipv6only __sk_common.skc_ipv6only
#define sk_net_refcnt __sk_common.skc_net_refcnt
#define sk_bound_dev_if __sk_common.skc_bound_dev_if
#define sk_bind_node __sk_common.skc_bind_node
#define sk_prot __sk_common.skc_prot
#define sk_net __sk_common.skc_net
#define sk_v6_daddr __sk_common.skc_v6_daddr
#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
#define sk_cookie __sk_common.skc_cookie
#define sk_incoming_cpu __sk_common.skc_incoming_cpu
#define sk_flags __sk_common.skc_flags
#define sk_rxhash __sk_common.skc_rxhash
socket_lock_t sk_lock;
atomic_t sk_drops;
int sk_rcvlowat;
struct sk_buff_head sk_error_queue;
struct sk_buff_head sk_receive_queue;
/*
* The backlog queue is special, it is always used with
* the per-socket spinlock held and requires low latency
* access. Therefore we special case it's implementation.
* Note : rmem_alloc is in this structure to fill a hole
* on 64bit arches, not because its logically part of
* backlog.
*/
struct {
atomic_t rmem_alloc;
int len;
struct sk_buff *head;
struct sk_buff *tail;
} sk_backlog;
#define sk_rmem_alloc sk_backlog.rmem_alloc
int sk_forward_alloc;
#ifdef CONFIG_NET_RX_BUSY_POLL
unsigned int sk_ll_usec;
/* ===== mostly read cache line ===== */
unsigned int sk_napi_id;
#endif
int sk_rcvbuf;
struct sk_filter __rcu *sk_filter;
union {
struct socket_wq __rcu *sk_wq;
struct socket_wq *sk_wq_raw;
};
#ifdef CONFIG_XFRM
struct xfrm_policy __rcu *sk_policy[2];
#endif
struct dst_entry *sk_rx_dst;
struct dst_entry __rcu *sk_dst_cache;
atomic_t sk_omem_alloc;
int sk_sndbuf;
/* ===== cache line for TX ===== */
int sk_wmem_queued;
refcount_t sk_wmem_alloc;
unsigned long sk_tsq_flags;
struct sk_buff *sk_send_head;
struct sk_buff_head sk_write_queue;
__s32 sk_peek_off;
int sk_write_pending;
__u32 sk_dst_pending_confirm;
u32 sk_pacing_status; /* see enum sk_pacing */
long sk_sndtimeo;
struct timer_list sk_timer;
__u32 sk_priority;
__u32 sk_mark;
u32 sk_pacing_rate; /* bytes per second */
u32 sk_max_pacing_rate;
struct page_frag sk_frag;
netdev_features_t sk_route_caps;
netdev_features_t sk_route_nocaps;
int sk_gso_type;
unsigned int sk_gso_max_size;
gfp_t sk_allocation;
__u32 sk_txhash;
/*
* Because of non atomicity rules, all
* changes are protected by socket lock.
*/
unsigned int __sk_flags_offset[0];
#ifdef __BIG_ENDIAN_BITFIELD
#define SK_FL_PROTO_SHIFT 16
#define SK_FL_PROTO_MASK 0x00ff0000
#define SK_FL_TYPE_SHIFT 0
#define SK_FL_TYPE_MASK 0x0000ffff
#else
#define SK_FL_PROTO_SHIFT 8
#define SK_FL_PROTO_MASK 0x0000ff00
#define SK_FL_TYPE_SHIFT 16
#define SK_FL_TYPE_MASK 0xffff0000
#endif
kmemcheck_bitfield_begin(flags);
unsigned int sk_padding : 1,
sk_kern_sock : 1,
sk_no_check_tx : 1,
sk_no_check_rx : 1,
sk_userlocks : 4,
sk_protocol : 8,
sk_type : 16;
#define SK_PROTOCOL_MAX U8_MAX
kmemcheck_bitfield_end(flags);
u16 sk_gso_max_segs;
unsigned long sk_lingertime;
struct proto *sk_prot_creator;
rwlock_t sk_callback_lock;
int sk_err,
sk_err_soft;
u32 sk_ack_backlog;
u32 sk_max_ack_backlog;
kuid_t sk_uid;
struct pid *sk_peer_pid;
const struct cred *sk_peer_cred;
long sk_rcvtimeo;
ktime_t sk_stamp;
u16 sk_tsflags;
u8 sk_shutdown;
u32 sk_tskey;
atomic_t sk_zckey;
struct socket *sk_socket;
void *sk_user_data;
#ifdef CONFIG_SECURITY
void *sk_security;
#endif
struct sock_cgroup_data sk_cgrp_data;
struct mem_cgroup *sk_memcg;
void (*sk_state_change)(struct sock *sk);
void (*sk_data_ready)(struct sock *sk);
void (*sk_write_space)(struct sock *sk);
void (*sk_error_report)(struct sock *sk);
int (*sk_backlog_rcv)(struct sock *sk,
struct sk_buff *skb);
void (*sk_destruct)(struct sock *sk);
struct sock_reuseport __rcu *sk_reuseport_cb;
struct rcu_head sk_rcu;
};

how to determine how much time needed when calling moveItemAtURL:toURL: or replaceItemAtURL:WithItemAtURL:

When moving file from one place to another, or when replacing file, I always use the methods moveItemAtURL:toURL: or replaceItemAtURL:WithItemAtURL: from NSFileManager.
When calling these methods, I want to determine how much time needed, so that I can use the NSProgressIndicator to tell users how long it's going to take. Just like when you are moving file using OSX, it tells u how much time remaining.
I have looked at the apple doc but couldn't find any information regarding this.
Wondering if this can be implemented, please advise.
You can't know in advance haw long it going to take. What you can do is compute the "percent complete" while you are copying the file. But to do that you need to use lower level APIs. You can use NSFileManagers attributesOfItemAtPath:error to get the file size and NSStreams for doing the copying (there are so many way to do this). Percent complete is bytesWritten / totalBytesInFile.
--- Edit: added sample code as a category on NSURL with a callback block passing the total number of bytes written, percen complete and estimated time left in seconds.
#import <mach/mach_time.h>
#interface NSURL(CopyWithProgress)<NSObject>
- (void) copyFileURLToURL:(NSURL*)destURL withProgressBlock:(void(^)(double, double, double))block;
#end
#implementation NSURL(CopyWithProgress)
- (void) copyFileURLToURL:(NSURL*)destURL
withProgressBlock:(void(^)(double, double, double))block
{
///
// NOTE: error handling has been left out in favor of simplicity
// real production code should obviously handle errors.
NSUInteger fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:self.path error:nil].fileSize;
NSInputStream *fileInput = [NSInputStream inputStreamWithURL:self];
NSOutputStream *copyOutput = [NSOutputStream outputStreamWithURL:destURL append:NO];
static size_t bufferSize = 4096;
uint8_t *buffer = malloc(bufferSize);
size_t bytesToWrite;
size_t bytesWritten;
size_t copySize = 0;
size_t counter = 0;
[fileInput open];
[copyOutput open];
uint64_t time0 = mach_absolute_time();
while (fileInput.hasBytesAvailable) {
do {
bytesToWrite = [fileInput read:buffer maxLength:bufferSize];
bytesWritten = [copyOutput write:buffer maxLength:bytesToWrite];
bytesToWrite -= bytesWritten;
copySize += bytesWritten;
if (bytesToWrite > 0)
memmove(buffer, buffer + bytesWritten, bytesToWrite);
}
while (bytesToWrite > 0);
if (block != nil && ++counter % 10 == 0) {
double percent = (double)copySize / fileSize;
uint64_t time1 = mach_absolute_time();
double elapsed = (double)(time1 - time0)/NSEC_PER_SEC;
double estTimeLeft = ((1 - percent) / percent) * elapsed;
block(copySize, percent, estTimeLeft);
}
}
if (block != nil)
block(copySize, 1, 0);
}
#end
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSURL *fileURL = [NSURL URLWithString:#"file:///Users/eric/bin/data/english-words.txt"];
NSURL *destURL = [NSURL URLWithString:#"file:///Users/eric/Desktop/english-words.txt"];
[fileURL copyFileURLToURL:destURL withProgressBlock:^(double bytes, double pct, double estSecs) {
NSLog(#"Bytes=%f, Pct=%f, time left:%f s",bytes,pct,estSecs);
}];
}
return 0;
}
Sample Output:
Bytes=40960.000000, Pct=0.183890, time left:0.000753 s
Bytes=81920.000000, Pct=0.367780, time left:0.004336 s
Bytes=122880.000000, Pct=0.551670, time left:0.002672 s
Bytes=163840.000000, Pct=0.735560, time left:0.001396 s
Bytes=204800.000000, Pct=0.919449, time left:0.000391 s
Bytes=222742.000000, Pct=1.000000, time left:0.000000 s
I mostly concur with CRD. I just want to note that under certain common circumstances, both -moveItemAtURL:toURL: and -replaceItemAtURL:WithItemAtURL:... are very fast. When the source and destination are on the same volume, no data has to be copied or moved, only metadata. When the volume is local (as opposed to network-mounted), this typically takes negligible time. That said, it is appropriate to plan for the possibility that they could take significant time.
Also, he mentioned the copyfile() routine for moving files. A copy followed by deleting the original is the necessary approach when moving a file between volumes, but the rename() system call will perform a move within a volume without needing to copy anything. So, a reasonable approach would be to try rename() first and, if it fails with EXDEV, fall back to copyfile().
Finally, the exchangedata() system call can be used as part of a reimplementation of -replaceItemAtURL:WithItemAtURL:....
I don't recommend the approach suggested by aLevelOfIndirection because there are a lot of fiddly details about copying files. It's much better to rely on system libraries than trying to roll your own. His example completely ignores file metadata (file dates, extended attributes, etc.), for example.
The methods moveItemAtURL:toURL: and replaceItemAtURL:WithItemAtURL: are high-level operations. While they provide the semantics you want for the move/replace, as you've found out, they don't provide the kind of feedback you wish during those operations.
Apple is in the process of changing lower-level file handling routines, many are now marked as deprecated in 10.8, so you'll want to pick carefully what you choose to use. However at the lowest levels, system calls (manual section 2) and library functions (manual section 3), there are functions that you can use that are not being deprecated.
One option, there are others, is the function copyfile (manual section 3) which will copy a file or folder hierarchy and provides for a progress callback. That should give you most of the semantics of moveItemAtURL:toURL: along with progress, but you'll need to do more work for replaceItemAtURL:WithItemAtURL: to preserve safety (no data loss in case of error).
If that doesn't meet all your needs you can also look additionally at the low-evel stat and friends to find out file sizes etc.
HTH

Disabling Eclipse code formatting for part of a javadoc

I have a Java class for which part of the javadoc is actually generated as part of the build process: the return value of a method (a static String value) is inserted into the source file, much like $Revision: $ tags work in some version control software.
While this behaviour may be questionable, such duplication of information is required by the framework I use (WEKA machine learning library). I would like Eclipse's code formatter not to interfere with the generated comments. I am using the Eclipse Indigo release.
I can turn the formatter on/off with special comments //#formatter:on and //#formatter:off. However, the #formatter tags are only functional in 'normal' comments, not in javadoc comments. Obviously, they could be easily confused for javadoc tags. This means I cannot turn off the formatter (for example, automatic line breaking) for the generated part of the javadoc comment, and leave it on for the rest, because the #formatter directives must be placed around the javadoc comment.
It there a workaround to toggle code formatting inside javadoc comments?
Been a while since this was asked/answered and helped me refine the following answer, hopefully additionally useful. I use this alternative as I wanted to run Javascript in Javadocs.
Eclipse provides the #formatter:off and #formatter:on which needs to be enabled via Windows->Preferences->java->code style->formatter:::edit button::: tab "off/on tags".
They may be used in any comments.
Around the doc stuff
// #formatter:off
/**
* javadoc
*/
// #formatter:on
But when you want the formatter off within the javadoc use the
#formatter:xxx within html comments <!-- xxxxx --> to indicate what you
are trying to do. Use the <code>...</code> bloack to ensure no formatting and
the inclusion of code as javascript.
Editted the code statements in the example as I wanted this to work on eclipse
and netbeans. I found the formatter:off work but then stopped working on a different
version of eclipse (yes I use multiple IDE versions).
/**
* <br><!-- #formatter:off -->
* <code>
* <script type="text/javascript">
* // hash structure for holding variable as name and its text
* var hashText = {};
*
* // function causes a hyper-link to be created
* function insertLink(varName, text){
* var link22;
*
* hashText[varName] = text;
*
* link22 = '' + hashText[varName] + '';
*
* document.write(link22);
* }
* function insertLinkA(varName){
* var link22;
*
* link22 = '' + hashText[varName] + '';
*
* document.write(link22);
* }
*
* function setLinkPoint(varName, text){
* hashText[varName] = text;
*
* document.write('<a id="' + varName + '"><U>' + hashText[varName] + '</U></a>');
* }
*
* function setLinkPointA(varName){
* document.write('<a id="' + varName + '"><U>' + hashText[varName] + '</U></a>');
* }
* </script>
* <code>
* <!-- #formatter:on -->
*
*
*/
You can disable formatting for the header, but I don't believe you can selectively disable formatting for nonheader javadoc comments.
I encountered the same problem and changing #formatter:off to FORMATTEROFF did the trick for me.
the idea is to avoid using # in the on/off tag.
Now I can disable the formatter around a part of a javadoc.